Assert failure in find_var_for_subquery_tle

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Assert failure in find_var_for_subquery_tle
Date: 2026-10-08 07:42:35
Message-ID: CAMbWs48fFiPi7Q5yJCoM_nAs47zd5XgvPHd9nZyM_ewq+k_jpQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

The following query, run in the regression database, trips an Assert:

select ss.v from int8_tbl x,
lateral (select x.q1 from (select distinct q2 from int8_tbl) s
union all
select q2 from int8_tbl) ss(v);

TRAP: failed Assert("var->varno == rel->relid"), File: "pathkeys.c"

In set_append_rel_size(), we have this comment:

* NB: the resulting childrel->reltarget->exprs may contain arbitrary
* expressions, which otherwise would not occur in a rel's targetlist.
* Code that might be looking at an appendrel child must cope with
* such. (Normally, a rel's targetlist would only include Vars and
* PlaceHolderVars.)

Apparently find_var_for_subquery_tle() did not get this memo, and
assumes that every Var in a subquery rel's targetlist belongs to that
rel.

To fix, I think we can just ignore Vars that do not belong to the
subquery rel. Such a Var cannot be a reference to the subquery's
output, so it should never be treated as a match.

Any thoughts?

- Richard

Attachment Content-Type Size
v1-0001-Fix-bogus-Assert-in-find_var_for_subquery_tle.patch application/octet-stream 4.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2026-10-08 07:51:28 Re: pgstat: allow a stats kind to use its own dedicated dsa/dshash
Previous Message zengxx 2026-10-08 07:25:13 Re: Skip a redundant singleton GROUP BY node