Fix bug in cbc127917 to handle nested Append correctly
authorAmit Langote <[email protected]>
Tue, 25 Feb 2025 00:21:17 +0000 (09:21 +0900)
committerAmit Langote <[email protected]>
Tue, 25 Feb 2025 00:24:42 +0000 (09:24 +0900)
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 <[email protected]> (via sqlsmith)
Diagnosed-by: Tender Wang <[email protected]>
Reviewed-by: Tender Wang <[email protected]>
Discussion: https://postgr.es/m/74839af6-aadc-4f60-ae77-ae65f94bf607@gmail.com

src/backend/executor/execPartition.c
src/backend/partitioning/partprune.c
src/test/regress/expected/partition_prune.out
src/test/regress/sql/partition_prune.sql

index 432eeaf90346871c50aaf5bb7831c86dd403ca78..5cd5e2eeb80b3f53d568e42f0457425d0397d768 100644 (file)
@@ -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]);
        }
index ff926732f367eb6836320282b9c7059fca452e04..48a35f763e906c802f51c5db89903893f74761dc 100644 (file)
@@ -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);
index 1dbe6ff54fbeabcfaa409ab7c9e030d0dbe97d11..532597abe57786ffcf08d8ec55e95182c9e3d6a9 100644 (file)
@@ -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;
index 6aad02156c6190655b5cd38cf7a1db2c95d74cc4..5f36d589b6b5656ad22cf74e2d42e1f55855658f 100644 (file)
@@ -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;