Skip to content

Commit de5edc6

Browse files
committed
Fix old oversight in join removal logic.
Commit 9e7e29c introduced an Assert that join removal didn't reduce the eval_at set of any PlaceHolderVar to empty. At first glance it looks like join_is_removable ensures that's true --- but actually, the loop in join_is_removable skips PlaceHolderVars that are not referenced above the join due to be removed. So, if we don't want any empty eval_at sets, the right thing to do is to delete any now-unreferenced PlaceHolderVars from the data structure entirely. Per fuzz testing by Andreas Seltenreich. Back-patch to 9.3 where the aforesaid Assert was added.
1 parent 0d49135 commit de5edc6

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/backend/optimizer/plan/analyzejoins.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,16 +382,24 @@ remove_rel_from_query(PlannerInfo *root, int relid, Relids joinrelids)
382382
}
383383

384384
/*
385-
* Likewise remove references from PlaceHolderVar data structures.
385+
* Likewise remove references from PlaceHolderVar data structures,
386+
* removing any no-longer-needed placeholders entirely.
386387
*/
387-
foreach(l, root->placeholder_list)
388+
for (l = list_head(root->placeholder_list); l != NULL; l = nextl)
388389
{
389390
PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
390391

391-
phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, relid);
392-
Assert(!bms_is_empty(phinfo->ph_eval_at));
393-
Assert(!bms_is_member(relid, phinfo->ph_lateral));
394-
phinfo->ph_needed = bms_del_member(phinfo->ph_needed, relid);
392+
nextl = lnext(l);
393+
if (bms_is_subset(phinfo->ph_needed, joinrelids))
394+
root->placeholder_list = list_delete_ptr(root->placeholder_list,
395+
phinfo);
396+
else
397+
{
398+
phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, relid);
399+
Assert(!bms_is_empty(phinfo->ph_eval_at));
400+
Assert(!bms_is_member(relid, phinfo->ph_lateral));
401+
phinfo->ph_needed = bms_del_member(phinfo->ph_needed, relid);
402+
}
395403
}
396404

397405
/*

src/test/regress/expected/join.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3669,6 +3669,22 @@ SELECT * FROM
36693669
1 | 4567890123456789 | -4567890123456789 | 4567890123456789
36703670
(5 rows)
36713671

3672+
rollback;
3673+
-- another join removal bug: we must clean up correctly when removing a PHV
3674+
begin;
3675+
create temp table uniquetbl (f1 text unique);
3676+
explain (costs off)
3677+
select t1.* from
3678+
uniquetbl as t1
3679+
left join (select *, '***'::text as d1 from uniquetbl) t2
3680+
on t1.f1 = t2.f1
3681+
left join uniquetbl t3
3682+
on t2.d1 = t3.f1;
3683+
QUERY PLAN
3684+
--------------------------
3685+
Seq Scan on uniquetbl t1
3686+
(1 row)
3687+
36723688
rollback;
36733689
-- bug #8444: we've historically allowed duplicate aliases within aliased JOINs
36743690
select * from

src/test/regress/sql/join.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,21 @@ SELECT * FROM
11801180

11811181
rollback;
11821182

1183+
-- another join removal bug: we must clean up correctly when removing a PHV
1184+
begin;
1185+
1186+
create temp table uniquetbl (f1 text unique);
1187+
1188+
explain (costs off)
1189+
select t1.* from
1190+
uniquetbl as t1
1191+
left join (select *, '***'::text as d1 from uniquetbl) t2
1192+
on t1.f1 = t2.f1
1193+
left join uniquetbl t3
1194+
on t2.d1 = t3.f1;
1195+
1196+
rollback;
1197+
11831198
-- bug #8444: we've historically allowed duplicate aliases within aliased JOINs
11841199

11851200
select * from

0 commit comments

Comments
 (0)