agora inbox for pgsql-committers@postgresql.org  
help / color / mirror / Atom feed
pgsql: Fix preprocessing of PHV copies pushed down into subqueries
5+ messages / 1 participants
[nested] [flat]

* pgsql: Fix preprocessing of PHV copies pushed down into subqueries
@ 2026-09-19 12:58  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-19 12:58 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/8ea97ee348d4de725dc73df987f7dd28d755efe2

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



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

* pgsql: Fix preprocessing of PHV copies pushed down into subqueries
@ 2026-09-19 12:58  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-19 12:58 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
master

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

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



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

* pgsql: Fix preprocessing of PHV copies pushed down into subqueries
@ 2026-09-19 12:58  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-19 12:58 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/22a8f53397ee6d70d8da01bef6835b5843a90df8

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



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

* pgsql: Fix preprocessing of PHV copies pushed down into subqueries
@ 2026-09-19 12:58  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-19 12:58 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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



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

* pgsql: Fix preprocessing of PHV copies pushed down into subqueries
@ 2026-09-19 12:58  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-19 12:58 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/51f9e30f38c5f649c63b8ae1bbad9159937744d6

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



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


end of thread, other threads:[~2026-09-19 12:58 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-19 12:58 pgsql: Fix preprocessing of PHV copies pushed down into subqueries Richard Guo <rguo@postgresql.org>
2026-09-19 12:58 pgsql: Fix preprocessing of PHV copies pushed down into subqueries Richard Guo <rguo@postgresql.org>
2026-09-19 12:58 pgsql: Fix preprocessing of PHV copies pushed down into subqueries Richard Guo <rguo@postgresql.org>
2026-09-19 12:58 pgsql: Fix preprocessing of PHV copies pushed down into subqueries Richard Guo <rguo@postgresql.org>
2026-09-19 12:58 pgsql: Fix preprocessing of PHV copies pushed down into 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