agora inbox for pgsql-committers@postgresql.org  
help / color / mirror / Atom feed
pgsql: Fix stale copies of PHVs in subqueries
5+ messages / 1 participants
[nested] [flat]

* pgsql: Fix stale copies of PHVs in subqueries
@ 2026-09-14 03:31  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-14 03:31 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix stale copies of PHVs in subqueries

When a subquery references an output of another subquery that gets
pulled up, and that output must be wrapped in a PlaceHolderVar because
of an intermediate outer join, the PHV expression is pushed down into
the subquery.  That copy is not preprocessed along with the outer
query's expressions, so the two copies can diverge.  This used to be
harmless, but since commit 2ebf25e7d join removal edits the whole
query tree, walking into subqueries, and can trip an assert in
ChangeVarNodes if it removes a rel whose Var survives only in such a
copy.

To fix, preprocess these copies at their owning query level, early in
subquery_planner, before anything can consume them (in particular
before SubLinks are turned into SubPlans).  This covers copies pushed
into both LATERAL subquery RTEs and SubLink subselects, and handles
nested copies innermost-first.  extract_lateral_references no longer
preprocesses the copies it pulls out.

Correspondingly, the subquery's own processing must leave the contents
of an upper-level PHV alone, since the owning level has already
preprocessed them: eval_const_expressions returns such a PHV
unchanged, and flatten_join_alias_vars no longer recurses into it.

Back-patch to v16, as with commit 2ebf25e7d.

Reported-by: Tender Wang <tndrwang@gmail.com>
Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAHewXN=kWGAXV537mKtSyBYobGdHhYJVDJJMXXZEmmPWE_zaPw@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/2d48d0f153dc86c71a1ab80c5252ab35bb1d84b7

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c     |  15 +--
src/backend/optimizer/plan/planner.c       |  96 ++++++++++++++---
src/backend/optimizer/plan/subselect.c     |  13 +--
src/backend/optimizer/util/clauses.c       |  35 ++++---
src/backend/optimizer/util/paramassign.c   |  23 ++---
src/backend/optimizer/util/var.c           |  33 +++++-
src/include/optimizer/planner.h            |   1 -
src/test/regress/expected/groupingsets.out |   4 +-
src/test/regress/expected/join.out         | 160 +++++++++++++++++++++++++++--
src/test/regress/sql/join.sql              |  47 +++++++++
src/tools/pgindent/typedefs.list           |   1 +
11 files changed, 360 insertions(+), 68 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix stale copies of PHVs in subqueries
@ 2026-09-14 03:31  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-14 03:31 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix stale copies of PHVs in subqueries

When a subquery references an output of another subquery that gets
pulled up, and that output must be wrapped in a PlaceHolderVar because
of an intermediate outer join, the PHV expression is pushed down into
the subquery.  That copy is not preprocessed along with the outer
query's expressions, so the two copies can diverge.  This used to be
harmless, but since commit 2ebf25e7d join removal edits the whole
query tree, walking into subqueries, and can trip an assert in
ChangeVarNodes if it removes a rel whose Var survives only in such a
copy.

To fix, preprocess these copies at their owning query level, early in
subquery_planner, before anything can consume them (in particular
before SubLinks are turned into SubPlans).  This covers copies pushed
into both LATERAL subquery RTEs and SubLink subselects, and handles
nested copies innermost-first.  extract_lateral_references no longer
preprocesses the copies it pulls out.

Correspondingly, the subquery's own processing must leave the contents
of an upper-level PHV alone, since the owning level has already
preprocessed them: eval_const_expressions returns such a PHV
unchanged, and flatten_join_alias_vars no longer recurses into it.

Back-patch to v16, as with commit 2ebf25e7d.

Reported-by: Tender Wang <tndrwang@gmail.com>
Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAHewXN=kWGAXV537mKtSyBYobGdHhYJVDJJMXXZEmmPWE_zaPw@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/37b6e2657c09666c3b804bbc43d0e6a0dfc321dd

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c   |  15 +--
src/backend/optimizer/plan/planner.c     |  89 ++++++++++++++++++
src/backend/optimizer/plan/subselect.c   |  13 +--
src/backend/optimizer/util/clauses.c     |  35 ++++---
src/backend/optimizer/util/paramassign.c |  23 +++--
src/backend/optimizer/util/var.c         |  33 ++++++-
src/test/regress/expected/join.out       | 157 +++++++++++++++++++++++++++++--
src/test/regress/sql/join.sql            |  47 +++++++++
src/tools/pgindent/typedefs.list         |   1 +
9 files changed, 360 insertions(+), 53 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix stale copies of PHVs in subqueries
@ 2026-09-14 03:31  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-14 03:31 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix stale copies of PHVs in subqueries

When a subquery references an output of another subquery that gets
pulled up, and that output must be wrapped in a PlaceHolderVar because
of an intermediate outer join, the PHV expression is pushed down into
the subquery.  That copy is not preprocessed along with the outer
query's expressions, so the two copies can diverge.  This used to be
harmless, but since commit 2ebf25e7d join removal edits the whole
query tree, walking into subqueries, and can trip an assert in
ChangeVarNodes if it removes a rel whose Var survives only in such a
copy.

To fix, preprocess these copies at their owning query level, early in
subquery_planner, before anything can consume them (in particular
before SubLinks are turned into SubPlans).  This covers copies pushed
into both LATERAL subquery RTEs and SubLink subselects, and handles
nested copies innermost-first.  extract_lateral_references no longer
preprocesses the copies it pulls out.

Correspondingly, the subquery's own processing must leave the contents
of an upper-level PHV alone, since the owning level has already
preprocessed them: eval_const_expressions returns such a PHV
unchanged, and flatten_join_alias_vars no longer recurses into it.

Back-patch to v16, as with commit 2ebf25e7d.

Reported-by: Tender Wang <tndrwang@gmail.com>
Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAHewXN=kWGAXV537mKtSyBYobGdHhYJVDJJMXXZEmmPWE_zaPw@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_17_STABLE

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

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c |  15 ++--
src/backend/optimizer/plan/planner.c   |  90 ++++++++++++++++++++
src/backend/optimizer/plan/subselect.c |  12 +--
src/backend/optimizer/util/clauses.c   |  35 ++++----
src/backend/optimizer/util/var.c       |  33 ++++++--
src/test/regress/expected/join.out     | 145 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql          |  47 +++++++++++
src/tools/pgindent/typedefs.list       |   1 +
8 files changed, 344 insertions(+), 34 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix stale copies of PHVs in subqueries
@ 2026-09-14 03:31  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-14 03:31 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix stale copies of PHVs in subqueries

When a subquery references an output of another subquery that gets
pulled up, and that output must be wrapped in a PlaceHolderVar because
of an intermediate outer join, the PHV expression is pushed down into
the subquery.  That copy is not preprocessed along with the outer
query's expressions, so the two copies can diverge.  This used to be
harmless, but since commit 2ebf25e7d join removal edits the whole
query tree, walking into subqueries, and can trip an assert in
ChangeVarNodes if it removes a rel whose Var survives only in such a
copy.

To fix, preprocess these copies at their owning query level, early in
subquery_planner, before anything can consume them (in particular
before SubLinks are turned into SubPlans).  This covers copies pushed
into both LATERAL subquery RTEs and SubLink subselects, and handles
nested copies innermost-first.  extract_lateral_references no longer
preprocesses the copies it pulls out.

Correspondingly, the subquery's own processing must leave the contents
of an upper-level PHV alone, since the owning level has already
preprocessed them: eval_const_expressions returns such a PHV
unchanged, and flatten_join_alias_vars no longer recurses into it.

Back-patch to v16, as with commit 2ebf25e7d.

Reported-by: Tender Wang <tndrwang@gmail.com>
Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAHewXN=kWGAXV537mKtSyBYobGdHhYJVDJJMXXZEmmPWE_zaPw@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/54ac06f31b75f8c28be6444a7e32d523d6652874

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c     |  15 +--
src/backend/optimizer/plan/planner.c       |  89 ++++++++++++++++++
src/backend/optimizer/plan/subselect.c     |  12 ++-
src/backend/optimizer/util/clauses.c       |  35 ++++---
src/backend/optimizer/util/var.c           |  33 ++++++-
src/test/regress/expected/groupingsets.out |   2 +-
src/test/regress/expected/join.out         | 145 +++++++++++++++++++++++++++++
src/test/regress/sql/join.sql              |  47 ++++++++++
src/tools/pgindent/typedefs.list           |   1 +
9 files changed, 344 insertions(+), 35 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix stale copies of PHVs in subqueries
@ 2026-09-14 03:31  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-14 03:31 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix stale copies of PHVs in subqueries

When a subquery references an output of another subquery that gets
pulled up, and that output must be wrapped in a PlaceHolderVar because
of an intermediate outer join, the PHV expression is pushed down into
the subquery.  That copy is not preprocessed along with the outer
query's expressions, so the two copies can diverge.  This used to be
harmless, but since commit 2ebf25e7d join removal edits the whole
query tree, walking into subqueries, and can trip an assert in
ChangeVarNodes if it removes a rel whose Var survives only in such a
copy.

To fix, preprocess these copies at their owning query level, early in
subquery_planner, before anything can consume them (in particular
before SubLinks are turned into SubPlans).  This covers copies pushed
into both LATERAL subquery RTEs and SubLink subselects, and handles
nested copies innermost-first.  extract_lateral_references no longer
preprocesses the copies it pulls out.

Correspondingly, the subquery's own processing must leave the contents
of an upper-level PHV alone, since the owning level has already
preprocessed them: eval_const_expressions returns such a PHV
unchanged, and flatten_join_alias_vars no longer recurses into it.

Back-patch to v16, as with commit 2ebf25e7d.

Reported-by: Tender Wang <tndrwang@gmail.com>
Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAHewXN=kWGAXV537mKtSyBYobGdHhYJVDJJMXXZEmmPWE_zaPw@mail.gmail.com
Backpatch-through: 16

Branch
------
master

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

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c     |  15 +--
src/backend/optimizer/plan/planner.c       |  96 ++++++++++++++---
src/backend/optimizer/plan/subselect.c     |  13 +--
src/backend/optimizer/util/clauses.c       |  35 ++++---
src/backend/optimizer/util/paramassign.c   |  23 ++---
src/backend/optimizer/util/var.c           |  33 +++++-
src/include/optimizer/planner.h            |   1 -
src/test/regress/expected/groupingsets.out |   4 +-
src/test/regress/expected/join.out         | 160 +++++++++++++++++++++++++++--
src/test/regress/sql/join.sql              |  47 +++++++++
src/tools/pgindent/typedefs.list           |   1 +
11 files changed, 360 insertions(+), 68 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2026-09-14 03:31 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-14 03:31 pgsql: Fix stale copies of PHVs in subqueries Richard Guo <rguo@postgresql.org>
2026-09-14 03:31 pgsql: Fix stale copies of PHVs in subqueries Richard Guo <rguo@postgresql.org>
2026-09-14 03:31 pgsql: Fix stale copies of PHVs in subqueries Richard Guo <rguo@postgresql.org>
2026-09-14 03:31 pgsql: Fix stale copies of PHVs in subqueries Richard Guo <rguo@postgresql.org>
2026-09-14 03:31 pgsql: Fix stale copies of PHVs in subqueries Richard Guo <rguo@postgresql.org>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox