Report
A DETACH PARTITION CONCURRENTLY still waiting in its second transaction can finalize a newer pending detach of the same partition without waiting for that detach's lockers, so an in-flight INSERT routes a row into a partition that has since been attached elsewhere, violating its partition constraint
- Severity
- 45
- Blamed commit
- 71f4c8c6f74ba021e55d35b1128d22fb8c6e1629
On 7 branches
REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY (session A) commits its first transaction and then waits for the lockers of the parent. Meanwhile, another session can run DETACH ... FINALIZE, re-attach the same partition and start a new DETACH ... CONCURRENTLY (session B). That second detach is waiting for its own lockers, including an INSERT that started after the re-attach. When A's wait ends, A reopens the relations, finds the partition marked detach-pending (by B), and finalizes B's detach. A never waited for the lockers that B was waiting for.
The in-flight INSERT still routes rows by the partition list it built before the detach. It opens the partition lazily, after the partition has been detached and attached to another table with a different bound. Routed tuples are not checked against the leaf partition constraint, so the row lands in the new parent's partition with a key outside its bound. Pruned scans of the new parent then miss the row, and unpruned scans return it.
Reproduction
Five psql sessions, S1 to S5; the long INSERT runs in S4.
-- setup
CREATE TABLE p (a int) PARTITION BY LIST (a);
CREATE TABLE p1 PARTITION OF p FOR VALUES IN (1);
CREATE TABLE p2 PARTITION OF p FOR VALUES IN (2);
CREATE TABLE q (a int) PARTITION BY LIST (a);
CREATE TABLE q4 PARTITION OF q FOR VALUES IN (4);
-- S3: hold an old lock on the parent (no snapshot held)
BEGIN; LOCK TABLE ONLY p IN ACCESS SHARE MODE;
-- S1: blocks in its second transaction, waiting for S3
ALTER TABLE p DETACH PARTITION p1 CONCURRENTLY;
-- S2: finish the detach and re-attach the partition
ALTER TABLE p DETACH PARTITION p1 FINALIZE;
ALTER TABLE p ATTACH PARTITION p1 FOR VALUES IN (1);
-- S4: a long INSERT; the row for p1 is routed after the sleep
INSERT INTO p SELECT v FROM (VALUES (2), (1)) t(v)
WHERE v = 2 OR pg_sleep(4) IS NOT NULL
RETURNING tableoid::regclass, a;
-- S2 (within the 4 s): new concurrent detach; waits for S4
ALTER TABLE p DETACH PARTITION p1 CONCURRENTLY;
-- S3: release; S1 now completes, finalizing S2's pending detach
COMMIT;
-- S5: attach the now-free partition elsewhere
ALTER TABLE q ATTACH PARTITION p1 FOR VALUES IN (3);
Actual: S1 succeeds. S4's INSERT returns (p2,2) and (p1,1). S2
fails with relation "p1" is not a partition of relation "p". Then:
SELECT tableoid::regclass, a FROM q; -- (p1,1)
SELECT count(*) FROM q WHERE a = 1; -- 0 (pruned away)
SELECT pg_get_expr(relpartbound, oid) FROM pg_class
WHERE oid = 'p1'::regclass; -- FOR VALUES IN (3)
Partition p1 of q (FOR VALUES IN (3)) holds a row with a = 1.
Expected: the detach that finally removes p1 from p waits for every
transaction that could still route rows into p1 through p, as it does
when there is no stale waiter. Control: run only S4's INSERT and S2's
DETACH CONCURRENTLY. The detach waits for the INSERT, the row lands in
p1 while p1 is still a partition of p, and the later
ALTER TABLE q ATTACH PARTITION p1 FOR VALUES IN (3) fails with
partition constraint of relation "p1" is violated by some row.
Analysis
In ATExecDetachPartition (tablecmds.c), the concurrent path commits, calls WaitForLockersMultiple on the parent, and then reopens both relations by OID with try_relation_open. It then calls DetachPartitionFinalize, which only checks that the pg_inherits row is marked detach-pending. Nothing ties the pending state to the detach that set it. FINALIZE plus re-attach plus a new concurrent detach re-creates the same visible state, so the stale waiter completes somebody else's detach. Its set of waited-for lockers was taken before the newer lockers existed.
The INSERT holds only RowExclusiveLock on the parent, which does not conflict with the detach's ShareUpdateExclusiveLock. It locks the leaf only when the first row is routed to it (ExecInitPartitionInfo), after the detach and the new attach have committed. ExecInsert skips the partition constraint check for routed tuples, so nothing catches the misplaced row. The second-transaction logic was introduced with DETACH PARTITION CONCURRENTLY.
Notes
Tested on master at 207b5293b8d9a217bb918b5b44774b3b4f1e2f89 (assert-enabled build). Reproduced in 4 of 4 runs with the timing above. The cleaner outcomes in other variants (the stale waiter errors out when the partition is not pending-detach, or was dropped or moved) are fine. The problem is specifically a newer pending detach of the same partition.
The same code in ATExecDetachPartition is present on all supported branches since REL_14_STABLE; back branches were not tested.