Assert failure in get_baserel_parampathinfo with lateral UNION ALL

Started by Richard Guo2 months ago4 messageshackers
Jump to latest
#1Richard Guo
guofenglinux@gmail.com

The following query trips the Assert(join_clause_is_movable_into(...))
in get_baserel_parampathinfo():

create table t (a int, b int);

select * from t t1 left join t t2 on t1.a = t2.a,
lateral (select 1 as x union all select t1.a + t2.b) s
where t1.b = s.x;

What happens is that after the UNION ALL subquery is flattened, the
child EC member for the second branch is the expression "t1.a + t2.b",
where t2.b is nulled by the left join. create_join_clause() builds
the child clause "t1.b = t1.a + t2.b" with make_restrictinfo(), so its
clause_relids are computed from the expression: t1, t2 and the outer
join, plus the child's relid added by commit 03107b4ed. But the child
is parameterized by its lateral_relids, which list only baserels, so
the outer join's relid is never available and the Assert fails.

I think we should set a child clause's clause_relids to the union of
its members' em_relids instead. The lateral references need not be
represented there, since every path for the child is parameterized by
at least its lateral_relids. It is also what a child's non-EC join
clauses already get: for "t1.b < s.x", adjust_appendrel_attrs()
translates the parent clause's relids {t1, s} to {t1, child} rather
than recomputing them from the translated expression, so the lateral
references do not appear there either. For child clauses without
lateral references the resulting relids are the same as before.

Thoughts?

- Richard

Attachments:

t253875_1
v1-0001-Fix-relids-of-EC-derived-join-clauses-for-lateral.patchapplication/octet-stream; name=v1-0001-Fix-relids-of-EC-derived-join-clauses-for-lateral.patchDownload+128-13
#2wenhui qiu
qiuwenhuifx@gmail.com
In reply to: Richard Guo (#1)
Re: Assert failure in get_baserel_parampathinfo with lateral UNION ALL

HI Richard

I think the change to derive `clause_relids` from the child EMs makes

sense, especially for the lateral-reference case described in the patch.
One thing I may be missing, though: after replacing
`rinfo->clause_relids`, should we also update `rinfo->num_base_rels`?
`make_restrictinfo()` computes `num_base_rels` from the original
`clause_relids`, but this patch subsequently replaces `clause_relids` for
child clauses.
In cases involving lateral references and nulling outer joins, the two
sets can be different, so `num_base_rels` could potentially become stale.
Would it be safer to recompute it from the new `clause_relids` here, using
the same definition as in `make_restrictinfo()`?
I am not sure whether there is an invariant elsewhere that makes
`num_base_rels` unaffected in these cases. I
t might also be useful to add a regression case specifically covering this
metadata, in addition to the existing plan/execution coverage.

Thanks

#3Richard Guo
guofenglinux@gmail.com
In reply to: wenhui qiu (#2)
Re: Assert failure in get_baserel_parampathinfo with lateral UNION ALL

On Mon, Sep 21, 2026 at 3:30 PM wenhui qiu <qiuwenhuifx@gmail.com> wrote:

One thing I may be missing, though: after replacing `rinfo->clause_relids`, should we also update `rinfo->num_base_rels`?

Good point. I think we should do that. And it seems that commit
03107b4ed was sloppy about updating num_base_rels. On master the
problematic RestrictInfo has stale num_base_rels:

:num_base_rels 2
:clause_relids (b 1 2 3 8)

I'm a little surprised that that hasn't caused problems so far.

- Richard

#4Richard Guo
guofenglinux@gmail.com
In reply to: Richard Guo (#3)
Re: Assert failure in get_baserel_parampathinfo with lateral UNION ALL

On Wed, Sep 30, 2026 at 11:47 AM Richard Guo <guofenglinux@gmail.com> wrote:

Good point. I think we should do that. And it seems that commit
03107b4ed was sloppy about updating num_base_rels. On master the
problematic RestrictInfo has stale num_base_rels:

I updated the patch to also update num_base_rels, then pushed and
back-patched it to v16. Thanks for the review.

- Richard