mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
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
This commit is contained in:
@ -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]);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user