FRN402[high]Constraints
Foreign key added without NOT VALID
Validating a new foreign key scans the table and blocks writes on both tables.
Why it's risky in PostgreSQL
A new foreign key checks every existing row and takes SHARE ROW EXCLUSIVE locks on both the referencing and referenced tables.
Add it NOT VALID, then run VALIDATE CONSTRAINT in a later migration: in the same transaction the first locks are still held while VALIDATE scans the table.
What triggers it, and the safer way
ALTER TABLE orders ADD CONSTRAINT orders_customer_fk FOREIGN KEY (customer_id) REFERENCES customers (id);-- migration 1
ALTER TABLE orders ADD CONSTRAINT orders_customer_fk FOREIGN KEY (customer_id) REFERENCES customers (id) NOT VALID;
-- migration 2, its own transaction
ALTER TABLE orders VALIDATE CONSTRAINT orders_customer_fk;Suppress it when you're sure
Put this comment on the line before the statement. The reason is required and shows in the report.
-- fronelta:allow FRN402 <why this is safe here>Applies to PostgreSQL 14, 15, 16, 17.