Skip to content

Commit 0671a71

Browse files
committed
Fix setrefs.c's failure to do expression processing on prune steps.
We should run the expression subtrees of PartitionedRelPruneInfo structs through fix_scan_expr. Failure to do so means that AlternativeSubPlans within those expressions won't be cleaned up properly, resulting in "unrecognized node type" errors since v14. It seems fairly likely that at least some of the other steps done by fix_scan_expr are important here as well, resulting in as-yet- undetected bugs. Therefore, I've chosen to back-patch this to all supported branches including v13, even though the known symptom doesn't manifest in v13. Per bug #18778 from Alexander Lakhin. Discussion: https://postgr.es/m/18778-24cd399df6c806af@postgresql.org
1 parent 149ed87 commit 0671a71

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/backend/optimizer/plan/setrefs.c

+12
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,12 @@ set_append_references(PlannerInfo *root,
17941794
PartitionedRelPruneInfo *pinfo = lfirst(l2);
17951795

17961796
pinfo->rtindex += rtoffset;
1797+
pinfo->initial_pruning_steps =
1798+
fix_scan_list(root, pinfo->initial_pruning_steps,
1799+
rtoffset, 1);
1800+
pinfo->exec_pruning_steps =
1801+
fix_scan_list(root, pinfo->exec_pruning_steps,
1802+
rtoffset, 1);
17971803
}
17981804
}
17991805
}
@@ -1870,6 +1876,12 @@ set_mergeappend_references(PlannerInfo *root,
18701876
PartitionedRelPruneInfo *pinfo = lfirst(l2);
18711877

18721878
pinfo->rtindex += rtoffset;
1879+
pinfo->initial_pruning_steps =
1880+
fix_scan_list(root, pinfo->initial_pruning_steps,
1881+
rtoffset, 1);
1882+
pinfo->exec_pruning_steps =
1883+
fix_scan_list(root, pinfo->exec_pruning_steps,
1884+
rtoffset, 1);
18731885
}
18741886
}
18751887
}

src/test/regress/expected/partition_prune.out

+29
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,35 @@ explain (costs off) select * from rparted_by_int2 where a > 100_000_000_000_000;
18751875
(2 rows)
18761876

18771877
drop table lp, coll_pruning, rlp, mc3p, mc2p, boolpart, iboolpart, boolrangep, rp, coll_pruning_multi, like_op_noprune, lparted_by_int2, rparted_by_int2;
1878+
-- check that AlternativeSubPlan within a pruning expression gets cleaned up
1879+
create table asptab (id int primary key) partition by range (id);
1880+
create table asptab0 partition of asptab for values from (0) to (1);
1881+
create table asptab1 partition of asptab for values from (1) to (2);
1882+
explain (costs off)
1883+
select * from
1884+
(select exists (select 1 from int4_tbl tinner where f1 = touter.f1) as b
1885+
from int4_tbl touter) ss,
1886+
asptab
1887+
where asptab.id > ss.b::int;
1888+
QUERY PLAN
1889+
--------------------------------------------------------------------
1890+
Nested Loop
1891+
-> Seq Scan on int4_tbl touter
1892+
-> Append
1893+
-> Index Only Scan using asptab0_pkey on asptab0 asptab_1
1894+
Index Cond: (id > (EXISTS(SubPlan 3))::integer)
1895+
SubPlan 4
1896+
-> Seq Scan on int4_tbl tinner_2
1897+
-> Index Only Scan using asptab1_pkey on asptab1 asptab_2
1898+
Index Cond: (id > (EXISTS(SubPlan 3))::integer)
1899+
SubPlan 3
1900+
-> Seq Scan on int4_tbl tinner_1
1901+
Filter: (f1 = touter.f1)
1902+
SubPlan 2
1903+
-> Seq Scan on int4_tbl tinner
1904+
(14 rows)
1905+
1906+
drop table asptab;
18781907
--
18791908
-- Test Partition pruning for HASH partitioning
18801909
--

src/test/regress/sql/partition_prune.sql

+15
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,21 @@ explain (costs off) select * from rparted_by_int2 where a > 100_000_000_000_000;
369369

370370
drop table lp, coll_pruning, rlp, mc3p, mc2p, boolpart, iboolpart, boolrangep, rp, coll_pruning_multi, like_op_noprune, lparted_by_int2, rparted_by_int2;
371371

372+
-- check that AlternativeSubPlan within a pruning expression gets cleaned up
373+
374+
create table asptab (id int primary key) partition by range (id);
375+
create table asptab0 partition of asptab for values from (0) to (1);
376+
create table asptab1 partition of asptab for values from (1) to (2);
377+
378+
explain (costs off)
379+
select * from
380+
(select exists (select 1 from int4_tbl tinner where f1 = touter.f1) as b
381+
from int4_tbl touter) ss,
382+
asptab
383+
where asptab.id > ss.b::int;
384+
385+
drop table asptab;
386+
372387
--
373388
-- Test Partition pruning for HASH partitioning
374389
--

0 commit comments

Comments
 (0)