From 9b3c81c281f30b7f3c617f094abc01ac7e3b1010 Mon Sep 17 00:00:00 2001 From: Jared Mackey <10620022+jared-mackey@users.noreply.github.com> Date: Wed, 3 May 2023 02:48:37 -0600 Subject: [PATCH] Add example schema for #1582 (#1585) --- issues/1582/schema.sql | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 issues/1582/schema.sql diff --git a/issues/1582/schema.sql b/issues/1582/schema.sql new file mode 100644 index 00000000..df146158 --- /dev/null +++ b/issues/1582/schema.sql @@ -0,0 +1,39 @@ +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + +-- Table without any dependencies +CREATE TABLE IF NOT EXISTS public.table_1 ( + id uuid DEFAULT public.uuid_generate_v4() NOT NULL PRIMARY KEY +); + +-- Table that depends on table_1 +CREATE TABLE IF NOT EXISTS public.table_2 ( + id uuid DEFAULT public.uuid_generate_v4() NOT NULL, + table_1_id uuid NOT NULL REFERENCES public.table_1(id), + -- Add constraints / uniques here if needed. Will auto apply to all partitions. + PRIMARY KEY (id, table_1_id) +) +PARTITION BY HASH (table_1_id); + +-- Generate partition tables. +-- No clue if this works on other SQL implementations than PostgreSQL. +DO $$ + DECLARE + counter integer := 0; + BEGIN + WHILE counter < 128 + LOOP + EXECUTE('CREATE TABLE IF NOT EXISTS public.table_2_p_hash_p' || counter || ' PARTITION OF public.table_2 FOR VALUES WITH (MODULUS 128, REMAINDER ' || counter || ');'); + counter := counter + 1; + END LOOP; +END$$; + +-- Table that depends on table_1 and table_2. +-- Foreign keys to table_2 are through the partitions +CREATE TABLE IF NOT EXISTS public.table_3 ( + id uuid DEFAULT public.uuid_generate_v4() NOT NULL PRIMARY KEY, + -- FK to table_1 only needed so that we can FK to table_2 + table_1_id uuid REFERENCES public.table_1(id) NOT NULL, + table_2_id uuid NOT NULL, + FOREIGN KEY (table_2_id, table_1_id) REFERENCES public.table_2(id, table_1_id) +); +