aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor/execPartition.c
diff options
context:
space:
mode:
authorAmit Langote <amitlan@postgresql.org>2025-02-25 09:21:17 +0900
committerAmit Langote <amitlan@postgresql.org>2025-02-25 09:24:42 +0900
commitcbb9086c9ef64b020cb9036f50afc14644bbf734 (patch)
tree215c47330d68db022a29bfab45279a14f3292ae3 /src/backend/executor/execPartition.c
parent48796a98d5aed68e349118d4e05ac26b76c765b0 (diff)
downloadpostgresql-cbb9086c9ef64b020cb9036f50afc14644bbf734.tar.gz
postgresql-cbb9086c9ef64b020cb9036f50afc14644bbf734.zip
Fix bug in cbc127917 to handle nested Append correctly
A non-leaf partition with a subplan that is an Append node was omitted from PlannedStmt.unprunableRelids because it was mistakenly included in PlannerGlobal.prunableRelids due to the way PartitionedRelPruneInfo.leafpart_rti_map[] is constructed. This happened when a non-leaf partition used an unflattened Append or MergeAppend. As a result, ExecGetRangeTableRelation() reported an error when called from CreatePartitionPruneState() to process the partition's own PartitionPruneInfo, since it was treated as prunable when it should not have been. Reported-by: Alexander Lakhin <exclusion@gmail.com> (via sqlsmith) Diagnosed-by: Tender Wang <tndrwang@gmail.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Discussion: https://postgr.es/m/74839af6-aadc-4f60-ae77-ae65f94bf607@gmail.com
Diffstat (limited to 'src/backend/executor/execPartition.c')
-rw-r--r--src/backend/executor/execPartition.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index 432eeaf9034..5cd5e2eeb80 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -2589,9 +2589,9 @@ ExecFindMatchingSubPlans(PartitionPruneState *prunestate,
* find_matching_subplans_recurse
* Recursive worker function for ExecFindMatchingSubPlans
*
- * Adds valid (non-prunable) subplan IDs to *validsubplans and the RT indexes
- * of their corresponding leaf partitions to *validsubplan_rtis if
- * it's non-NULL.
+ * Adds valid (non-prunable) subplan IDs to *validsubplans. If
+ * *validsubplan_rtis is non-NULL, it also adds the RT indexes of their
+ * corresponding partitions, but only if they are leaf partitions.
*/
static void
find_matching_subplans_recurse(PartitionPruningData *prunedata,
@@ -2628,7 +2628,12 @@ find_matching_subplans_recurse(PartitionPruningData *prunedata,
{
*validsubplans = bms_add_member(*validsubplans,
pprune->subplan_map[i]);
- if (validsubplan_rtis)
+
+ /*
+ * Only report leaf partitions. Non-leaf partitions may appear
+ * here when they use an unflattened Append or MergeAppend.
+ */
+ if (validsubplan_rtis && pprune->leafpart_rti_map[i])
*validsubplan_rtis = bms_add_member(*validsubplan_rtis,
pprune->leafpart_rti_map[i]);
}