Skip to content

Commit 556c0b9

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 b5d8fd4 commit 556c0b9

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
@@ -3273,16 +3273,6 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
32733273
jtnode = j->larg;
32743274
}
32753275
break;
3276-
case JOIN_RIGHT:
3277-
/* Mirror-image of the JOIN_LEFT case */
3278-
if ((varno = get_result_relid(root, j->larg)) != 0 &&
3279-
(j->quals == NULL ||
3280-
!find_dependent_phvs(root, varno)))
3281-
{
3282-
remove_result_refs(root, varno, j->rarg);
3283-
jtnode = j->rarg;
3284-
}
3285-
break;
32863276
case JOIN_SEMI:
32873277

32883278
/*
@@ -3291,14 +3281,17 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
32913281
* LHS, since we should either return the LHS row or not. For
32923282
* simplicity we inject the filter qual into a new FromExpr.
32933283
*
3294-
* Unlike the LEFT/RIGHT cases, we just Assert that there are
3295-
* no PHVs that need to be evaluated at the semijoin's RHS,
3296-
* since the rest of the query couldn't reference any outputs
3297-
* of the semijoin's RHS.
3284+
* There is a fine point about PHVs that are supposed to be
3285+
* evaluated at the RHS. Such PHVs could only appear in the
3286+
* semijoin's qual, since the rest of the query cannot
3287+
* reference any outputs of the semijoin's RHS. Therefore,
3288+
* they can't actually go to null before being examined, and
3289+
* it'd be OK to just remove the PHV wrapping. We don't have
3290+
* infrastructure for that, but remove_result_refs() will
3291+
* relabel them as to be evaluated at the LHS, which is fine.
32983292
*/
32993293
if ((varno = get_result_relid(root, j->rarg)) != 0)
33003294
{
3301-
Assert(!find_dependent_phvs(root, varno));
33023295
remove_result_refs(root, varno, j->larg);
33033296
if (j->quals)
33043297
jtnode = (Node *)
@@ -3312,6 +3305,7 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
33123305
/* We have no special smarts for these cases */
33133306
break;
33143307
default:
3308+
/* Note: JOIN_RIGHT should be gone at this point */
33153309
elog(ERROR, "unrecognized join type: %d",
33163310
(int) j->jointype);
33173311
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)