pgsql: Fix bogus Assert in find_var_for_subquery_tle

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix bogus Assert in find_var_for_subquery_tle
Date: 2026-10-16 07:49:04
Message-ID: E1xHcga-00000001qIu-016I@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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(at)gmail(dot)com>
Reviewed-by: shihao zhong <zhong950419(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAMbWs48fFiPi7Q5yJCoM_nAs47zd5XgvPHd9nZyM_ewq+k_jpQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_14_STABLE

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

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

Browse pgsql-committers by date

  From Date Subject
Next Message David Rowley 2026-10-16 08:07:30 pgsql: Adjust weird usages of sentinel_ok()
Previous Message Richard Guo 2026-10-16 07:49:03 pgsql: Fix bogus Assert in find_var_for_subquery_tle