Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Foreing keys on partitioned tables

What is the best approach to add a foreign key on a partitoned table on Postgresql?

1st approach

Add foreign keys on child tables always as NOT VALID.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ALTER TABLE <partition_name> ADD CONSTRAINT <foreign_key_name> 
FOREIGN KEY (someId) REFERENCES reftable(someId) NOT VALID;

Validate the foreign key on child tables.

ALTER TABLE <partition_name> VALIDATE CONSTRAINT <foreign_key_name>;

Create the foreign key on the parent table.

ALTER TABLE <parent_table_name> ADD CONSTRAINT <foreign_key_name> 
FOREIGN KEY (someId) REFERENCES reftable(someId);

2nd approach

Add foreign key on parent table as NOT VALID.

ALTER TABLE <parent_table_name> ADD CONSTRAINT <foreign_key_name> 
FOREIGN KEY (someId) REFERENCES reftable(someId) NOT VALID;

Validate the foreign key on the parent table:

ALTER TABLE <parent_table_name> VALIDATE CONSTRAINT <foreign_key_name>;

>Solution :

The answer is simple: the second method is not possible and will lead to the error

ERROR:  cannot add NOT VALID foreign key on partitioned table "<parent_table_name>" referencing relation "reftable"
DETAIL:  This feature is not yet supported on partitioned tables.

So, by exclusion, the first method is the better one. It minimizes locking as well: only the final statement that creates the constraint on the partitioned table requires a lock that conflicts with data modifications, and that statement will be very fast.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading