Skip to content

Commit 0224646

Browse files
committed
Remove bogus Assert and dead code in remove_useless_results_recurse().
The JOIN_SEMI case Assert'ed that there are no PlaceHolderVars that need to be evaluated at the semijoin's RHS, which is wrong because there could be some in the semijoin's qual condition. However, there could not be any references further up than that, and within the qual there is not any way that such a PHV could have gone to null yet, so we don't really need the PHV and there is no need to avoid making the RHS-removal optimization. The upshot is that there's no actual bug in production code, and we ought to just remove this misguided Assert. While we're here, also drop the JOIN_RIGHT case, which is dead code because reduce_outer_joins() already got rid of JOIN_RIGHT. Per bug #17700 from Xin Wen. Uselessness of the JOIN_RIGHT case pointed out by Richard Guo. Back-patch to v12 where this code was added. Discussion: https://postgr.es/m/17700-2b5c10d917c30687@postgresql.org
1 parent f3f70b8 commit 0224646

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

src/backend/optimizer/prep/prepjointree.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3175,16 +3175,6 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
31753175
jtnode = j->larg;
31763176
}
31773177
break;
3178-
case JOIN_RIGHT:
3179-
/* Mirror-image of the JOIN_LEFT case */
3180-
if ((varno = get_result_relid(root, j->larg)) != 0 &&
3181-
(j->quals == NULL ||
3182-
!find_dependent_phvs(root, varno)))
3183-
{
3184-
remove_result_refs(root, varno, j->rarg);
3185-
jtnode = j->rarg;
3186-
}
3187-
break;
31883178
case JOIN_SEMI:
31893179

31903180
/*
@@ -3193,14 +3183,17 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
31933183
* LHS, since we should either return the LHS row or not. For
31943184
* simplicity we inject the filter qual into a new FromExpr.
31953185
*
3196-
* Unlike the LEFT/RIGHT cases, we just Assert that there are
3197-
* no PHVs that need to be evaluated at the semijoin's RHS,
3198-
* since the rest of the query couldn't reference any outputs
3199-
* of the semijoin's RHS.
3186+
* There is a fine point about PHVs that are supposed to be
3187+
* evaluated at the RHS. Such PHVs could only appear in the
3188+
* semijoin's qual, since the rest of the query cannot
3189+
* reference any outputs of the semijoin's RHS. Therefore,
3190+
* they can't actually go to null before being examined, and
3191+
* it'd be OK to just remove the PHV wrapping. We don't have
3192+
* infrastructure for that, but remove_result_refs() will
3193+
* relabel them as to be evaluated at the LHS, which is fine.
32003194
*/
32013195
if ((varno = get_result_relid(root, j->rarg)) != 0)
32023196
{
3203-
Assert(!find_dependent_phvs(root, varno));
32043197
remove_result_refs(root, varno, j->larg);
32053198
if (j->quals)
32063199
jtnode = (Node *)
@@ -3214,6 +3207,7 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
32143207
/* We have no special smarts for these cases */
32153208
break;
32163209
default:
3210+
/* Note: JOIN_RIGHT should be gone at this point */
32173211
elog(ERROR, "unrecognized join type: %d",
32183212
(int) j->jointype);
32193213
break;

src/test/regress/expected/join.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3456,6 +3456,26 @@ where b;
34563456
0 | t | t
34573457
(2 rows)
34583458

3459+
-- Test PHV in a semijoin qual, which confused useless-RTE removal (bug #17700)
3460+
explain (verbose, costs off)
3461+
with ctetable as not materialized ( select 1 as f1 )
3462+
select * from ctetable c1
3463+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
3464+
QUERY PLAN
3465+
----------------------------
3466+
Result
3467+
Output: 1
3468+
One-Time Filter: (1 = 1)
3469+
(3 rows)
3470+
3471+
with ctetable as not materialized ( select 1 as f1 )
3472+
select * from ctetable c1
3473+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
3474+
f1
3475+
----
3476+
1
3477+
(1 row)
3478+
34593479
--
34603480
-- test inlining of immutable functions
34613481
--

src/test/regress/sql/join.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,16 @@ select * from
11131113
select a as b) as t3
11141114
where b;
11151115

1116+
-- Test PHV in a semijoin qual, which confused useless-RTE removal (bug #17700)
1117+
explain (verbose, costs off)
1118+
with ctetable as not materialized ( select 1 as f1 )
1119+
select * from ctetable c1
1120+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
1121+
1122+
with ctetable as not materialized ( select 1 as f1 )
1123+
select * from ctetable c1
1124+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
1125+
11161126
--
11171127
-- test inlining of immutable functions
11181128
--

0 commit comments

Comments
 (0)