pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix bogus outer-join commutation with lateral refs in PlaceHolde
5+ messages / 1 participants
[nested] [flat]

* pgsql: Fix bogus outer-join commutation with lateral refs in PlaceHolde
@ 2026-10-17 08:21 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-10-17 08:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix bogus outer-join commutation with lateral refs in PlaceHolderVars

make_outerjoininfo() decides whether outer join identity 3 applies
between an outer join and a lower one in its LHS by checking that the
join clause is strict for the lower join's RHS.  It relied on the
clause then necessarily referencing that RHS.  That is not so when the
strictness comes through a PlaceHolderVar: find_nonnullable_rels()
looks inside the PHV's expression and reports rels it references
laterally, while pull_varnos() reports only where the PHV is
evaluated.

In that case we marked the two joins as commutable although the clause
does not use the lower join's RHS at all.  Joinrels for the same set
of base rels could then be built both with and without the upper
join's relid, leading to an assertion failure in
standard_join_search().  The bogus commutation also caused an extra
clone of the upper join's clause to be generated, which could be
applied at a higher join in addition to the clone already applied
below it.  In non-assert builds, this could lead to planner errors
such as "variable not found in subplan target lists".

To fix, ignore rels that the clause is strict for but does not
reference, consistent with clause_relids treating a PHV as computed at
its ph_eval_at.

Back-patch to v16, where this commutation logic was introduced.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49xvh4-uPv3rxWkk_rtMT1ic+psHvEAxaFEXu2Rf-32Tw@mail.gmail.com
Backpatch-through: 16

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/7787aec8ab000932a8704a988b175b39ff816f2f

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c |  7 +++++++
src/test/regress/expected/join.out     | 33 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql          | 14 ++++++++++++++
3 files changed, 54 insertions(+)



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

* pgsql: Fix bogus outer-join commutation with lateral refs in PlaceHolde
@ 2026-10-17 08:21 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-10-17 08:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix bogus outer-join commutation with lateral refs in PlaceHolderVars

make_outerjoininfo() decides whether outer join identity 3 applies
between an outer join and a lower one in its LHS by checking that the
join clause is strict for the lower join's RHS.  It relied on the
clause then necessarily referencing that RHS.  That is not so when the
strictness comes through a PlaceHolderVar: find_nonnullable_rels()
looks inside the PHV's expression and reports rels it references
laterally, while pull_varnos() reports only where the PHV is
evaluated.

In that case we marked the two joins as commutable although the clause
does not use the lower join's RHS at all.  Joinrels for the same set
of base rels could then be built both with and without the upper
join's relid, leading to an assertion failure in
standard_join_search().  The bogus commutation also caused an extra
clone of the upper join's clause to be generated, which could be
applied at a higher join in addition to the clone already applied
below it.  In non-assert builds, this could lead to planner errors
such as "variable not found in subplan target lists".

To fix, ignore rels that the clause is strict for but does not
reference, consistent with clause_relids treating a PHV as computed at
its ph_eval_at.

Back-patch to v16, where this commutation logic was introduced.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49xvh4-uPv3rxWkk_rtMT1ic+psHvEAxaFEXu2Rf-32Tw@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/221423812daa55b1ed62b86cab9cb46006812922

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c |  7 +++++++
src/test/regress/expected/join.out     | 33 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql          | 14 ++++++++++++++
3 files changed, 54 insertions(+)



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

* pgsql: Fix bogus outer-join commutation with lateral refs in PlaceHolde
@ 2026-10-17 08:21 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-10-17 08:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix bogus outer-join commutation with lateral refs in PlaceHolderVars

make_outerjoininfo() decides whether outer join identity 3 applies
between an outer join and a lower one in its LHS by checking that the
join clause is strict for the lower join's RHS.  It relied on the
clause then necessarily referencing that RHS.  That is not so when the
strictness comes through a PlaceHolderVar: find_nonnullable_rels()
looks inside the PHV's expression and reports rels it references
laterally, while pull_varnos() reports only where the PHV is
evaluated.

In that case we marked the two joins as commutable although the clause
does not use the lower join's RHS at all.  Joinrels for the same set
of base rels could then be built both with and without the upper
join's relid, leading to an assertion failure in
standard_join_search().  The bogus commutation also caused an extra
clone of the upper join's clause to be generated, which could be
applied at a higher join in addition to the clone already applied
below it.  In non-assert builds, this could lead to planner errors
such as "variable not found in subplan target lists".

To fix, ignore rels that the clause is strict for but does not
reference, consistent with clause_relids treating a PHV as computed at
its ph_eval_at.

Back-patch to v16, where this commutation logic was introduced.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49xvh4-uPv3rxWkk_rtMT1ic+psHvEAxaFEXu2Rf-32Tw@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_18_STABLE

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

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c |  7 +++++++
src/test/regress/expected/join.out     | 33 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql          | 14 ++++++++++++++
3 files changed, 54 insertions(+)



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

* pgsql: Fix bogus outer-join commutation with lateral refs in PlaceHolde
@ 2026-10-17 08:21 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-10-17 08:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix bogus outer-join commutation with lateral refs in PlaceHolderVars

make_outerjoininfo() decides whether outer join identity 3 applies
between an outer join and a lower one in its LHS by checking that the
join clause is strict for the lower join's RHS.  It relied on the
clause then necessarily referencing that RHS.  That is not so when the
strictness comes through a PlaceHolderVar: find_nonnullable_rels()
looks inside the PHV's expression and reports rels it references
laterally, while pull_varnos() reports only where the PHV is
evaluated.

In that case we marked the two joins as commutable although the clause
does not use the lower join's RHS at all.  Joinrels for the same set
of base rels could then be built both with and without the upper
join's relid, leading to an assertion failure in
standard_join_search().  The bogus commutation also caused an extra
clone of the upper join's clause to be generated, which could be
applied at a higher join in addition to the clone already applied
below it.  In non-assert builds, this could lead to planner errors
such as "variable not found in subplan target lists".

To fix, ignore rels that the clause is strict for but does not
reference, consistent with clause_relids treating a PHV as computed at
its ph_eval_at.

Back-patch to v16, where this commutation logic was introduced.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49xvh4-uPv3rxWkk_rtMT1ic+psHvEAxaFEXu2Rf-32Tw@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/65e944d92b082cc16e2265a91bab8b73934a0e74

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c |  7 +++++++
src/test/regress/expected/join.out     | 30 ++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql          | 14 ++++++++++++++
3 files changed, 51 insertions(+)



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

* pgsql: Fix bogus outer-join commutation with lateral refs in PlaceHolde
@ 2026-10-17 08:21 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-10-17 08:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix bogus outer-join commutation with lateral refs in PlaceHolderVars

make_outerjoininfo() decides whether outer join identity 3 applies
between an outer join and a lower one in its LHS by checking that the
join clause is strict for the lower join's RHS.  It relied on the
clause then necessarily referencing that RHS.  That is not so when the
strictness comes through a PlaceHolderVar: find_nonnullable_rels()
looks inside the PHV's expression and reports rels it references
laterally, while pull_varnos() reports only where the PHV is
evaluated.

In that case we marked the two joins as commutable although the clause
does not use the lower join's RHS at all.  Joinrels for the same set
of base rels could then be built both with and without the upper
join's relid, leading to an assertion failure in
standard_join_search().  The bogus commutation also caused an extra
clone of the upper join's clause to be generated, which could be
applied at a higher join in addition to the clone already applied
below it.  In non-assert builds, this could lead to planner errors
such as "variable not found in subplan target lists".

To fix, ignore rels that the clause is strict for but does not
reference, consistent with clause_relids treating a PHV as computed at
its ph_eval_at.

Back-patch to v16, where this commutation logic was introduced.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49xvh4-uPv3rxWkk_rtMT1ic+psHvEAxaFEXu2Rf-32Tw@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_16_STABLE

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

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c |  7 +++++++
src/test/regress/expected/join.out     | 30 ++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql          | 14 ++++++++++++++
3 files changed, 51 insertions(+)



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


end of thread, other threads:[~2026-10-17 08:21 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-17 08:21 pgsql: Fix bogus outer-join commutation with lateral refs in PlaceHolde Richard Guo <rguo@postgresql.org>
2026-10-17 08:21 pgsql: Fix bogus outer-join commutation with lateral refs in PlaceHolde Richard Guo <rguo@postgresql.org>
2026-10-17 08:21 pgsql: Fix bogus outer-join commutation with lateral refs in PlaceHolde Richard Guo <rguo@postgresql.org>
2026-10-17 08:21 pgsql: Fix bogus outer-join commutation with lateral refs in PlaceHolde Richard Guo <rguo@postgresql.org>
2026-10-17 08:21 pgsql: Fix bogus outer-join commutation with lateral refs in PlaceHolde 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