pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix relids of EC-derived join clauses for lateral UNION ALL memb
Date: Mon, 05 Oct 2026 06:05:07 +0000
Message-ID: <E1xDbox-00000000NBr-3SOW@gemulon.postgresql.org> (raw)

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@gmail.com>
Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4-cRxpQNyQJW=PkDagSDGQ=3Rp2t_fSAyROKsDStjKHUQ@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/46875a4c7464be5513bf8891b7971f5534241870

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(-)



view thread (5+ messages)  latest in thread

Message-ID: <E1xDbox-00000000NBr-3SOW@gemulon.postgresql.org>
Permalink:  ../E1xDbox-00000000NBr-3SOW@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xDbox-00000000NBr-3SOW@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: rguo@postgresql.org
  Subject: Re: pgsql: Fix relids of EC-derived join clauses for lateral UNION ALL memb
  In-Reply-To: <E1xDbox-00000000NBr-3SOW@gemulon.postgresql.org>
  References: <E1xDbox-00000000NBr-3SOW@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox