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 lateral references in sort keys added by createplan.c
Date: Wed, 21 Oct 2026 09:07:01 +0000
Message-ID: <E1xJSHl-00000000Ag8-03ih@gemulon.postgresql.org> (raw)

Fix lateral references in sort keys added by createplan.c

When prepare_sort_from_pathkeys() cannot find a pathkey's expression
in the input plan's targetlist, it adds a resjunk entry computing an
EquivalenceClass member's expression.  It copied that expression
as-is, without replacing lateral references with nestloop Params the
way build_path_tlist() does for the rest of the targetlist.

This matters when the sort key contains a PlaceHolderVar whose
expression has a lateral reference, and the sort is placed directly
atop the plan node that evaluates the PlaceHolderVar.  The targetlist
already contains the PlaceHolderVar, so the sort key is considered
computable, but the entry added for it still has the outer relation's
Var inside the PlaceHolderVar.  If the node is a join, setrefs.c fails
with "variable not found in subplan target lists".  If it is a scan,
setrefs.c leaves the Var in place, and the executor fetches it from
the scan tuple, leading to wrong results.

To fix, pass root to prepare_sort_from_pathkeys() and apply
replace_nestloop_params() to the expression before adding it to the
targetlist.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49LWNm7_OxuW1V71wLi+Ev3KbfY1bMY36uydNKwSPnbtQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_18_STABLE

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

Modified Files
--------------
src/backend/optimizer/plan/createplan.c | 58 +++++++++++++---------
src/test/regress/expected/join.out      | 87 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql           | 34 +++++++++++++
3 files changed, 156 insertions(+), 23 deletions(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1xJSHl-00000000Ag8-03ih@gemulon.postgresql.org>
Permalink:  ../E1xJSHl-00000000Ag8-03ih@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xJSHl-00000000Ag8-03ih@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 lateral references in sort keys added by createplan.c
  In-Reply-To: <E1xJSHl-00000000Ag8-03ih@gemulon.postgresql.org>
  References: <E1xJSHl-00000000Ag8-03ih@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