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: v1-0001-Fix-bogus-Assert-in-find_var_for_subquery_tle.patch
Description: Binary data

Reply via email to