\set VERBOSITY terse SET search_path = 'public'; CREATE EXTENSION pg_pathman; CREATE SCHEMA fkeys; /* Check primary keys generation */ CREATE TABLE fkeys.test_ref(comment TEXT UNIQUE); INSERT INTO fkeys.test_ref VALUES('test'); CREATE TABLE fkeys.test_fkey( id INT NOT NULL, comment TEXT, FOREIGN KEY (comment) REFERENCES fkeys.test_ref(comment)); INSERT INTO fkeys.test_fkey SELECT generate_series(1, 1000), 'test'; SELECT create_range_partitions('fkeys.test_fkey', 'id', 1, 100); create_range_partitions ------------------------- 10 (1 row) INSERT INTO fkeys.test_fkey VALUES(1, 'wrong'); ERROR: insert or update on table "test_fkey_1" violates foreign key constraint "test_fkey_1_comment_fkey" INSERT INTO fkeys.test_fkey VALUES(1, 'test'); SELECT drop_partitions('fkeys.test_fkey'); NOTICE: 101 rows copied from fkeys.test_fkey_1 NOTICE: 100 rows copied from fkeys.test_fkey_2 NOTICE: 100 rows copied from fkeys.test_fkey_3 NOTICE: 100 rows copied from fkeys.test_fkey_4 NOTICE: 100 rows copied from fkeys.test_fkey_5 NOTICE: 100 rows copied from fkeys.test_fkey_6 NOTICE: 100 rows copied from fkeys.test_fkey_7 NOTICE: 100 rows copied from fkeys.test_fkey_8 NOTICE: 100 rows copied from fkeys.test_fkey_9 NOTICE: 100 rows copied from fkeys.test_fkey_10 drop_partitions ----------------- 10 (1 row) SELECT create_hash_partitions('fkeys.test_fkey', 'id', 10); create_hash_partitions ------------------------ 10 (1 row) INSERT INTO fkeys.test_fkey VALUES(1, 'wrong'); ERROR: insert or update on table "test_fkey_0" violates foreign key constraint "test_fkey_0_comment_fkey" INSERT INTO fkeys.test_fkey VALUES(1, 'test'); SELECT drop_partitions('fkeys.test_fkey'); NOTICE: 100 rows copied from fkeys.test_fkey_0 NOTICE: 90 rows copied from fkeys.test_fkey_1 NOTICE: 90 rows copied from fkeys.test_fkey_2 NOTICE: 116 rows copied from fkeys.test_fkey_3 NOTICE: 101 rows copied from fkeys.test_fkey_4 NOTICE: 90 rows copied from fkeys.test_fkey_5 NOTICE: 95 rows copied from fkeys.test_fkey_6 NOTICE: 118 rows copied from fkeys.test_fkey_7 NOTICE: 108 rows copied from fkeys.test_fkey_8 NOTICE: 94 rows copied from fkeys.test_fkey_9 drop_partitions ----------------- 10 (1 row) /* Try to partition table that's being referenced */ CREATE TABLE fkeys.messages( id SERIAL PRIMARY KEY, msg TEXT); CREATE TABLE fkeys.replies( id SERIAL PRIMARY KEY, message_id INTEGER REFERENCES fkeys.messages(id), msg TEXT); INSERT INTO fkeys.messages SELECT g, md5(g::text) FROM generate_series(1, 10) as g; INSERT INTO fkeys.replies SELECT g, g, md5(g::text) FROM generate_series(1, 10) as g; SELECT create_range_partitions('fkeys.messages', 'id', 1, 100, 2); /* not ok */ WARNING: foreign key "replies_message_id_fkey" references table "fkeys.messages" ERROR: table "fkeys.messages" is referenced from other tables ALTER TABLE fkeys.replies DROP CONSTRAINT replies_message_id_fkey; SELECT create_range_partitions('fkeys.messages', 'id', 1, 100, 2); /* ok */ create_range_partitions ------------------------- 2 (1 row) EXPLAIN (COSTS OFF) SELECT * FROM fkeys.messages; QUERY PLAN ------------------------------ Append -> Seq Scan on messages_1 -> Seq Scan on messages_2 (3 rows) DROP TABLE fkeys.messages, fkeys.replies CASCADE; NOTICE: drop cascades to 3 other objects DROP SCHEMA fkeys CASCADE; NOTICE: drop cascades to 2 other objects DROP EXTENSION pg_pathman CASCADE;