agora inbox for pgsql-committers@postgresql.org  
help / color / mirror / Atom feed
From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix preprocessing of PHV copies pushed down into subqueries
Date: Sat, 19 Sep 2026 12:58:31 +0000
Message-ID: <E1x7ueF-00000000P2X-12W5@gemulon.postgresql.org> (raw)

Fix preprocessing of PHV copies pushed down into subqueries

Commit e28cf96e9 preprocessed the copies of a query level's
PlaceHolderVars that were pushed down into its subqueries in a single
walk at the start of subquery_planner.  That was wrong in two ways.

First, such copies can also be inserted later, when a join alias Var
of the outer level is expanded within a LATERAL subquery or a
SubLink's subselect and the alias expression contains a PHV.  Those
copies were never preprocessed, so a SubLink within them survived into
the subquery's lateral references or into a SubPlan's argument list,
tripping an assertion in identify_current_nestloop_params or failing
in cost_qual_eval.

Second, when a pushed-down copy directly contains another PHV of the
same level, the walk preprocessed the inner one first and then the
outer one, so the outer one's preprocessing ran into the SubPlans just
created in the inner one and tripped an assertion in
flatten_join_alias_vars.

To fix, preprocess the copies right after join alias expansion
instead: within preprocess_expression, just before SubLinks are turned
into SubPlans, and for LATERAL subquery RTEs, right after their
aliases are flattened.  Preprocessing a copy handles everything within
it, including any further copies nested inside its SubLinks, so the
walker no longer looks inside a copy.

Back-patch to v16, as with commit e28cf96e9.

Reported-by: Fujii Masao <masao.fujii@gmail.com>
Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAHGQGwH-acRnAk1=Ki4EiAJEt67+Rz=_jyg7pbP_c6BK4oHZ4Q@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/83bd1c1baddc37ed1eb5b26d6dea3ee275c1bdd2

Modified Files
--------------
src/backend/optimizer/plan/planner.c   |  52 ++++++-----
src/backend/optimizer/plan/subselect.c |   7 +-
src/backend/optimizer/util/var.c       |   9 +-
src/test/regress/expected/join.out     | 154 ++++++++++++++++++++++++++++++---
src/test/regress/sql/join.sql          |  35 ++++++++
5 files changed, 216 insertions(+), 41 deletions(-)



view thread (5+ messages)  latest in thread

Message-ID: <E1x7ueF-00000000P2X-12W5@gemulon.postgresql.org>
Permalink:  ../E1x7ueF-00000000P2X-12W5@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1x7ueF-00000000P2X-12W5@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 using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: rguo@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Fix preprocessing of PHV copies pushed down into subqueries
  In-Reply-To: <E1x7ueF-00000000P2X-12W5@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 agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox