Skip to content

Commit c97a547

Browse files
committed
Partially restore qual scope checks in distribute_qual_to_rels().
The LATERAL implementation is now basically complete, and I still don't see a cost-effective way to make an exact qual scope cross-check in the presence of LATERAL. However, I did add a PlannerInfo.hasLateralRTEs flag along the way, so it's easy to make the check only when not hasLateralRTEs. That seems to still be useful, and it beats having no check at all.
1 parent da3df99 commit c97a547

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/backend/optimizer/plan/initsplan.c

+13-15
Original file line numberDiff line numberDiff line change
@@ -1103,21 +1103,20 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
11031103
relids = pull_varnos(clause);
11041104

11051105
/*
1106-
* Cross-check: clause should contain no relids not within its scope.
1107-
* Otherwise the parser messed up.
1106+
* Normally relids is a subset of qualscope, and we like to check that
1107+
* here as a crosscheck on the parser and rewriter. That need not be the
1108+
* case when there are LATERAL RTEs, however: the clause could contain
1109+
* references to rels outside its syntactic scope as a consequence of
1110+
* pull-up of such references from a LATERAL subquery below it. So, only
1111+
* check if the query contains no LATERAL RTEs.
11081112
*
1109-
* XXX temporarily disable the qualscope cross-check, which tends to
1110-
* reject quals pulled up from LATERAL subqueries. This is only in the
1111-
* nature of a debugging crosscheck anyway. I'm loath to remove it
1112-
* permanently, but need to think a bit harder about how to replace it.
1113-
* See also disabled Assert below. (The ojscope test is still okay
1114-
* because we prevent pullup of LATERAL subqueries that might cause it to
1115-
* be violated.)
1113+
* However, if it's an outer-join clause, we always insist that relids be
1114+
* a subset of ojscope. This is safe because is_simple_subquery()
1115+
* disallows pullup of LATERAL subqueries that could cause the restriction
1116+
* to be violated.
11161117
*/
1117-
#ifdef NOT_USED
1118-
if (!bms_is_subset(relids, qualscope))
1118+
if (!root->hasLateralRTEs && !bms_is_subset(relids, qualscope))
11191119
elog(ERROR, "JOIN qualification cannot refer to other relations");
1120-
#endif
11211120
if (ojscope && !bms_is_subset(relids, ojscope))
11221121
elog(ERROR, "JOIN qualification cannot refer to other relations");
11231122

@@ -1272,9 +1271,8 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
12721271
if (outerjoin_delayed)
12731272
{
12741273
/* Should still be a subset of current scope ... */
1275-
#ifdef NOT_USED /* XXX temporarily disabled for LATERAL */
1276-
Assert(bms_is_subset(relids, qualscope));
1277-
#endif
1274+
Assert(root->hasLateralRTEs || bms_is_subset(relids, qualscope));
1275+
Assert(ojscope == NULL || bms_is_subset(relids, ojscope));
12781276

12791277
/*
12801278
* Because application of the qual will be delayed by outer join,

0 commit comments

Comments
 (0)