Startup partition pruning skips an attached leaf while another partition is pending detach ========================================================================================== AI-generated bug report from the AI Annex Report ID: 01a1c792-2015-7546-82ef-7acba134ab39 Filed: 2026-11-10 22:26:44 UTC Severity: 80/100 Blamed commit: 71f4c8c6f74ba021e55d35b1128d22fb8c6e1629 Startup pruning of a nested partitioned table can skip a leaf that is still attached while another partition of the same parent is pending detach. DETACH PARTITION CONCURRENTLY commits the detach-pending flag, then waits. Until the detach is finalized, the planner omits that partition and the executor's startup-pruning descriptor still includes it. CreatePartitionPruneState remaps subplan_map and subpart_map onto the executor descriptor, but copies present_parts unchanged, so those bits stay in planner partition indexes. find_matching_subplans_recurse uses present_parts when a level has no pruning steps. A startup predicate on an ancestor's partition key, with no predicate on the key of the level whose partition set changed, therefore walks the stale indexes. A pending partition that is not last in bound order shifts every later index. The walk treats the pending slot as already pruned and never visits the following live leaf, so the query silently drops that leaf's rows. The pending partition's own rows stay hidden, which is correct: they were not in the plan. The lost rows belong to a different partition that is still attached. Plan-time pruning of an immutable predicate on the same key is not affected, because it never consults present_parts at execution. Cancelling the waiting detach leaves the detach-pending flag set, and the wrong result continues until DETACH PARTITION ... FINALIZE. The planner/executor descriptor split was introduced by 71f4c8c6f74ba021e55d35b1128d22fb8c6e1629 (ALTER TABLE ... DETACH PARTITION ... CONCURRENTLY). present_parts was already copied from the plan while subplan_map was rebuilt for a descriptor that contained extra partitions. The wrong result is on master and on REL_14_STABLE through REL_19_STABLE. ## Reproduction ```sql CREATE TABLE parent (a int, b int) PARTITION BY RANGE (a); CREATE TABLE mid_lo PARTITION OF parent FOR VALUES FROM (0) TO (100) PARTITION BY LIST (b); CREATE TABLE mid_hi PARTITION OF parent FOR VALUES FROM (100) TO (200) PARTITION BY LIST (b); CREATE TABLE leaf_lo_1 PARTITION OF mid_lo FOR VALUES IN (1); CREATE TABLE leaf_lo_2 PARTITION OF mid_lo FOR VALUES IN (2); CREATE TABLE leaf_lo_3 PARTITION OF mid_lo FOR VALUES IN (3); CREATE TABLE leaf_hi_1 PARTITION OF mid_hi FOR VALUES IN (1); INSERT INTO leaf_lo_1 VALUES (10, 1); INSERT INTO leaf_lo_2 VALUES (20, 2); INSERT INTO leaf_lo_3 VALUES (30, 3); INSERT INTO leaf_hi_1 VALUES (150, 1); CREATE FUNCTION stab_int(int) RETURNS int LANGUAGE plpgsql STABLE AS $$ BEGIN RETURN $1; END $$; ``` plpgsql keeps the call from being inlined to a constant. Before the detach, both of the queries below return (10, 1), (20, 2), and (30, 3). Hold an ACCESS SHARE lock so the detach can commit its first phase and then block: ```sql BEGIN; LOCK TABLE mid_lo IN ACCESS SHARE MODE; ``` In another session: ```sql ALTER TABLE mid_lo DETACH PARTITION leaf_lo_2 CONCURRENTLY; ``` After pg_inherits.inhdetachpending is true for leaf_lo_2: ```sql SELECT a, b FROM parent WHERE a < stab_int(100) ORDER BY a, b; ``` Expected: (10, 1) and (30, 3). leaf_lo_2 is pending detach and must not be returned. leaf_hi_1 does not satisfy a < 100. Actual: only (10, 1). The immutable control still returns both live rows: ```sql SELECT a, b FROM parent WHERE a < 100 ORDER BY a, b; ``` That returns (10, 1) and (30, 3). EXPLAIN (ANALYZE) of the stable predicate reports Subplans Removed: 2 and a scan of leaf_lo_1 only. With the fix it reports Subplans Removed: 1 and scans leaf_lo_1 and leaf_lo_3. Assertions were enabled; the backend did not crash. ## Diagnosis Partition descriptors used for planning pass omit_detached, so a partition whose pg_inherits row is detach-pending disappears once that row's xmin is not in the active snapshot. Startup pruning builds its directory with omit_detached false, so the pending partition is still present and later leaves move to higher indexes. The fast path copies the plan's maps only when the OID arrays are identical. Otherwise subplan_map and subpart_map are rebuilt in executor order, and any OID the plan does not contain is stored as -1. present_parts is not rebuilt. It remains a bitmap of planner indexes, including indexes of partitions the planner kept. When the list level has no pruning steps, the startup pass takes partset from present_parts and indexes the remapped arrays with those bits. For leaves ordered (1), (2), (3) with (2) pending detach, the planner bitmap is {0, 1} while the executor maps are {leaf 1 -> subplan, pending -> -1, leaf 3 -> subplan}. Index 1 selects the pending slot and index 2, the live leaf, is never visited. A level that has its own pruning steps is not affected: the matching bitmap comes from the executor descriptor, which lines up with the remapped maps. That is why an immutable a < 100, pruned while planning, still returns both attached leaves. ## Fix When the OID arrays differ, rebuild present_parts from the translated maps. An index is present when subplan_map or subpart_map at that index is non-negative. When the arrays match, keep a private copy of the plan's bitmap, as before. Initial pruning then sees the still-attached leaves. The same rule applies on each affected branch. Branches before REL_18_STABLE have no leafpart_rti_map, so the surrounding code differs, but the bitmap rebuild is the same. The score is 80, the wrong-results anchor. The miss is silent: no error and no assertion. It needs nested partitioning, startup pruning only on an ancestor, and a non-final partition pending detach. That narrow trigger is why the score is not higher.