Patch
Fix runtime pruning across a pending concurrent detach
- Branch
- REL_18_STABLE
- Risk
- –
- Base commit
- f1b267e784135c74cfd4144902fd41ceb1cda0a4
To apply it to a checkout of REL_18_STABLE at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1c792-2016-7d44-90cf-c6f83164d8f6 | jq -r .body | git am
From 50ed158029165db03612b27a9972280249a9f497 Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Tue, 10 Nov 2026 14:22:55 -0800
Subject: [PATCH] Fix runtime pruning across a pending concurrent detach
CreatePartitionPruneState translates subplan_map and subpart_map when
the executor partition descriptor does not match the planner's. A
partition pending detach is omitted by the planner and included by the
executor, so those indexes shift. present_parts was still copied from
the plan and stayed in planner order.
find_matching_subplans_recurse uses present_parts for a level with no
pruning steps. Nested startup pruning then walked the stale indexes
and could skip a leaf that is still attached.
Rebuild present_parts from the translated maps.
---
src/backend/executor/execPartition.c | 28 +++++++++++++++++++++++++---
1 file changed, 25 insertions(+), 3 deletions(-)
diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index 4b1343d749d..d1531b94c1a 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -2118,6 +2118,12 @@ CreatePartitionPruneState(EState *estate, PartitionPruneInfo *pruneinfo,
pprune->leafpart_rti_map = pinfo->leafpart_rti_map;
memcpy(pprune->subplan_map, pinfo->subplan_map,
sizeof(int) * pinfo->nparts);
+
+ /*
+ * present_parts is also subject to later modification.
+ * Partition indexes match the plan, so copy it directly.
+ */
+ pprune->present_parts = bms_copy(pinfo->present_parts);
}
else
{
@@ -2195,10 +2201,26 @@ CreatePartitionPruneState(EState *estate, PartitionPruneInfo *pruneinfo,
pprune->subplan_map[pp_idx] = -1;
pprune->leafpart_rti_map[pp_idx] = 0;
}
- }
- /* present_parts is also subject to later modification */
- pprune->present_parts = bms_copy(pinfo->present_parts);
+ /*
+ * present_parts in the plan is indexed by the planner's
+ * partition descriptor. The maps above were rebuilt in the
+ * executor descriptor's order, which differs when a partition
+ * is pending detach: the planner omits it and the executor
+ * includes it. A level with no pruning steps walks
+ * present_parts as indexes into those maps, so rebuild the
+ * bitmap from the translated maps. A partition is present
+ * when it still has a subplan or a subpartition.
+ */
+ pprune->present_parts = NULL;
+ for (pp_idx = 0; pp_idx < partdesc->nparts; pp_idx++)
+ {
+ if (pprune->subplan_map[pp_idx] >= 0 ||
+ pprune->subpart_map[pp_idx] >= 0)
+ pprune->present_parts =
+ bms_add_member(pprune->present_parts, pp_idx);
+ }
+ }
/*
* Only initial_context is initialized here. exec_context is
base-commit: f1b267e784135c74cfd4144902fd41ceb1cda0a4
--
2.53.0