ERROR: failed to apply nullingrels to a non-Var

Поиск
Список
Период
Сортировка
Искать

ERROR: failed to apply nullingrels to a non-Var

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
I ran into $subject while fixing a bug caused by lateral PHVs.

create table t (a int);

select 1 from t t1
  left join (select 1 as x union all select 2) s on true
  left join lateral (select s.x as y from t t2) ss on true
  left join t t3 on ss.y is null;
ERROR:  failed to apply nullingrels to a non-Var

Commit 767c59895 made set_append_rel_size() skip join quals that came
from above an outer join that can null the parent rel, by checking
their clause_relids.  But that doesn't catch a nulled reference to the
parent rel that appears only within a PHV, since pull_varnos() only
reports where the PHV is evaluated.  Here ss.y is a PHV containing
s.x, so the t3 join qual is not skipped.

It seems to me that we should check required_relids instead here.

-         if (!bms_overlap(rinfo->clause_relids, rel->nulling_relids))
+         if (!bms_overlap(rinfo->required_relids, rel->nulling_relids))

Or, maybe an alternative is to skip quals whose clause_relids overlap
the rel's lateral_referencers, as join_clause_is_movable_to() does,
but checking required_relids seems simpler to me.

Thoughts?

- Richard


Re: ERROR: failed to apply nullingrels to a non-Var

От:
wenhui qiu <qiuwenhuifx@gmail.com>
Дата:
HI Richard
 

Thanks for reporting this and tracking it down to lateral PHVs.

Between the two approaches you mentioned, I'm personally more inclined towards your second thought (checking lateral_referencers).

My slight concern with switching to required_relids is that required_relids and nulling_relids serve slightly different purposes conceptually. While checking required_relids happens to catch the issue here, I wonder if that might be an indirect effect of how PHV's ph_eval_at tracks outer joins. For instance, if s were involved in an inner join instead of a left join, rel->nulling_relids would be empty, but we probably still wouldn't want to push down a qual that references s's lateral referencers into the child relations.

In that sense, checking rel->lateral_referencers seems to capture the root of the problem more naturally: the qual references something (ss) that topologically depends on s, so it shouldn't be pushed down to s's child relations in the first place. That also aligns very well with the reasoning already used in join_clause_is_movable_to().

So perhaps something like this would be a bit more robust:

diff
- if (!bms_overlap(rinfo->clause_relids, rel->nulling_relids))
+ if (!bms_overlap(rinfo->clause_relids, rel->nulling_relids) &&
+ !bms_overlap(rinfo->clause_relids, rel->lateral_referencers))

What do you think?



Regards 

Re: ERROR: failed to apply nullingrels to a non-Var

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Mon, Oct 12, 2026 at 10:18 AM Richard Guo  wrote:
> It seems to me that we should check required_relids instead here.
>
> -         if (!bms_overlap(rinfo->clause_relids, rel->nulling_relids))
> +         if (!bms_overlap(rinfo->required_relids, rel->nulling_relids))
>
> Or, maybe an alternative is to skip quals whose clause_relids overlap
> the rel's lateral_referencers, as join_clause_is_movable_to() does,
> but checking required_relids seems simpler to me.

After a million times of back and forth, I've decided to go with the
lateral_referencers fix, as it is easier to prove correct.  Any
reference to the parent rel hidden inside a PHV evaluated elsewhere is
a lateral reference, so such quals always have one of the parent rel's
lateral referencers in their clause_relids.  Skipping them is
harmless.  Enforcing one at a scan of the parent rel would require
parameterizing the scan by a rel that itself depends on the parent
rel, which is circular.

The required_relids check turned out to be insufficient.
make_outerjoininfo() can't see PHV-hidden references either, so
required_relids may lack the nulling outer join.  (And I think this is
another latent bug that we need to fix too.)

I've pushed the fix and back-patched it to v16.

- Richard


FAQ