postgres.git / summary / log / commit / refs

commit    cbb9086c9ef64b020cb9036f50afc14644bbf734
Author:   Amit Langote <amitlan@postgresql.org>
Date:     Tue Feb 25 00:21:17 2025 +0000
First released in: 18.0

    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


src/backend/executor/execPartition.c | 13 +++++--- src/backend/partitioning/partprune.c | 9 ++++- src/test/regress/expected/partition_prune.out | 48 +++++++++++++++++++++++++++ src/test/regress/sql/partition_prune.sql | 12 +++++++ 4 files changed, 77 insertions(+), 5 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]); } diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.c index ff926732f36..48a35f763e9 100644 --- a/src/backend/partitioning/partprune.c +++ b/src/backend/partitioning/partprune.c @@ -687,7 +687,14 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, if (subplanidx >= 0) { present_parts = bms_add_member(present_parts, i); - leafpart_rti_map[i] = (int) partrel->relid; + + /* + * Non-leaf partitions may appear here when they use an + * unflattened Append or MergeAppend. These should not be + * included in prunableRelids. + */ + if (partrel->nparts == -1) + leafpart_rti_map[i] = (int) partrel->relid; /* Record finding this subplan */ subplansfound = bms_add_member(subplansfound, subplanidx); diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.out index 1dbe6ff54fb..532597abe57 100644 --- a/src/test/regress/expected/partition_prune.out +++ b/src/test/regress/expected/partition_prune.out @@ -4590,5 +4590,53 @@ table part_abc_view; 2 | c | t (1 row) +-- A case with nested MergeAppend with its own PartitionPruneInfo. +create index on part_abc (a); +alter table part_abc add d int; +create table part_abc_3 partition of part_abc for values in (3, 4) partition by range (d); +create table part_abc_3_1 partition of part_abc_3 for values from (minvalue) to (1); +create table part_abc_3_2 partition of part_abc_3 for values from (1) to (100); +create table part_abc_3_3 partition of part_abc_3 for values from (100) to (maxvalue); +explain (costs off) +select min(a) over (partition by a order by a) from part_abc where a >= stable_one() + 1 and d <= stable_one() +union all +select min(a) over (partition by a order by a) from part_abc where a >= stable_one() + 1 and d >= stable_one(); + QUERY PLAN +---------------------------------------------------------------------------------------------- + Append + -> Subquery Scan on "*SELECT* 1_1" + -> WindowAgg + -> Append + Subplans Removed: 1 + -> Index Scan using part_abc_2_a_idx on part_abc_2 part_abc_1 + Index Cond: (a >= (stable_one() + 1)) + Filter: (d <= stable_one()) + -> Merge Append + Sort Key: part_abc_3.a + Subplans Removed: 1 + -> Index Scan using part_abc_3_1_a_idx on part_abc_3_1 part_abc_3 + Index Cond: (a >= (stable_one() + 1)) + Filter: (d <= stable_one()) + -> Index Scan using part_abc_3_2_a_idx on part_abc_3_2 part_abc_4 + Index Cond: (a >= (stable_one() + 1)) + Filter: (d <= stable_one()) + -> Subquery Scan on "*SELECT* 2" + -> WindowAgg + -> Append + Subplans Removed: 1 + -> Index Scan using part_abc_2_a_idx on part_abc_2 part_abc_6 + Index Cond: (a >= (stable_one() + 1)) + Filter: (d >= stable_one()) + -> Merge Append + Sort Key: a + Subplans Removed: 1 + -> Index Scan using part_abc_3_2_a_idx on part_abc_3_2 part_abc_8 + Index Cond: (a >= (stable_one() + 1)) + Filter: (d >= stable_one()) + -> Index Scan using part_abc_3_3_a_idx on part_abc_3_3 part_abc_9 + Index Cond: (a >= (stable_one() + 1)) + Filter: (d >= stable_one()) +(33 rows) + drop view part_abc_view; drop table part_abc; diff --git a/src/test/regress/sql/partition_prune.sql b/src/test/regress/sql/partition_prune.sql index 6aad02156c6..5f36d589b6b 100644 --- a/src/test/regress/sql/partition_prune.sql +++ b/src/test/regress/sql/partition_prune.sql @@ -1397,5 +1397,17 @@ using (select stable_one() + 2 as pid) as q join part_abc_1 pt1 on (q.pid = pt1. when matched then delete returning pt.a; table part_abc_view; +-- A case with nested MergeAppend with its own PartitionPruneInfo. +create index on part_abc (a); +alter table part_abc add d int; +create table part_abc_3 partition of part_abc for values in (3, 4) partition by range (d); +create table part_abc_3_1 partition of part_abc_3 for values from (minvalue) to (1); +create table part_abc_3_2 partition of part_abc_3 for values from (1) to (100); +create table part_abc_3_3 partition of part_abc_3 for values from (100) to (maxvalue); +explain (costs off) +select min(a) over (partition by a order by a) from part_abc where a >= stable_one() + 1 and d <= stable_one() +union all +select min(a) over (partition by a order by a) from part_abc where a >= stable_one() + 1 and d >= stable_one(); + drop view part_abc_view; drop table part_abc; [parent: 48796a98d5ae]