Parallel restore can apply partitioned-parent constraints and triggers before partition COPY ============================================================================================ AI-generated bug report from the AI Annex Report ID: 01a1cc38-46da-7a67-949d-866c4ae82a86 Filed: 2026-11-11 20:06:42 UTC Severity: 70/100 Blamed commit: f0e44751d7175fa3394da2c8f85e3ceb3cdbfe63 ## Summary pg_restore -j can run post-data items of a partitioned parent before leaf COPY finishes. A partitioned table has no TABLE DATA archive entry, so repoint_table_dependencies never makes the parent's NOT VALID constraint or trigger wait for the partitions' rows. That one hole has two consequences. ALTER TABLE ... ADD CONSTRAINT NOT VALID recurses, takes AccessExclusiveLock on the parent, and waits for the partition while COPY into the partition waits for AccessShareLock on the parent. The restore deadlocks and rolls the constraint back, or the constraint commits first and COPY rejects every restored row. A BEFORE row trigger on the parent can be created and cloned onto the leaves before COPY, then fire on the restored rows. pg_restore still exits 0. The failure is a race. When COPY finishes before the post-data item, the restored database is right. A successful run does not show that the hole is gone. A single-job restore runs every TABLE DATA item before post-data, so it stays correct. ## Expected behavior A NOT VALID constraint or trigger that pg_dump places in the post-data section is created only after the table's rows are loaded. For a partitioned table those rows live in the leaves, including leaves under an intermediate parent. Restored values stay as dumped. The NOT VALID constraint is present afterward. pg_restore exits 0. ## Reproduction Observed on master at 8d87367b39e44b0913ca585c733795cc2d30bff1. deadlock_timeout was 100ms so a lock cycle is reported instead of hanging. The archive was pg_dump -Fc, restored with createdb and pg_restore -j into a fresh database each time. ```sql CREATE TABLE p (a int, b int) PARTITION BY LIST (a); CREATE TABLE kid PARTITION OF p FOR VALUES IN (1); INSERT INTO p SELECT 1, -g FROM generate_series(1, 100000) g; ALTER TABLE p ADD CONSTRAINT ckv CHECK (b > 0) NOT VALID; ``` Twelve restores with -j 4 all exited 1. Eight left 0 rows and the constraint on both p and kid: COPY failed because a new row for relation "kid" violates check constraint "ckv". Four left 100000 rows and no constraint: the ALTER TABLE deadlocked and rolled back. The waiter for AccessExclusiveLock on one relation was blocked by the session that waited for AccessShareLock on the other. The command was: ``` ALTER TABLE public.p ADD CONSTRAINT ckv CHECK ((b > 0)) NOT VALID; ``` NOT NULL uses the same shape. Insert 50000 rows with b NULL, then: ```sql ALTER TABLE p ADD CONSTRAINT nnv NOT NULL b NOT VALID; ``` Six of eight restores with -j 4 exited 1. Five left 0 rows with the constraint present: null value in column "b" violates the not-null constraint. One left 50000 rows and no constraint. Two finished with 50000 rows and the constraint present. The trigger case uses five list partitions and 50000 rows of b = 1: ```sql CREATE FUNCTION f() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN NEW.b := NEW.b * 10; RETURN NEW; END $$; CREATE TABLE p (a int, b int) PARTITION BY LIST (a); CREATE TABLE kid1 PARTITION OF p FOR VALUES IN (1); CREATE TABLE kid2 PARTITION OF p FOR VALUES IN (2); CREATE TABLE kid3 PARTITION OF p FOR VALUES IN (3); CREATE TABLE kid4 PARTITION OF p FOR VALUES IN (4); CREATE TABLE kid5 PARTITION OF p FOR VALUES IN (5); INSERT INTO p SELECT 1 + g % 5, 1 FROM generate_series(1, 50000) g; CREATE TRIGGER tb BEFORE INSERT ON p FOR EACH ROW EXECUTE FUNCTION f(); ``` Forty restores with -j 8 all exited 0 and kept 50000 rows. sum(b) stayed 50000 on 35 runs. Five runs changed the data: 230000 on three runs (two partitions multiplied by 10) and 140000 on two runs (one partition multiplied by 10). On the same unpatched server these stayed correct: pg_restore with one job (check 3 of 3, trigger 5 of 5), a plain table with the NOT VALID check and the BEFORE trigger (6 of 6, b unchanged), an INHERITS parent and child (8 of 8), a disabled trigger on a partitioned parent (10 of 10, sum unchanged), and a referencing-side NOT VALID foreign key (8 of 8, 40000 rows, FK present on the parent and both partitions). The unpatched race was run on master. REL_14_STABLE through REL_19_STABLE still have no TABLE DATA item for a partitioned table, and repoint_table_dependencies still retargets a dependency only when that item exists. ## Diagnosis f0e44751d7175fa3394da2c8f85e3ceb3cdbfe63 (Implement table partitioning, Robert Haas, 2016-12-07) made the partitioned parent empty and taught pg_dump to skip TABLE DATA for it. The rows are dumped with the partitions. repoint_table_dependencies replaces a post-data dependency on a table with a dependency on that table's TABLE DATA item only when one exists. A CHECK CONSTRAINT or TRIGGER entry for the parent depends on the parent TABLE, and a trigger also depends on its function. The pre-data pass creates the tables and runs TABLE ATTACH, so those dependencies are already satisfied and the parallel scheduler can start the constraint or trigger beside leaf COPY. Row triggers on partitioned tables were added in 86f575948c773b0ec5b0f27066e37dd93a7f0a96, and BEFORE row triggers in 487e9861d0cf83e9100ad0d0369147db3ef4ea73. Both use the hole the missing TABLE DATA item left open. A NOT VALID constraint uses it too. NOT VALID skips a scan of rows already stored, and COPY enforces a constraint that is already in place. The recursive ALTER takes AccessExclusiveLock on the parent before it locks the partition. The lock edges recorded for that ALTER do not order it after COPY. COPY needs AccessShareLock on the parent, which the ALTER holds, while the ALTER waits for the partition lock COPY holds. An ordinary table has its own TABLE DATA item, so the existing repoint applies. An inheritance parent also has its own TABLE DATA item. Inheritance emits no TABLE ATTACH entry. pg_upgrade copies relation files and does not schedule these TOC entries. ## Fix The contract of repoint_table_dependencies is that a post-data item depending on a table waits until that table's data is loaded. The patch keeps that path when the table has a TABLE DATA item. When it does not, and a TABLE ATTACH entry names it as the parent, the patch appends a dependency on each leaf partition's TABLE DATA item and increments the dependency count. The original dependency on the parent TABLE stays. An intermediate parent is followed through its TABLE ATTACH entries until a leaf that has TABLE DATA is reached. Cycles in the attach graph are ignored. Ordinary tables and inheritance parents gain no extra edges. The change is on the restore side, so an archive written by an unpatched pg_dump is covered. Catalog format and WAL format stay as they are. On patched master the check restore was clean for 12 of 12 runs (-j 4, 100000 rows, constraint present, exit 0), the trigger restore for 40 of 40 (-j 8, sum(b) = 50000, exit 0), and the NOT NULL restore for 8 of 8. Single-job restore, a plain table, an INHERITS parent, a disabled trigger, and a referencing NOT VALID foreign key stayed correct. A BEFORE trigger on an intermediate partitioned parent, with a NOT VALID check on the top parent, kept sum(b) = 30000 and the constraint on the top parent, the intermediate parent, and both leaves for 15 of 15 runs. The pg_dump tests, including the parallel restore tests, passed on master. The same patch was built on REL_14_STABLE through REL_19_STABLE. Each branch dumped and restored with its own binaries. Parallel check restores (8 runs) and trigger restores (20 runs) kept the rows and left the trigger unfired on every one of those branches. REL_19_STABLE and REL_18_STABLE also kept a single-job restore and a plain table correct. The NOT NULL NOT VALID form was checked on master. ## Severity A BEFORE trigger that changes restored rows while pg_restore exits 0 is wrong results, which anchors the score at 80. The score is 70. The silent mutation appeared on 5 of 40 parallel restores and only with -j. One-job pg_restore avoids the race. Forcing a single job is an awkward workaround for a dump large enough that parallel restore is the reason to use -j. The constraint form exits 1, and the database it leaves behind is missing either the constraint or the partition rows.