Skip to content

Commit 51dfaa0

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 ad86d15 commit 51dfaa0

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
@@ -3251,16 +3251,6 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
32513251
jtnode = j->larg;
32523252
}
32533253
break;
3254-
case JOIN_RIGHT:
3255-
/* Mirror-image of the JOIN_LEFT case */
3256-
if ((varno = get_result_relid(root, j->larg)) != 0 &&
3257-
(j->quals == NULL ||
3258-
!find_dependent_phvs(root, varno)))
3259-
{
3260-
remove_result_refs(root, varno, j->rarg);
3261-
jtnode = j->rarg;
3262-
}
3263-
break;
32643254
case JOIN_SEMI:
32653255

32663256
/*
@@ -3269,14 +3259,17 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
32693259
* LHS, since we should either return the LHS row or not. For
32703260
* simplicity we inject the filter qual into a new FromExpr.
32713261
*
3272-
* Unlike the LEFT/RIGHT cases, we just Assert that there are
3273-
* no PHVs that need to be evaluated at the semijoin's RHS,
3274-
* since the rest of the query couldn't reference any outputs
3275-
* of the semijoin's RHS.
3262+
* There is a fine point about PHVs that are supposed to be
3263+
* evaluated at the RHS. Such PHVs could only appear in the
3264+
* semijoin's qual, since the rest of the query cannot
3265+
* reference any outputs of the semijoin's RHS. Therefore,
3266+
* they can't actually go to null before being examined, and
3267+
* it'd be OK to just remove the PHV wrapping. We don't have
3268+
* infrastructure for that, but remove_result_refs() will
3269+
* relabel them as to be evaluated at the LHS, which is fine.
32763270
*/
32773271
if ((varno = get_result_relid(root, j->rarg)) != 0)
32783272
{
3279-
Assert(!find_dependent_phvs(root, varno));
32803273
remove_result_refs(root, varno, j->larg);
32813274
if (j->quals)
32823275
jtnode = (Node *)
@@ -3290,6 +3283,7 @@ remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
32903283
/* We have no special smarts for these cases */
32913284
break;
32923285
default:
3286+
/* Note: JOIN_RIGHT should be gone at this point */
32933287
elog(ERROR, "unrecognized join type: %d",
32943288
(int) j->jointype);
32953289
break;

src/test/regress/expected/join.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3550,6 +3550,26 @@ where b;
35503550
0 | t | t
35513551
(2 rows)
35523552

3553+
-- Test PHV in a semijoin qual, which confused useless-RTE removal (bug #17700)
3554+
explain (verbose, costs off)
3555+
with ctetable as not materialized ( select 1 as f1 )
3556+
select * from ctetable c1
3557+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
3558+
QUERY PLAN
3559+
----------------------------
3560+
Result
3561+
Output: 1
3562+
One-Time Filter: (1 = 1)
3563+
(3 rows)
3564+
3565+
with ctetable as not materialized ( select 1 as f1 )
3566+
select * from ctetable c1
3567+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
3568+
f1
3569+
----
3570+
1
3571+
(1 row)
3572+
35533573
--
35543574
-- test inlining of immutable functions
35553575
--

src/test/regress/sql/join.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,16 @@ select * from
11561156
select a as b) as t3
11571157
where b;
11581158

1159+
-- Test PHV in a semijoin qual, which confused useless-RTE removal (bug #17700)
1160+
explain (verbose, costs off)
1161+
with ctetable as not materialized ( select 1 as f1 )
1162+
select * from ctetable c1
1163+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
1164+
1165+
with ctetable as not materialized ( select 1 as f1 )
1166+
select * from ctetable c1
1167+
where f1 in ( select c3.f1 from ctetable c2 full join ctetable c3 on true );
1168+
11591169
--
11601170
-- test inlining of immutable functions
11611171
--

0 commit comments

Comments
 (0)