Tuple routing can write rows into a partition pending DETACH PARTITION CONCURRENTLY (run-time pruning under READ COMMITTED, or a post-detach snapshot under REPEATABLE READ/SERIALIZABLE), silently moving them out of the partitioned table ============================================================================================================================================================================================================================================ AI-generated bug report from the AI Annex Report ID: 01a2049c-0103-7f12-9984-85303d81115e Filed: 2026-11-22 18:54:22 UTC Severity: 60/100 Blamed commit: 71f4c8c6f74ba021e55d35b1128d22fb8c6e1629 ## Summary While a partition is pending DETACH PARTITION ... CONCURRENTLY (the second transaction is waiting, or was cancelled and FINALIZE has not run yet), tuple routing can still send rows into that partition even though the writing statement's snapshot already treats it as detached. The rows are accepted silently, are not visible through the partitioned table, and end up in the standalone table once the detach completes. An UPDATE that moves rows this way makes them disappear from the partitioned table. This is what the routing code is meant to prevent. The comment in ExecInitPartitionDispatchInfo() says a READ COMMITTED transaction "immediately gets a 'no partition for tuple' error", and a plain INSERT does get that error. Two paths bypass it: 1. READ COMMITTED: the statement also uses executor partition pruning (for example a generic plan of a prepared UPDATE ... WHERE key = $1, or a WHERE key = (SELECT ...) clause). 2. REPEATABLE READ or SERIALIZABLE: the transaction's snapshot was taken after the first DETACH transaction committed. Its scans omit the partition, but routing still includes it, so the transaction cannot see the rows it has just written. ## Reproduction Setup: ```sql CREATE TABLE p (a int, b text) 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 p3 PARTITION OF p FOR VALUES IN (3); INSERT INTO p VALUES (1, 'one'), (2, 'two'); ``` Session 1: ```sql BEGIN; SELECT count(*) FROM p; ``` Session 2 (commits phase 1, then waits for session 1): ```sql ALTER TABLE p DETACH PARTITION p2 CONCURRENTLY; ``` Session 3, READ COMMITTED, autocommit: ```sql INSERT INTO p VALUES (2, 'plain'); -- ERROR: no partition of relation "p" found for row (expected) UPDATE p SET a = 2 WHERE a = 1; -- ERROR: no partition of relation "p" found for row (expected) INSERT INTO p SELECT 2, 'via-pruning' FROM p WHERE a = (SELECT 1); -- INSERT 0 1 (wrong) SET plan_cache_mode = force_generic_plan; PREPARE u(int) AS UPDATE p SET a = 2, b = 'moved' WHERE a = $1; EXECUTE u(1); -- UPDATE 1 (wrong) SELECT * FROM p; -- (0 rows): the row 'one' is no longer in p ``` Then COMMIT in session 1. The detach completes, and the standalone table p2 contains (2,'two'), (2,'via-pruning') and (2,'moved'). The two statements that should have failed like their plain counterparts succeeded, and their rows left the partitioned table. With plan_cache_mode = force_custom_plan the same EXECUTE fails with the expected error. REPEATABLE READ variant (same setup; session 1 and session 2 as above, or cancel session 2 so the partition stays pending): ```sql BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT * FROM p; -- (1,'one') only INSERT INTO p VALUES (2, 'inserted'); -- INSERT 0 1 UPDATE p SET a = 2 WHERE a = 1; -- UPDATE 1 SELECT * FROM p; -- 0 rows COMMIT; ``` Expected: the INSERT and the UPDATE fail as they do under READ COMMITTED, because this snapshot already treats p2 as detached. Actual: both succeed, and the transaction cannot see its own rows. SERIALIZABLE behaves the same. Control: a REPEATABLE READ transaction whose snapshot was taken before the detach started still sees p2, and its inserted row is visible to it. That case is intended. ## Analysis ExecInitPartitionDispatchInfo() (execPartition.c) creates estate->es_partition_directory only if it does not exist yet, with omit_detached = !IsolationUsesXactSnapshot(). Executor pruning (CreatePartitionPruneState()) also uses estate->es_partition_directory and creates it with omit_detached = false. Initial and exec pruning states are built before ModifyTable sets up tuple routing, so in a statement with run-time pruning the routing code reuses a directory that includes detach-pending partitions. PartitionDirectoryLookup() then returns the same partition descriptor to both. Under snapshot isolation, routing always passes omit_detached = false, so it includes the pending partition even when its pg_inherits xmin is already visible to the active snapshot. The planner omits the partition in that case (find_inheritance_children_extended() checks the xmin against the active snapshot), so scans and routing disagree inside the same transaction. Using the snapshot-based omission for routing would still keep the partition for transactions whose snapshot predates the detach. Both behaviors come from the commit that added DETACH CONCURRENTLY, 71f4c8c6f74ba021e55d35b1128d22fb8c6e1629. It introduced the isolation-dependent flag for routing and the shared directory with pruning. 8aba9322511f718f12b618470d8c07f0ee5f0700 later inverted the flag's meaning but kept the same behavior. ## Notes Tested on master 9b5d4e3331753b3399e84965093109cac8f364bf. No crash or assertion failure: the rows are written to the wrong place without any error. The phase-2 wait does block on the writing transaction, so the rows end up in the detached table and not as dangling heap data. Related: 01a2034f-7f8b-70f4-8132-225714021bc9 and 01a201f9-676e-7002-ab35-e44c121e0f98 (same commit, different mechanism and fix location). Branches: listed as REL_14_STABLE through master, the supported branches that contain the blame commit. Only master was tested.