Skip to content

Commit bf826ea

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 f7a8fc1 commit bf826ea

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
@@ -1795,6 +1795,12 @@ set_append_references(PlannerInfo *root,
17951795
PartitionedRelPruneInfo *pinfo = lfirst(l2);
17961796

17971797
pinfo->rtindex += rtoffset;
1798+
pinfo->initial_pruning_steps =
1799+
fix_scan_list(root, pinfo->initial_pruning_steps,
1800+
rtoffset, 1);
1801+
pinfo->exec_pruning_steps =
1802+
fix_scan_list(root, pinfo->exec_pruning_steps,
1803+
rtoffset, 1);
17981804
}
17991805
}
18001806
}
@@ -1871,6 +1877,12 @@ set_mergeappend_references(PlannerInfo *root,
18711877
PartitionedRelPruneInfo *pinfo = lfirst(l2);
18721878

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

src/test/regress/expected/partition_prune.out

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

18951895
drop table lp, coll_pruning, rlp, mc3p, mc2p, boolpart, iboolpart, boolrangep, rp, coll_pruning_multi, like_op_noprune, lparted_by_int2, rparted_by_int2;
1896+
-- check that AlternativeSubPlan within a pruning expression gets cleaned up
1897+
create table asptab (id int primary key) partition by range (id);
1898+
create table asptab0 partition of asptab for values from (0) to (1);
1899+
create table asptab1 partition of asptab for values from (1) to (2);
1900+
explain (costs off)
1901+
select * from
1902+
(select exists (select 1 from int4_tbl tinner where f1 = touter.f1) as b
1903+
from int4_tbl touter) ss,
1904+
asptab
1905+
where asptab.id > ss.b::int;
1906+
QUERY PLAN
1907+
--------------------------------------------------------------------
1908+
Nested Loop
1909+
-> Seq Scan on int4_tbl touter
1910+
-> Append
1911+
-> Index Only Scan using asptab0_pkey on asptab0 asptab_1
1912+
Index Cond: (id > (EXISTS(SubPlan 3))::integer)
1913+
SubPlan 4
1914+
-> Seq Scan on int4_tbl tinner_2
1915+
-> Index Only Scan using asptab1_pkey on asptab1 asptab_2
1916+
Index Cond: (id > (EXISTS(SubPlan 3))::integer)
1917+
SubPlan 3
1918+
-> Seq Scan on int4_tbl tinner_1
1919+
Filter: (f1 = touter.f1)
1920+
SubPlan 2
1921+
-> Seq Scan on int4_tbl tinner
1922+
(14 rows)
1923+
1924+
drop table asptab;
18961925
--
18971926
-- Test Partition pruning for HASH partitioning
18981927
--

src/test/regress/sql/partition_prune.sql

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

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

391+
-- check that AlternativeSubPlan within a pruning expression gets cleaned up
392+
393+
create table asptab (id int primary key) partition by range (id);
394+
create table asptab0 partition of asptab for values from (0) to (1);
395+
create table asptab1 partition of asptab for values from (1) to (2);
396+
397+
explain (costs off)
398+
select * from
399+
(select exists (select 1 from int4_tbl tinner where f1 = touter.f1) as b
400+
from int4_tbl touter) ss,
401+
asptab
402+
where asptab.id > ss.b::int;
403+
404+
drop table asptab;
405+
391406
--
392407
-- Test Partition pruning for HASH partitioning
393408
--

0 commit comments

Comments
 (0)