pgsql: Fix relids of EC-derived join clauses for lateral UNION ALL memb

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix relids of EC-derived join clauses for lateral UNION ALL memb
Date: 2026-10-05 06:05:07
Message-ID: E1xDbox-00000000NBv-3cnd@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix relids of EC-derived join clauses for lateral UNION ALL members

When a LATERAL UNION ALL subquery is flattened into an appendrel, a
child EquivalenceMember's expression can contain lateral references to
other relations. create_join_clause() builds a clause from such a
member with make_restrictinfo(), which computes clause_relids from the
expression, and so includes the relids of the lateral references along
with those of any outer joins that null them. Commit 03107b4ed made
sure that clause_relids include the child's relids, but it only ever
added relids.

This is a problem when a lateral reference is nullable. The child is
parameterized by its lateral_relids, which do not include the outer
joins that null the lateral references, so the clause fails the
join_clause_is_movable_into() assertion that
get_baserel_parampathinfo() applies to EC-derived clauses.

To fix, set a child clause's clause_relids from its members' em_relids
rather than from their expressions. This matches how
adjust_appendrel_attrs() computes clause_relids for a child's other
join clauses. The lateral references need not be included, since
every path for the child is parameterized by at least its
lateral_relids. Also update num_base_rels to match, which commit
03107b4ed neglected to do.

Back-patch to v16, where outer-join relids were added to
clause_relids. Before that, the relids of a lateral reference are
always part of the child's parameterization, so the problem does not
arise.

Author: Richard Guo <guofenglinux(at)gmail(dot)com>
Reviewed-by: wenhui qiu <qiuwenhuifx(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAMbWs4-cRxpQNyQJW=PkDagSDGQ=3Rp2t_fSAyROKsDStjKHUQ@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/b83b7ddf7a64dce41624b5c0dabefd74dfe10ec0

Modified Files
--------------
src/backend/optimizer/path/equivclass.c | 32 +++++++-----
src/test/regress/expected/join.out | 92 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql | 26 ++++++++++
3 files changed, 138 insertions(+), 12 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Etsuro Fujita 2026-10-05 07:17:23 pgsql: postgres_fdw: Fix corner cases in transaction mode propagation.
Previous Message Peter Eisentraut 2026-10-05 05:50:56 pgsql: Don't take the address of flexible array members