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: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar
Date: Mon, 19 Oct 2026 02:07:40 +0000
Message-ID: <E1xIcmq-00000002CL5-2oqE@gemulon.postgresql.org> (raw)

Don't remove an RTE_RESULT needed by a nested PlaceHolderVar

When remove_useless_result_rtes() removes an RTE_RESULT, any
PlaceHolderVars that were to be evaluated there must be evaluated
somewhere else.  If the RTE_RESULT is the nullable side of an outer
join, there is no other place that would preserve the PHVs' nulling
behavior, so find_dependent_phvs() refuses the removal if any such PHV
exists anywhere in the query.  If the RTE_RESULT is a child of an
inner join, its PHVs can instead be evaluated at the join of all its
sibling rels, unless a sibling needs them before that join is formed.
find_dependent_phvs_in_jointree() checks for that by looking for such
PHVs in the join quals and LATERAL RTEs of the siblings.

That misses a PHV that is evaluated at some but not all of the sibling
rels and contains one of the RTE_RESULT's PHVs.  Such a PHV can appear
anywhere in the query, such as in the targetlist.  This can happen
when pulled-up expressions are wrapped in PHVs because of outer joins
or grouping sets.  Once the RTE_RESULT is removed, the outer PHV needs
a value that is only available at a join above its own evaluation
level.  The outer PHV's rel thus becomes laterally dependent on the
other sibling rels.  If one of those rels laterally references it in
turn, we end up with a rel that is laterally dependent on itself,
causing an assertion failure in create_lateral_join_info().

To fix, also search the whole query for PHVs that are evaluated at
some but not all of the sibling rels, and refuse to remove the
RTE_RESULT if any of them contains one of its PHVs.  An outer PHV that
is evaluated at all of the sibling rels is not a problem, since the
moved PHV is available there, so we don't block removal in that common
case.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_hLOsW8_SePp_3EmYTLX8Oc4zEZEvypyPRszpoMhDdGQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_14_STABLE

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

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 66 +++++++++++++++++++++++-
src/test/regress/expected/join.out        | 83 +++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql             | 34 +++++++++++++
3 files changed, 181 insertions(+), 2 deletions(-)



view thread (7+ messages)

Message-ID: <E1xIcmq-00000002CL5-2oqE@gemulon.postgresql.org>
Permalink:  ../E1xIcmq-00000002CL5-2oqE@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xIcmq-00000002CL5-2oqE@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: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar
  In-Reply-To: <E1xIcmq-00000002CL5-2oqE@gemulon.postgresql.org>
  References: <E1xIcmq-00000002CL5-2oqE@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