agora inbox for pgsql-committers@postgresql.org  
help / color / mirror / Atom feed
pgsql: Fix edge case in remove_useless_result_rtes() with outer joins.
5+ messages / 1 participants
[nested] [flat]

* pgsql: Fix edge case in remove_useless_result_rtes() with outer joins.
@ 2026-07-18 18:09 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2026-07-18 18:09 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix edge case in remove_useless_result_rtes() with outer joins.

find_dependent_phvs() and find_dependent_phvs_in_jointree() decide
whether a PlaceHolderVar depends on the RTE_RESULT rel we're
considering removing by comparing the PHV's phrels to a singleton set
containing that rel's RT index, reasoning that if phrels contains any
other relid bits then those define an appropriate place where we can
evaluate the PHV.  But since this code was originally written, we've
redefined phrels to include outer-join relids, and that breaks this
logic, potentially allowing us to remove an RTE_RESULT that leaves no
valid place to evaluate the PHV.  The planner doesn't throw an error
when that happens, but it does produce an incorrect plan that will not
replace the PHV's value with NULL when needed.

In the known test case for this bug, the "extra" OJ relid is one that
we've actually decided to remove but haven't yet cleaned out of the
query's PHVs.  It's not entirely clear though that that would always
be the case.  Let's restore this code to the way it was designed to
work, by considering only base relids within the PHV's phrels.

Bug: #19553
Reported-by: Viktor Leis <leis@in.tum.de>
Author: Matheus Alcantara <matheusssilv97@gmail.com>
Co-authored-by: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19553-4561747f93f368a7@postgresql.org
Backpatch-through: 16

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/1df9e8d969d60cbbb296b408a9f595fa2aab2701

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 64 ++++++++++++++++++++++++-------
src/test/regress/expected/join.out        | 27 +++++++++++++
src/test/regress/sql/join.sql             | 10 +++++
3 files changed, 88 insertions(+), 13 deletions(-)



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

* pgsql: Fix edge case in remove_useless_result_rtes() with outer joins.
@ 2026-07-18 18:09 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2026-07-18 18:09 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix edge case in remove_useless_result_rtes() with outer joins.

find_dependent_phvs() and find_dependent_phvs_in_jointree() decide
whether a PlaceHolderVar depends on the RTE_RESULT rel we're
considering removing by comparing the PHV's phrels to a singleton set
containing that rel's RT index, reasoning that if phrels contains any
other relid bits then those define an appropriate place where we can
evaluate the PHV.  But since this code was originally written, we've
redefined phrels to include outer-join relids, and that breaks this
logic, potentially allowing us to remove an RTE_RESULT that leaves no
valid place to evaluate the PHV.  The planner doesn't throw an error
when that happens, but it does produce an incorrect plan that will not
replace the PHV's value with NULL when needed.

In the known test case for this bug, the "extra" OJ relid is one that
we've actually decided to remove but haven't yet cleaned out of the
query's PHVs.  It's not entirely clear though that that would always
be the case.  Let's restore this code to the way it was designed to
work, by considering only base relids within the PHV's phrels.

Bug: #19553
Reported-by: Viktor Leis <leis@in.tum.de>
Author: Matheus Alcantara <matheusssilv97@gmail.com>
Co-authored-by: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19553-4561747f93f368a7@postgresql.org
Backpatch-through: 16

Branch
------
REL_19_STABLE

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

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 64 ++++++++++++++++++++++++-------
src/test/regress/expected/join.out        | 27 +++++++++++++
src/test/regress/sql/join.sql             | 10 +++++
3 files changed, 88 insertions(+), 13 deletions(-)



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

* pgsql: Fix edge case in remove_useless_result_rtes() with outer joins.
@ 2026-07-18 18:09 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2026-07-18 18:09 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix edge case in remove_useless_result_rtes() with outer joins.

find_dependent_phvs() and find_dependent_phvs_in_jointree() decide
whether a PlaceHolderVar depends on the RTE_RESULT rel we're
considering removing by comparing the PHV's phrels to a singleton set
containing that rel's RT index, reasoning that if phrels contains any
other relid bits then those define an appropriate place where we can
evaluate the PHV.  But since this code was originally written, we've
redefined phrels to include outer-join relids, and that breaks this
logic, potentially allowing us to remove an RTE_RESULT that leaves no
valid place to evaluate the PHV.  The planner doesn't throw an error
when that happens, but it does produce an incorrect plan that will not
replace the PHV's value with NULL when needed.

In the known test case for this bug, the "extra" OJ relid is one that
we've actually decided to remove but haven't yet cleaned out of the
query's PHVs.  It's not entirely clear though that that would always
be the case.  Let's restore this code to the way it was designed to
work, by considering only base relids within the PHV's phrels.

Bug: #19553
Reported-by: Viktor Leis <leis@in.tum.de>
Author: Matheus Alcantara <matheusssilv97@gmail.com>
Co-authored-by: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19553-4561747f93f368a7@postgresql.org
Backpatch-through: 16

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/21f5e659e7587abf640f9c4aaff2238e3c3ff683

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 64 ++++++++++++++++++++++++-------
src/test/regress/expected/join.out        | 27 +++++++++++++
src/test/regress/sql/join.sql             | 10 +++++
3 files changed, 88 insertions(+), 13 deletions(-)



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

* pgsql: Fix edge case in remove_useless_result_rtes() with outer joins.
@ 2026-07-18 18:09 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2026-07-18 18:09 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix edge case in remove_useless_result_rtes() with outer joins.

find_dependent_phvs() and find_dependent_phvs_in_jointree() decide
whether a PlaceHolderVar depends on the RTE_RESULT rel we're
considering removing by comparing the PHV's phrels to a singleton set
containing that rel's RT index, reasoning that if phrels contains any
other relid bits then those define an appropriate place where we can
evaluate the PHV.  But since this code was originally written, we've
redefined phrels to include outer-join relids, and that breaks this
logic, potentially allowing us to remove an RTE_RESULT that leaves no
valid place to evaluate the PHV.  The planner doesn't throw an error
when that happens, but it does produce an incorrect plan that will not
replace the PHV's value with NULL when needed.

In the known test case for this bug, the "extra" OJ relid is one that
we've actually decided to remove but haven't yet cleaned out of the
query's PHVs.  It's not entirely clear though that that would always
be the case.  Let's restore this code to the way it was designed to
work, by considering only base relids within the PHV's phrels.

Bug: #19553
Reported-by: Viktor Leis <leis@in.tum.de>
Author: Matheus Alcantara <matheusssilv97@gmail.com>
Co-authored-by: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19553-4561747f93f368a7@postgresql.org
Backpatch-through: 16

Branch
------
REL_17_STABLE

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

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 64 ++++++++++++++++++++++++-------
src/test/regress/expected/join.out        | 27 +++++++++++++
src/test/regress/sql/join.sql             | 10 +++++
3 files changed, 88 insertions(+), 13 deletions(-)



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

* pgsql: Fix edge case in remove_useless_result_rtes() with outer joins.
@ 2026-07-18 18:09 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2026-07-18 18:09 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix edge case in remove_useless_result_rtes() with outer joins.

find_dependent_phvs() and find_dependent_phvs_in_jointree() decide
whether a PlaceHolderVar depends on the RTE_RESULT rel we're
considering removing by comparing the PHV's phrels to a singleton set
containing that rel's RT index, reasoning that if phrels contains any
other relid bits then those define an appropriate place where we can
evaluate the PHV.  But since this code was originally written, we've
redefined phrels to include outer-join relids, and that breaks this
logic, potentially allowing us to remove an RTE_RESULT that leaves no
valid place to evaluate the PHV.  The planner doesn't throw an error
when that happens, but it does produce an incorrect plan that will not
replace the PHV's value with NULL when needed.

In the known test case for this bug, the "extra" OJ relid is one that
we've actually decided to remove but haven't yet cleaned out of the
query's PHVs.  It's not entirely clear though that that would always
be the case.  Let's restore this code to the way it was designed to
work, by considering only base relids within the PHV's phrels.

Bug: #19553
Reported-by: Viktor Leis <leis@in.tum.de>
Author: Matheus Alcantara <matheusssilv97@gmail.com>
Co-authored-by: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19553-4561747f93f368a7@postgresql.org
Backpatch-through: 16

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/3e1fe25e686d788a32c926e2aa79bc8d9312841b

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 64 ++++++++++++++++++++++++-------
src/test/regress/expected/join.out        | 27 +++++++++++++
src/test/regress/sql/join.sql             | 10 +++++
3 files changed, 88 insertions(+), 13 deletions(-)



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


end of thread, other threads:[~2026-07-18 18:09 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-07-18 18:09 pgsql: Fix edge case in remove_useless_result_rtes() with outer joins. Tom Lane <tgl@sss.pgh.pa.us>
2026-07-18 18:09 pgsql: Fix edge case in remove_useless_result_rtes() with outer joins. Tom Lane <tgl@sss.pgh.pa.us>
2026-07-18 18:09 pgsql: Fix edge case in remove_useless_result_rtes() with outer joins. Tom Lane <tgl@sss.pgh.pa.us>
2026-07-18 18:09 pgsql: Fix edge case in remove_useless_result_rtes() with outer joins. Tom Lane <tgl@sss.pgh.pa.us>
2026-07-18 18:09 pgsql: Fix edge case in remove_useless_result_rtes() with outer joins. Tom Lane <tgl@sss.pgh.pa.us>

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