Skip to content

Commit 1eb9221

Browse files
committed
Fix executor prune failure when plan already pruned
In a multi-layer partitioning setup, if at plan time all the sub-partitions are pruned but the intermediate one remains, the executor later throws a spurious error that there's nothing to prune. That is correct, but there's no reason to throw an error. Therefore, don't. Reported-by: Andreas Seltenreich <[email protected]> Author: David Rowley <[email protected]> Discussion: https://postgr.es/m/[email protected]
1 parent fa73b37 commit 1eb9221

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/backend/executor/execPartition.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -1886,8 +1886,13 @@ find_matching_subplans_recurse(PartitionPruningData *prunedata,
18861886
initial_prune, validsubplans);
18871887
else
18881888
{
1889-
/* Shouldn't happen */
1890-
elog(ERROR, "partition missing from subplans");
1889+
/*
1890+
* We get here if the planner already pruned all the sub-
1891+
* partitions for this partition. Silently ignore this
1892+
* partition in this case. The end result is the same: we
1893+
* would have pruned all partitions just the same, but we
1894+
* don't have any pruning steps to execute to verify this.
1895+
*/
18911896
}
18921897
}
18931898
}

src/test/regress/expected/partition_prune.out

+18
Original file line numberDiff line numberDiff line change
@@ -3570,3 +3570,21 @@ execute q (1, 1);
35703570

35713571
reset plan_cache_mode;
35723572
drop table p, q;
3573+
-- Ensure run-time pruning works correctly when we match a partitioned table
3574+
-- on the first level but find no matching partitions on the second level.
3575+
create table listp (a int, b int) partition by list (a);
3576+
create table listp1 partition of listp for values in(1);
3577+
create table listp2 partition of listp for values in(2) partition by list(b);
3578+
create table listp2_10 partition of listp2 for values in (10);
3579+
explain (analyze, costs off, summary off, timing off)
3580+
select * from listp where a = (select 2) and b <> 10;
3581+
QUERY PLAN
3582+
-------------------------------------------
3583+
Append (actual rows=0 loops=1)
3584+
InitPlan 1 (returns $0)
3585+
-> Result (actual rows=1 loops=1)
3586+
-> Seq Scan on listp1 (never executed)
3587+
Filter: ((b <> 10) AND (a = $0))
3588+
(5 rows)
3589+
3590+
drop table listp;

src/test/regress/sql/partition_prune.sql

+12
Original file line numberDiff line numberDiff line change
@@ -946,3 +946,15 @@ execute q (1, 1);
946946

947947
reset plan_cache_mode;
948948
drop table p, q;
949+
950+
-- Ensure run-time pruning works correctly when we match a partitioned table
951+
-- on the first level but find no matching partitions on the second level.
952+
create table listp (a int, b int) partition by list (a);
953+
create table listp1 partition of listp for values in(1);
954+
create table listp2 partition of listp for values in(2) partition by list(b);
955+
create table listp2_10 partition of listp2 for values in (10);
956+
957+
explain (analyze, costs off, summary off, timing off)
958+
select * from listp where a = (select 2) and b <> 10;
959+
960+
drop table listp;

0 commit comments

Comments
 (0)