Skip to content

Commit 798c017

Browse files
committed
remove_rel_from_query() must clean up PlaceHolderVar.phrels fields.
While we got away with this sloppiness before, it's not okay now that fee7b77 caused build_joinrel_tlist() to make use of phrels. Per report from Robins Tharakan. Richard Guo (some cosmetic tweaks by me) Discussion: https://postgr.es/m/CAMbWs4_ngw9sKxpTE8hqk=-ooVX_CQP3DarA4HzkRMz_JKpTrA@mail.gmail.com
1 parent b7e84c6 commit 798c017

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

src/backend/optimizer/plan/analyzejoins.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,8 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
316316
* Remove the target relid from the planner's data structures, having
317317
* determined that there is no need to include it in the query.
318318
*
319-
* We are not terribly thorough here. We must make sure that the rel is
320-
* no longer treated as a baserel, and that attributes of other baserels
321-
* are no longer marked as being needed at joins involving this rel.
322-
* Also, join quals involving the rel have to be removed from the joininfo
323-
* lists, but only if they belong to the outer join identified by ojrelid
324-
* and joinrelids.
319+
* We are not terribly thorough here. We only bother to update parts of
320+
* the planner's data structures that will actually be consulted later.
325321
*/
326322
static void
327323
remove_rel_from_query(PlannerInfo *root, int relid, int ojrelid,
@@ -429,11 +425,18 @@ remove_rel_from_query(PlannerInfo *root, int relid, int ojrelid,
429425
}
430426
else
431427
{
428+
PlaceHolderVar *phv = phinfo->ph_var;
429+
432430
phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, relid);
433431
phinfo->ph_eval_at = bms_del_member(phinfo->ph_eval_at, ojrelid);
434-
Assert(!bms_is_empty(phinfo->ph_eval_at));
432+
Assert(!bms_is_empty(phinfo->ph_eval_at)); /* checked previously */
435433
phinfo->ph_needed = bms_del_member(phinfo->ph_needed, relid);
436434
phinfo->ph_needed = bms_del_member(phinfo->ph_needed, ojrelid);
435+
/* ph_needed might or might not become empty */
436+
phv->phrels = bms_del_member(phv->phrels, relid);
437+
phv->phrels = bms_del_member(phv->phrels, ojrelid);
438+
Assert(!bms_is_empty(phv->phrels));
439+
Assert(phv->phnullingrels == NULL); /* no need to adjust */
437440
}
438441
}
439442

src/test/regress/expected/join.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5052,6 +5052,22 @@ from int4_tbl as t1
50525052
One-Time Filter: false
50535053
(5 rows)
50545054

5055+
-- per further discussion of bug #17781
5056+
explain (costs off)
5057+
select ss1.x
5058+
from (select f1/2 as x from int4_tbl i4 left join a on a.id = i4.f1) ss1
5059+
right join int8_tbl i8 on true
5060+
where current_user is not null; -- this is to add a Result node
5061+
QUERY PLAN
5062+
-----------------------------------------------
5063+
Result
5064+
One-Time Filter: (CURRENT_USER IS NOT NULL)
5065+
-> Nested Loop Left Join
5066+
-> Seq Scan on int8_tbl i8
5067+
-> Materialize
5068+
-> Seq Scan on int4_tbl i4
5069+
(6 rows)
5070+
50555071
-- check that join removal works for a left join when joining a subquery
50565072
-- that is guaranteed to be unique by its GROUP BY clause
50575073
explain (costs off)

src/test/regress/sql/join.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,13 @@ from int4_tbl as t1
18131813
on t5.q1 = t7.q2)
18141814
on false;
18151815

1816+
-- per further discussion of bug #17781
1817+
explain (costs off)
1818+
select ss1.x
1819+
from (select f1/2 as x from int4_tbl i4 left join a on a.id = i4.f1) ss1
1820+
right join int8_tbl i8 on true
1821+
where current_user is not null; -- this is to add a Result node
1822+
18161823
-- check that join removal works for a left join when joining a subquery
18171824
-- that is guaranteed to be unique by its GROUP BY clause
18181825
explain (costs off)

0 commit comments

Comments
 (0)