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 bogus Assert in find_var_for_subquery_tle
Date: Fri, 16 Oct 2026 07:49:03 +0000
Message-ID: <E1xHcgZ-00000001qIp-3ubV@gemulon.postgresql.org> (raw)

Fix bogus Assert in find_var_for_subquery_tle

find_var_for_subquery_tle() searches a subquery rel's targetlist for a
Var referencing a given subquery output column, and asserted that any
Var it finds there belongs to that rel.  That does not hold for an
appendrel child.  set_append_rel_size() builds the child's targetlist
by translating the parent's, and as noted there the result can contain
arbitrary expressions.  In particular, when a member of a LATERAL UNION
ALL subquery is pulled up and its output is a lateral reference, the
translated expression is a Var of the laterally referenced rel.  If the
child is itself a subquery with sorted paths, convert_subquery_pathkeys()
would trip the Assert.

In non-assert builds, the function went on to compare such a Var's
varattno with the subquery column's resno, and so could match a Var of
an unrelated rel.

To fix, ignore Vars that do not belong to the subquery rel, the same
way set_rel_width() already does.  Also correct the adjacent comment,
which claimed that the only non-Var entries are placeholders.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: shihao zhong <zhong950419@gmail.com>
Discussion: https://postgr.es/m/CAMbWs48fFiPi7Q5yJCoM_nAs47zd5XgvPHd9nZyM_ewq+k_jpQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_16_STABLE

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

Modified Files
--------------
src/backend/optimizer/path/pathkeys.c | 12 ++++++++++--
src/test/regress/expected/join.out    | 18 ++++++++++++++++++
src/test/regress/sql/join.sql         |  7 +++++++
3 files changed, 35 insertions(+), 2 deletions(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1xHcgZ-00000001qIp-3ubV@gemulon.postgresql.org>
Permalink:  ../E1xHcgZ-00000001qIp-3ubV@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xHcgZ-00000001qIp-3ubV@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 bogus Assert in find_var_for_subquery_tle
  In-Reply-To: <E1xHcgZ-00000001qIp-3ubV@gemulon.postgresql.org>
  References: <E1xHcgZ-00000001qIp-3ubV@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