pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar
7+ messages / 1 participants
[nested] [flat]

* pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar
@ 2026-10-19 02:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-10-19 02:07 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't remove an RTE_RESULT needed by a nested PlaceHolderVar

When remove_useless_result_rtes() removes an RTE_RESULT, any
PlaceHolderVars that were to be evaluated there must be evaluated
somewhere else.  If the RTE_RESULT is the nullable side of an outer
join, there is no other place that would preserve the PHVs' nulling
behavior, so find_dependent_phvs() refuses the removal if any such PHV
exists anywhere in the query.  If the RTE_RESULT is a child of an
inner join, its PHVs can instead be evaluated at the join of all its
sibling rels, unless a sibling needs them before that join is formed.
find_dependent_phvs_in_jointree() checks for that by looking for such
PHVs in the join quals and LATERAL RTEs of the siblings.

That misses a PHV that is evaluated at some but not all of the sibling
rels and contains one of the RTE_RESULT's PHVs.  Such a PHV can appear
anywhere in the query, such as in the targetlist.  This can happen
when pulled-up expressions are wrapped in PHVs because of outer joins
or grouping sets.  Once the RTE_RESULT is removed, the outer PHV needs
a value that is only available at a join above its own evaluation
level.  The outer PHV's rel thus becomes laterally dependent on the
other sibling rels.  If one of those rels laterally references it in
turn, we end up with a rel that is laterally dependent on itself,
causing an assertion failure in create_lateral_join_info().

To fix, also search the whole query for PHVs that are evaluated at
some but not all of the sibling rels, and refuse to remove the
RTE_RESULT if any of them contains one of its PHVs.  An outer PHV that
is evaluated at all of the sibling rels is not a problem, since the
moved PHV is available there, so we don't block removal in that common
case.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_hLOsW8_SePp_3EmYTLX8Oc4zEZEvypyPRszpoMhDdGQ@mail.gmail.com
Backpatch-through: 14

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/3138f248cd3de43dd0f96760fc666152ea8a5c37

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 69 ++++++++++++++++++++++++-
src/test/regress/expected/join.out        | 83 +++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql             | 34 +++++++++++++
3 files changed, 184 insertions(+), 2 deletions(-)



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

* pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar
@ 2026-10-19 02:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-10-19 02:07 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't remove an RTE_RESULT needed by a nested PlaceHolderVar

When remove_useless_result_rtes() removes an RTE_RESULT, any
PlaceHolderVars that were to be evaluated there must be evaluated
somewhere else.  If the RTE_RESULT is the nullable side of an outer
join, there is no other place that would preserve the PHVs' nulling
behavior, so find_dependent_phvs() refuses the removal if any such PHV
exists anywhere in the query.  If the RTE_RESULT is a child of an
inner join, its PHVs can instead be evaluated at the join of all its
sibling rels, unless a sibling needs them before that join is formed.
find_dependent_phvs_in_jointree() checks for that by looking for such
PHVs in the join quals and LATERAL RTEs of the siblings.

That misses a PHV that is evaluated at some but not all of the sibling
rels and contains one of the RTE_RESULT's PHVs.  Such a PHV can appear
anywhere in the query, such as in the targetlist.  This can happen
when pulled-up expressions are wrapped in PHVs because of outer joins
or grouping sets.  Once the RTE_RESULT is removed, the outer PHV needs
a value that is only available at a join above its own evaluation
level.  The outer PHV's rel thus becomes laterally dependent on the
other sibling rels.  If one of those rels laterally references it in
turn, we end up with a rel that is laterally dependent on itself,
causing an assertion failure in create_lateral_join_info().

To fix, also search the whole query for PHVs that are evaluated at
some but not all of the sibling rels, and refuse to remove the
RTE_RESULT if any of them contains one of its PHVs.  An outer PHV that
is evaluated at all of the sibling rels is not a problem, since the
moved PHV is available there, so we don't block removal in that common
case.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_hLOsW8_SePp_3EmYTLX8Oc4zEZEvypyPRszpoMhDdGQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/0b534f9b34c6fcd8d254d859305af557a615abd3

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 69 ++++++++++++++++++++++++-
src/test/regress/expected/join.out        | 83 +++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql             | 34 +++++++++++++
3 files changed, 184 insertions(+), 2 deletions(-)



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

* pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar
@ 2026-10-19 02:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-10-19 02:07 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't remove an RTE_RESULT needed by a nested PlaceHolderVar

When remove_useless_result_rtes() removes an RTE_RESULT, any
PlaceHolderVars that were to be evaluated there must be evaluated
somewhere else.  If the RTE_RESULT is the nullable side of an outer
join, there is no other place that would preserve the PHVs' nulling
behavior, so find_dependent_phvs() refuses the removal if any such PHV
exists anywhere in the query.  If the RTE_RESULT is a child of an
inner join, its PHVs can instead be evaluated at the join of all its
sibling rels, unless a sibling needs them before that join is formed.
find_dependent_phvs_in_jointree() checks for that by looking for such
PHVs in the join quals and LATERAL RTEs of the siblings.

That misses a PHV that is evaluated at some but not all of the sibling
rels and contains one of the RTE_RESULT's PHVs.  Such a PHV can appear
anywhere in the query, such as in the targetlist.  This can happen
when pulled-up expressions are wrapped in PHVs because of outer joins
or grouping sets.  Once the RTE_RESULT is removed, the outer PHV needs
a value that is only available at a join above its own evaluation
level.  The outer PHV's rel thus becomes laterally dependent on the
other sibling rels.  If one of those rels laterally references it in
turn, we end up with a rel that is laterally dependent on itself,
causing an assertion failure in create_lateral_join_info().

To fix, also search the whole query for PHVs that are evaluated at
some but not all of the sibling rels, and refuse to remove the
RTE_RESULT if any of them contains one of its PHVs.  An outer PHV that
is evaluated at all of the sibling rels is not a problem, since the
moved PHV is available there, so we don't block removal in that common
case.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_hLOsW8_SePp_3EmYTLX8Oc4zEZEvypyPRszpoMhDdGQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/17b45abefb59800c4e4f94bd17b5a3de91e4a633

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 69 ++++++++++++++++++++++++-
src/test/regress/expected/join.out        | 83 +++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql             | 34 +++++++++++++
3 files changed, 184 insertions(+), 2 deletions(-)



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

* pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar
@ 2026-10-19 02:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-10-19 02:07 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't remove an RTE_RESULT needed by a nested PlaceHolderVar

When remove_useless_result_rtes() removes an RTE_RESULT, any
PlaceHolderVars that were to be evaluated there must be evaluated
somewhere else.  If the RTE_RESULT is the nullable side of an outer
join, there is no other place that would preserve the PHVs' nulling
behavior, so find_dependent_phvs() refuses the removal if any such PHV
exists anywhere in the query.  If the RTE_RESULT is a child of an
inner join, its PHVs can instead be evaluated at the join of all its
sibling rels, unless a sibling needs them before that join is formed.
find_dependent_phvs_in_jointree() checks for that by looking for such
PHVs in the join quals and LATERAL RTEs of the siblings.

That misses a PHV that is evaluated at some but not all of the sibling
rels and contains one of the RTE_RESULT's PHVs.  Such a PHV can appear
anywhere in the query, such as in the targetlist.  This can happen
when pulled-up expressions are wrapped in PHVs because of outer joins
or grouping sets.  Once the RTE_RESULT is removed, the outer PHV needs
a value that is only available at a join above its own evaluation
level.  The outer PHV's rel thus becomes laterally dependent on the
other sibling rels.  If one of those rels laterally references it in
turn, we end up with a rel that is laterally dependent on itself,
causing an assertion failure in create_lateral_join_info().

To fix, also search the whole query for PHVs that are evaluated at
some but not all of the sibling rels, and refuse to remove the
RTE_RESULT if any of them contains one of its PHVs.  An outer PHV that
is evaluated at all of the sibling rels is not a problem, since the
moved PHV is available there, so we don't block removal in that common
case.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_hLOsW8_SePp_3EmYTLX8Oc4zEZEvypyPRszpoMhDdGQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/94035972af3f10cdaf9ff2b5d232ad5f45463469

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 69 ++++++++++++++++++++++++-
src/test/regress/expected/join.out        | 83 +++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql             | 34 +++++++++++++
3 files changed, 184 insertions(+), 2 deletions(-)



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

* pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar
@ 2026-10-19 02:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-10-19 02:07 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't remove an RTE_RESULT needed by a nested PlaceHolderVar

When remove_useless_result_rtes() removes an RTE_RESULT, any
PlaceHolderVars that were to be evaluated there must be evaluated
somewhere else.  If the RTE_RESULT is the nullable side of an outer
join, there is no other place that would preserve the PHVs' nulling
behavior, so find_dependent_phvs() refuses the removal if any such PHV
exists anywhere in the query.  If the RTE_RESULT is a child of an
inner join, its PHVs can instead be evaluated at the join of all its
sibling rels, unless a sibling needs them before that join is formed.
find_dependent_phvs_in_jointree() checks for that by looking for such
PHVs in the join quals and LATERAL RTEs of the siblings.

That misses a PHV that is evaluated at some but not all of the sibling
rels and contains one of the RTE_RESULT's PHVs.  Such a PHV can appear
anywhere in the query, such as in the targetlist.  This can happen
when pulled-up expressions are wrapped in PHVs because of outer joins
or grouping sets.  Once the RTE_RESULT is removed, the outer PHV needs
a value that is only available at a join above its own evaluation
level.  The outer PHV's rel thus becomes laterally dependent on the
other sibling rels.  If one of those rels laterally references it in
turn, we end up with a rel that is laterally dependent on itself,
causing an assertion failure in create_lateral_join_info().

To fix, also search the whole query for PHVs that are evaluated at
some but not all of the sibling rels, and refuse to remove the
RTE_RESULT if any of them contains one of its PHVs.  An outer PHV that
is evaluated at all of the sibling rels is not a problem, since the
moved PHV is available there, so we don't block removal in that common
case.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_hLOsW8_SePp_3EmYTLX8Oc4zEZEvypyPRszpoMhDdGQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_16_STABLE

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

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 69 ++++++++++++++++++++++++-
src/test/regress/expected/join.out        | 83 +++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql             | 34 +++++++++++++
3 files changed, 184 insertions(+), 2 deletions(-)



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

* pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar
@ 2026-10-19 02:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-10-19 02:07 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't remove an RTE_RESULT needed by a nested PlaceHolderVar

When remove_useless_result_rtes() removes an RTE_RESULT, any
PlaceHolderVars that were to be evaluated there must be evaluated
somewhere else.  If the RTE_RESULT is the nullable side of an outer
join, there is no other place that would preserve the PHVs' nulling
behavior, so find_dependent_phvs() refuses the removal if any such PHV
exists anywhere in the query.  If the RTE_RESULT is a child of an
inner join, its PHVs can instead be evaluated at the join of all its
sibling rels, unless a sibling needs them before that join is formed.
find_dependent_phvs_in_jointree() checks for that by looking for such
PHVs in the join quals and LATERAL RTEs of the siblings.

That misses a PHV that is evaluated at some but not all of the sibling
rels and contains one of the RTE_RESULT's PHVs.  Such a PHV can appear
anywhere in the query, such as in the targetlist.  This can happen
when pulled-up expressions are wrapped in PHVs because of outer joins
or grouping sets.  Once the RTE_RESULT is removed, the outer PHV needs
a value that is only available at a join above its own evaluation
level.  The outer PHV's rel thus becomes laterally dependent on the
other sibling rels.  If one of those rels laterally references it in
turn, we end up with a rel that is laterally dependent on itself,
causing an assertion failure in create_lateral_join_info().

To fix, also search the whole query for PHVs that are evaluated at
some but not all of the sibling rels, and refuse to remove the
RTE_RESULT if any of them contains one of its PHVs.  An outer PHV that
is evaluated at all of the sibling rels is not a problem, since the
moved PHV is available there, so we don't block removal in that common
case.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_hLOsW8_SePp_3EmYTLX8Oc4zEZEvypyPRszpoMhDdGQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/34147f8840a40085a55ab10f830e91319654c0ff

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 66 +++++++++++++++++++++++-
src/test/regress/expected/join.out        | 83 +++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql             | 34 +++++++++++++
3 files changed, 181 insertions(+), 2 deletions(-)



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

* pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar
@ 2026-10-19 02:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Richard Guo @ 2026-10-19 02:07 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't remove an RTE_RESULT needed by a nested PlaceHolderVar

When remove_useless_result_rtes() removes an RTE_RESULT, any
PlaceHolderVars that were to be evaluated there must be evaluated
somewhere else.  If the RTE_RESULT is the nullable side of an outer
join, there is no other place that would preserve the PHVs' nulling
behavior, so find_dependent_phvs() refuses the removal if any such PHV
exists anywhere in the query.  If the RTE_RESULT is a child of an
inner join, its PHVs can instead be evaluated at the join of all its
sibling rels, unless a sibling needs them before that join is formed.
find_dependent_phvs_in_jointree() checks for that by looking for such
PHVs in the join quals and LATERAL RTEs of the siblings.

That misses a PHV that is evaluated at some but not all of the sibling
rels and contains one of the RTE_RESULT's PHVs.  Such a PHV can appear
anywhere in the query, such as in the targetlist.  This can happen
when pulled-up expressions are wrapped in PHVs because of outer joins
or grouping sets.  Once the RTE_RESULT is removed, the outer PHV needs
a value that is only available at a join above its own evaluation
level.  The outer PHV's rel thus becomes laterally dependent on the
other sibling rels.  If one of those rels laterally references it in
turn, we end up with a rel that is laterally dependent on itself,
causing an assertion failure in create_lateral_join_info().

To fix, also search the whole query for PHVs that are evaluated at
some but not all of the sibling rels, and refuse to remove the
RTE_RESULT if any of them contains one of its PHVs.  An outer PHV that
is evaluated at all of the sibling rels is not a problem, since the
moved PHV is available there, so we don't block removal in that common
case.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_hLOsW8_SePp_3EmYTLX8Oc4zEZEvypyPRszpoMhDdGQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_14_STABLE

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

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 66 +++++++++++++++++++++++-
src/test/regress/expected/join.out        | 83 +++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql             | 34 +++++++++++++
3 files changed, 181 insertions(+), 2 deletions(-)



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


end of thread, other threads:[~2026-10-19 02:07 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-19 02:07 pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar Richard Guo <rguo@postgresql.org>
2026-10-19 02:07 pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar Richard Guo <rguo@postgresql.org>
2026-10-19 02:07 pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar Richard Guo <rguo@postgresql.org>
2026-10-19 02:07 pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar Richard Guo <rguo@postgresql.org>
2026-10-19 02:07 pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar Richard Guo <rguo@postgresql.org>
2026-10-19 02:07 pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar Richard Guo <rguo@postgresql.org>
2026-10-19 02:07 pgsql: Don't remove an RTE_RESULT needed by a nested PlaceHolderVar Richard Guo <rguo@postgresql.org>

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