pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix lateral references in sort keys added by createplan.c
7+ messages / 1 participants
[nested] [flat]

* pgsql: Fix lateral references in sort keys added by createplan.c
@ 2026-10-21 09:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

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

Fix lateral references in sort keys added by createplan.c

When prepare_sort_from_pathkeys() cannot find a pathkey's expression
in the input plan's targetlist, it adds a resjunk entry computing an
EquivalenceClass member's expression.  It copied that expression
as-is, without replacing lateral references with nestloop Params the
way build_path_tlist() does for the rest of the targetlist.

This matters when the sort key contains a PlaceHolderVar whose
expression has a lateral reference, and the sort is placed directly
atop the plan node that evaluates the PlaceHolderVar.  The targetlist
already contains the PlaceHolderVar, so the sort key is considered
computable, but the entry added for it still has the outer relation's
Var inside the PlaceHolderVar.  If the node is a join, setrefs.c fails
with "variable not found in subplan target lists".  If it is a scan,
setrefs.c leaves the Var in place, and the executor fetches it from
the scan tuple, leading to wrong results.

To fix, pass root to prepare_sort_from_pathkeys() and apply
replace_nestloop_params() to the expression before adding it to the
targetlist.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49LWNm7_OxuW1V71wLi+Ev3KbfY1bMY36uydNKwSPnbtQ@mail.gmail.com
Backpatch-through: 14

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/6b9f40d49ae33eb0b4a7c77666fe4971ef7ba087

Modified Files
--------------
src/backend/optimizer/plan/createplan.c | 58 +++++++++++++---------
src/test/regress/expected/join.out      | 87 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql           | 34 +++++++++++++
3 files changed, 156 insertions(+), 23 deletions(-)



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

* pgsql: Fix lateral references in sort keys added by createplan.c
@ 2026-10-21 09:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

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

Fix lateral references in sort keys added by createplan.c

When prepare_sort_from_pathkeys() cannot find a pathkey's expression
in the input plan's targetlist, it adds a resjunk entry computing an
EquivalenceClass member's expression.  It copied that expression
as-is, without replacing lateral references with nestloop Params the
way build_path_tlist() does for the rest of the targetlist.

This matters when the sort key contains a PlaceHolderVar whose
expression has a lateral reference, and the sort is placed directly
atop the plan node that evaluates the PlaceHolderVar.  The targetlist
already contains the PlaceHolderVar, so the sort key is considered
computable, but the entry added for it still has the outer relation's
Var inside the PlaceHolderVar.  If the node is a join, setrefs.c fails
with "variable not found in subplan target lists".  If it is a scan,
setrefs.c leaves the Var in place, and the executor fetches it from
the scan tuple, leading to wrong results.

To fix, pass root to prepare_sort_from_pathkeys() and apply
replace_nestloop_params() to the expression before adding it to the
targetlist.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49LWNm7_OxuW1V71wLi+Ev3KbfY1bMY36uydNKwSPnbtQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_19_STABLE

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

Modified Files
--------------
src/backend/optimizer/plan/createplan.c | 58 +++++++++++++---------
src/test/regress/expected/join.out      | 87 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql           | 34 +++++++++++++
3 files changed, 156 insertions(+), 23 deletions(-)



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

* pgsql: Fix lateral references in sort keys added by createplan.c
@ 2026-10-21 09:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

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

Fix lateral references in sort keys added by createplan.c

When prepare_sort_from_pathkeys() cannot find a pathkey's expression
in the input plan's targetlist, it adds a resjunk entry computing an
EquivalenceClass member's expression.  It copied that expression
as-is, without replacing lateral references with nestloop Params the
way build_path_tlist() does for the rest of the targetlist.

This matters when the sort key contains a PlaceHolderVar whose
expression has a lateral reference, and the sort is placed directly
atop the plan node that evaluates the PlaceHolderVar.  The targetlist
already contains the PlaceHolderVar, so the sort key is considered
computable, but the entry added for it still has the outer relation's
Var inside the PlaceHolderVar.  If the node is a join, setrefs.c fails
with "variable not found in subplan target lists".  If it is a scan,
setrefs.c leaves the Var in place, and the executor fetches it from
the scan tuple, leading to wrong results.

To fix, pass root to prepare_sort_from_pathkeys() and apply
replace_nestloop_params() to the expression before adding it to the
targetlist.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49LWNm7_OxuW1V71wLi+Ev3KbfY1bMY36uydNKwSPnbtQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_18_STABLE

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

Modified Files
--------------
src/backend/optimizer/plan/createplan.c | 58 +++++++++++++---------
src/test/regress/expected/join.out      | 87 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql           | 34 +++++++++++++
3 files changed, 156 insertions(+), 23 deletions(-)



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

* pgsql: Fix lateral references in sort keys added by createplan.c
@ 2026-10-21 09:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

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

Fix lateral references in sort keys added by createplan.c

When prepare_sort_from_pathkeys() cannot find a pathkey's expression
in the input plan's targetlist, it adds a resjunk entry computing an
EquivalenceClass member's expression.  It copied that expression
as-is, without replacing lateral references with nestloop Params the
way build_path_tlist() does for the rest of the targetlist.

This matters when the sort key contains a PlaceHolderVar whose
expression has a lateral reference, and the sort is placed directly
atop the plan node that evaluates the PlaceHolderVar.  The targetlist
already contains the PlaceHolderVar, so the sort key is considered
computable, but the entry added for it still has the outer relation's
Var inside the PlaceHolderVar.  If the node is a join, setrefs.c fails
with "variable not found in subplan target lists".  If it is a scan,
setrefs.c leaves the Var in place, and the executor fetches it from
the scan tuple, leading to wrong results.

To fix, pass root to prepare_sort_from_pathkeys() and apply
replace_nestloop_params() to the expression before adding it to the
targetlist.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49LWNm7_OxuW1V71wLi+Ev3KbfY1bMY36uydNKwSPnbtQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_17_STABLE

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

Modified Files
--------------
src/backend/optimizer/plan/createplan.c | 56 ++++++++++++---------
src/test/regress/expected/join.out      | 86 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql           | 34 +++++++++++++
3 files changed, 154 insertions(+), 22 deletions(-)



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

* pgsql: Fix lateral references in sort keys added by createplan.c
@ 2026-10-21 09:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

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

Fix lateral references in sort keys added by createplan.c

When prepare_sort_from_pathkeys() cannot find a pathkey's expression
in the input plan's targetlist, it adds a resjunk entry computing an
EquivalenceClass member's expression.  It copied that expression
as-is, without replacing lateral references with nestloop Params the
way build_path_tlist() does for the rest of the targetlist.

This matters when the sort key contains a PlaceHolderVar whose
expression has a lateral reference, and the sort is placed directly
atop the plan node that evaluates the PlaceHolderVar.  The targetlist
already contains the PlaceHolderVar, so the sort key is considered
computable, but the entry added for it still has the outer relation's
Var inside the PlaceHolderVar.  If the node is a join, setrefs.c fails
with "variable not found in subplan target lists".  If it is a scan,
setrefs.c leaves the Var in place, and the executor fetches it from
the scan tuple, leading to wrong results.

To fix, pass root to prepare_sort_from_pathkeys() and apply
replace_nestloop_params() to the expression before adding it to the
targetlist.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49LWNm7_OxuW1V71wLi+Ev3KbfY1bMY36uydNKwSPnbtQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/617555d749a3fc7d80b86474451e448cf4dae3e7

Modified Files
--------------
src/backend/optimizer/plan/createplan.c | 56 ++++++++++++---------
src/test/regress/expected/join.out      | 86 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql           | 34 +++++++++++++
3 files changed, 154 insertions(+), 22 deletions(-)



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

* pgsql: Fix lateral references in sort keys added by createplan.c
@ 2026-10-21 09:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

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

Fix lateral references in sort keys added by createplan.c

When prepare_sort_from_pathkeys() cannot find a pathkey's expression
in the input plan's targetlist, it adds a resjunk entry computing an
EquivalenceClass member's expression.  It copied that expression
as-is, without replacing lateral references with nestloop Params the
way build_path_tlist() does for the rest of the targetlist.

This matters when the sort key contains a PlaceHolderVar whose
expression has a lateral reference, and the sort is placed directly
atop the plan node that evaluates the PlaceHolderVar.  The targetlist
already contains the PlaceHolderVar, so the sort key is considered
computable, but the entry added for it still has the outer relation's
Var inside the PlaceHolderVar.  If the node is a join, setrefs.c fails
with "variable not found in subplan target lists".  If it is a scan,
setrefs.c leaves the Var in place, and the executor fetches it from
the scan tuple, leading to wrong results.

To fix, pass root to prepare_sort_from_pathkeys() and apply
replace_nestloop_params() to the expression before adding it to the
targetlist.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49LWNm7_OxuW1V71wLi+Ev3KbfY1bMY36uydNKwSPnbtQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/5092f544e6109d6e234f047f56782c561b10dac2

Modified Files
--------------
src/backend/optimizer/plan/createplan.c | 56 ++++++++++++---------
src/test/regress/expected/join.out      | 86 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql           | 34 +++++++++++++
3 files changed, 154 insertions(+), 22 deletions(-)



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

* pgsql: Fix lateral references in sort keys added by createplan.c
@ 2026-10-21 09:07 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

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

Fix lateral references in sort keys added by createplan.c

When prepare_sort_from_pathkeys() cannot find a pathkey's expression
in the input plan's targetlist, it adds a resjunk entry computing an
EquivalenceClass member's expression.  It copied that expression
as-is, without replacing lateral references with nestloop Params the
way build_path_tlist() does for the rest of the targetlist.

This matters when the sort key contains a PlaceHolderVar whose
expression has a lateral reference, and the sort is placed directly
atop the plan node that evaluates the PlaceHolderVar.  The targetlist
already contains the PlaceHolderVar, so the sort key is considered
computable, but the entry added for it still has the outer relation's
Var inside the PlaceHolderVar.  If the node is a join, setrefs.c fails
with "variable not found in subplan target lists".  If it is a scan,
setrefs.c leaves the Var in place, and the executor fetches it from
the scan tuple, leading to wrong results.

To fix, pass root to prepare_sort_from_pathkeys() and apply
replace_nestloop_params() to the expression before adding it to the
targetlist.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49LWNm7_OxuW1V71wLi+Ev3KbfY1bMY36uydNKwSPnbtQ@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_14_STABLE

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

Modified Files
--------------
src/backend/optimizer/plan/createplan.c | 56 ++++++++++++---------
src/test/regress/expected/join.out      | 86 +++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql           | 34 +++++++++++++
3 files changed, 154 insertions(+), 22 deletions(-)



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


end of thread, other threads:[~2026-10-21 09: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-21 09:07 pgsql: Fix lateral references in sort keys added by createplan.c Richard Guo <rguo@postgresql.org>
2026-10-21 09:07 pgsql: Fix lateral references in sort keys added by createplan.c Richard Guo <rguo@postgresql.org>
2026-10-21 09:07 pgsql: Fix lateral references in sort keys added by createplan.c Richard Guo <rguo@postgresql.org>
2026-10-21 09:07 pgsql: Fix lateral references in sort keys added by createplan.c Richard Guo <rguo@postgresql.org>
2026-10-21 09:07 pgsql: Fix lateral references in sort keys added by createplan.c Richard Guo <rguo@postgresql.org>
2026-10-21 09:07 pgsql: Fix lateral references in sort keys added by createplan.c Richard Guo <rguo@postgresql.org>
2026-10-21 09:07 pgsql: Fix lateral references in sort keys added by createplan.c 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