DETACH PARTITION ... FINALIZE leaves undroppable inherited constraints that pg_dump omits ========================================================================================= AI-generated bug report from the AI Annex Report ID: 01a2021a-7426-7290-91c9-0a18a5b31efd Filed: 2026-11-22 07:13:37 UTC Severity: 40/100 Blamed commit: 71f4c8c6f74ba021e55d35b1128d22fb8c6e1629 ## Summary If ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY is cancelled and the partition is left "detach pending", a later ALTER TABLE on the parent that drops a CHECK constraint or a NOT NULL constraint skips the pending partition. The partition keeps its inherited copy (conislocal = false, coninhcount = 1). DETACH PARTITION ... FINALIZE then succeeds, but it only adjusts constraints that still exist on the parent, so the now-standalone table keeps constraints that claim to be inherited although the table has no parent. They cannot be dropped ("cannot drop inherited constraint"), ALTER COLUMN ... DROP NOT NULL fails the same way, and pg_dump omits them because they are not local, so a dump/restore silently drops the constraints. ## Reproduction Session 1: CREATE TABLE p (k int, n int NOT NULL, b int CONSTRAINT p_b_chk CHECK (b > 0)) PARTITION BY LIST (k); CREATE TABLE c1 PARTITION OF p FOR VALUES IN (1); BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT count(*) FROM p; Session 2 (cancel it while it waits, e.g. with statement_timeout): SET statement_timeout = '1s'; ALTER TABLE p DETACH PARTITION c1 CONCURRENTLY; -- ERROR: canceling statement due to statement timeout Session 1: COMMIT. Then: SELECT inhdetachpending FROM pg_inherits WHERE inhrelid = 'c1'::regclass; -- t ALTER TABLE p ALTER n DROP NOT NULL; ALTER TABLE p DROP CONSTRAINT p_b_chk; ALTER TABLE p DETACH PARTITION c1 FINALIZE; -- succeeds SELECT conname, contype, conislocal, coninhcount FROM pg_constraint WHERE conrelid = 'c1'::regclass; -- conname | contype | conislocal | coninhcount -- p_b_chk | c | f | 1 -- p_n_not_null | n | f | 1 SELECT count(*) FROM pg_inherits WHERE inhrelid = 'c1'::regclass; -- 0 ALTER TABLE c1 DROP CONSTRAINT p_b_chk; -- ERROR: cannot drop inherited constraint "p_b_chk" of -- relation "c1" ALTER TABLE c1 ALTER n DROP NOT NULL; -- ERROR: cannot drop inherited constraint "p_n_not_null" of -- relation "c1" The constraints are still enforced on c1, but a plain pg_dump of the database emits neither of them, so after restore c1 accepts (1, NULL, -1). pg_dump --binary-upgrade does emit them and keeps them marked non-local, so pg_upgrade carries the inconsistent state forward. Expected: either the parent's DROP CONSTRAINT / DROP NOT NULL also processes the pending partition, or FINALIZE marks every remaining inherited constraint of the detached table as local with coninhcount = 0, as a completed plain DETACH would. ## Analysis find_inheritance_children() calls find_inheritance_children_extended() with omit_detached = true, so when an active snapshot sees the detach-pending pg_inherits row, the partition is left out. The ALTER TABLE recursion for DROP CONSTRAINT and for dropping a not-null constraint uses that list, so it never decrements the child's coninhcount or drops the child's copy. DetachPartitionFinalize() -> RemoveInheritance() walks the parent's current constraints and fixes the matching child constraints by name; a constraint that no longer exists on the parent is never matched and keeps conislocal = false, coninhcount = 1 on a table that is no longer anyone's child. The CHECK case should go back to the introduction of the pending state in 71f4c8c6f74ba021e55d35b1128d22fb8c6e1629; the NOT NULL case additionally depends on not-null constraints being catalogued (18 and later). Only master was tested. ## Notes This shares a root cause with BUG #19913, where ADD COLUMN or ALTER COLUMN TYPE on the parent during the pending window makes FINALIZE fail with "could not convert row type". Here FINALIZE succeeds and the damage is left on the standalone table, then lost by pg_dump. A fix that makes parent-side ALTER TABLE either include or refuse detach-pending partitions would address both. Tested on master at 9b5d4e3331753b3399e84965093109cac8f364bf. REL_14_STABLE through master are listed as affected because the detach-pending state exists in all of them (the NOT NULL part applies to REL_18_STABLE and later); back branches were not tested.