ERROR: failed to apply nullingrels to a non-Var

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: ERROR: failed to apply nullingrels to a non-Var
Date: 2026-10-12 01:18:38
Message-ID: CAMbWs4_fzrrN9G6-wLwwrdM-n=fwj3Uhr1zkgj3o91EEC-CuEA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tender Wang 2026-10-12 01:22:15 Wrong results from self-join elimination with inheritance and ONLY
Previous Message Chao Li 2026-10-12 01:12:03 Re: Fix detection of truncated zstd-compressed backups