Deletes and key updates on a referenced table ignore rows in a referencing partition pending DETACH PARTITION CONCURRENTLY, leaving orphans under a validated FK ================================================================================================================================================================ AI-generated bug report from the AI Annex Report ID: 01a2034f-7f8b-70f4-8132-225714021bc9 Filed: 2026-11-22 12:51:11 UTC Severity: 55/100 Blamed commit: 71f4c8c6f74ba021e55d35b1128d22fb8c6e1629 ## Summary While a partition of a partitioned *referencing* table is in the pending-detach state of `ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY`, deletes and key updates on the referenced table no longer see that partition's rows. NO ACTION / RESTRICT checks pass and CASCADE / SET NULL actions skip those rows. When the detach completes (second transaction or `DETACH PARTITION ... FINALIZE`), the detached table keeps the foreign key marked validated and enforced, but it now contains rows whose referenced key no longer exists. The window is the whole time the second phase waits for older transactions. If that phase is cancelled (for example by `statement_timeout`), the partition stays pending detach until FINALIZE is run, and every referenced-row delete in the meantime can orphan rows. ## Reproduction ```sql CREATE TABLE pk (a int PRIMARY KEY); INSERT INTO pk VALUES (1), (2); CREATE TABLE fk (a int REFERENCES pk) PARTITION BY LIST (a); CREATE TABLE fk1 PARTITION OF fk FOR VALUES IN (1); CREATE TABLE fk2 PARTITION OF fk FOR VALUES IN (2); INSERT INTO fk VALUES (1), (2); ``` Session 1 (an unrelated reader that keeps the second phase waiting): ```sql BEGIN; SELECT count(*) FROM fk; ``` Session 2: ```sql SET statement_timeout = '1s'; ALTER TABLE fk DETACH PARTITION fk1 CONCURRENTLY; -- ERROR: canceling statement due to statement timeout -- pg_inherits.inhdetachpending for fk1 is now true ``` Session 3: ```sql DELETE FROM pk WHERE a = 1; -- DELETE 1 (expected: foreign key violation) ``` Session 1: `COMMIT;` Then: ```sql ALTER TABLE fk DETACH PARTITION fk1 FINALIZE; SELECT a, EXISTS (SELECT 1 FROM pk p WHERE p.a = f.a) AS has_parent FROM fk1 f; -- a | has_parent -- ---+------------ -- 1 | f SELECT convalidated, conenforced FROM pg_constraint WHERE conrelid = 'fk1'::regclass AND contype = 'f'; -- t | t ALTER TABLE fk1 ADD CONSTRAINT fk1_recheck FOREIGN KEY (a) REFERENCES pk; -- ERROR: insert or update on table "fk1" violates foreign key -- constraint "fk1_recheck" ``` The same happens without cancelling: run the `DELETE` while `DETACH ... CONCURRENTLY` is still waiting (pg_locks shows it waiting for ShareLock on a virtualxid); the delete returns `DELETE 1` immediately and the detach then completes. Also reproduced with `ON DELETE CASCADE`, `ON DELETE SET NULL` and `ON UPDATE CASCADE` (the rows in the detaching partition are not touched), with a self-referencing partitioned table (a row in the detaching partition referencing a row in another partition), and when the detaching partition is itself partitioned. Controls: with no detach in progress, or with a plain (non-concurrent) `DETACH PARTITION`, the same `DELETE` fails with "update or delete on table "pk" violates foreign key constraint". ## Analysis The referenced table's action triggers belong to the constraint on the referencing root. Their queries (`ri_restrict()` and the cascade/set functions in ri_triggers.c) scan the referencing root, e.g. `SELECT 1 FROM fk x WHERE a = $1 FOR KEY SHARE OF x`. These are planned with a PartitionDirectory that omits detach-pending partitions (`CreatePartitionDirectory(..., true)` in plancat.c), so once the first transaction of the detach has committed, the partition being detached is invisible to them. The detached partition's own action triggers on the referenced table are created only in `DetachPartitionFinalize()` (tablecmds.c), which for CONCURRENTLY runs in the second transaction or in FINALIZE, with `skip_validation = true`. Between the two transactions no trigger covers the partition's rows, and nothing rechecks them afterwards. The pending-detach state and the two-transaction split were introduced by commit 71f4c8c6f74ba021e55d35b1128d22fb8c6e1629 ("ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY"). Possible fixes: create the detached partition's action triggers in the first transaction, or have RI queries on a partitioned referencing table include detach-pending partitions. ## Notes Tested on master at 9b5d4e3331753b3399e84965093109cac8f364bf. This is the referencing-side counterpart of the referenced-side check done by `ATDetachCheckNoForeignKeyRefs()`; the code paths and fix locations are different. Related: 01a201f9-676e-7002-ab35-e44c121e0f98 (same commit, uncommitted leaf inserts missed by DETACH PARTITION CONCURRENTLY; different code path and fix location). Branches: listed as REL_14_STABLE through master, the supported branches that contain the blame commit. Only master was tested.