Skip to content

Commit bb8d48c

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 85565cb commit bb8d48c

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
@@ -3007,16 +3007,6 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
30073007
jtnode = j->larg;
30083008
}
30093009
break;
3010-
case JOIN_RIGHT:
3011-
/* Mirror-image of the JOIN_LEFT case */
3012-
if ((varno = get_result_relid(root, j->larg)) != 0 &&
3013-
(j->quals == NULL ||
3014-
!find_dependent_phvs(root, varno)))
3015-
{
3016-
remove_result_refs(root, varno, j->rarg);
3017-
jtnode = j->rarg;
3018-
}
3019-
break;
30203010
case JOIN_SEMI:
30213011

30223012
/*
@@ -3025,14 +3015,17 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
30253015
* LHS, since we should either return the LHS row or not. For
30263016
* simplicity we inject the filter qual into a new FromExpr.
30273017
*
3028-
* Unlike the LEFT/RIGHT cases, we just Assert that there are
3029-
* no PHVs that need to be evaluated at the semijoin's RHS,
3030-
* since the rest of the query couldn't reference any outputs
3031-
* of the semijoin's RHS.
3018+
* There is a fine point about PHVs that are supposed to be
3019+
* evaluated at the RHS. Such PHVs could only appear in the
3020+
* semijoin's qual, since the rest of the query cannot
3021+
* reference any outputs of the semijoin's RHS. Therefore,
3022+
* they can't actually go to null before being examined, and
3023+
* it'd be OK to just remove the PHV wrapping. We don't have
3024+
* infrastructure for that, but remove_result_refs() will
3025+
* relabel them as to be evaluated at the LHS, which is fine.
30323026
*/
30333027
if ((varno = get_result_relid(root, j->rarg)) != 0)
30343028
{
3035-
Assert(!find_dependent_phvs(root, varno));
30363029
remove_result_refs(root, varno, j->larg);
30373030
if (j->quals)
30383031
jtnode = (Node *)
@@ -3046,6 +3039,7 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
30463039
/* We have no special smarts for these cases */
30473040
break;
30483041
default:
3042+
/* Note: JOIN_RIGHT should be gone at this point */
30493043
elog(ERROR, "unrecognized join type: %d",
30503044
(int) j->jointype);
30513045
break;

src/test/regress/expected/join.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3374,6 +3374,26 @@ where b;
33743374
0 | t | t
33753375
(2 rows)
33763376

3377+
-- Test PHV in a semijoin qual, which confused useless-RTE removal (bug #17700)
3378+
explain (verbose, costs off)
3379+
with ctetable as not materialized ( select 1 as f1 )
3380+
select * from ctetable c1
3381+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
3382+
QUERY PLAN
3383+
----------------------------
3384+
Result
3385+
Output: 1
3386+
One-Time Filter: (1 = 1)
3387+
(3 rows)
3388+
3389+
with ctetable as not materialized ( select 1 as f1 )
3390+
select * from ctetable c1
3391+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
3392+
f1
3393+
----
3394+
1
3395+
(1 row)
3396+
33773397
--
33783398
-- test extraction of restriction OR clauses from join OR clause
33793399
-- (we used to only do this for indexable clauses)

src/test/regress/sql/join.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,16 @@ select * from
10801080
select a as b) as t3
10811081
where b;
10821082

1083+
-- Test PHV in a semijoin qual, which confused useless-RTE removal (bug #17700)
1084+
explain (verbose, costs off)
1085+
with ctetable as not materialized ( select 1 as f1 )
1086+
select * from ctetable c1
1087+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
1088+
1089+
with ctetable as not materialized ( select 1 as f1 )
1090+
select * from ctetable c1
1091+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
1092+
10831093
--
10841094
-- test extraction of restriction OR clauses from join OR clause
10851095
-- (we used to only do this for indexable clauses)

0 commit comments

Comments
 (0)