Assert failure in find_var_for_subquery_tle

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

Assert failure in find_var_for_subquery_tle

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
Вложения:
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

Re: Assert failure in find_var_for_subquery_tle

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi Richard,

The fix looks right to me. The new test trips the Assert without the
pathkeys.c change.

A CTE scan as the member hits the same Assert, example:

set_cte_pathlist().
explain (costs off)
with c as materialized (select q2 from int8_tbl order by q2)
select ss.v from int8_tbl x,
  lateral (select x.q1 from c union all
           select q2 from int8_tbl) ss(v);

The patch fixes this one too. Maybe the commit message could mention
it as well.

Thanks,
Shihao



Re: Assert failure in find_var_for_subquery_tle

От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Thu, Oct 8, 2026 at 4:42 PM Richard Guo  wrote:
> 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.

Pushed and back-patched to all supported branches.

- Richard


FAQ