pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Disallow joins whose lateral references need an unformable outer
10+ messages / 1 participants
[nested] [flat]

* pgsql: Disallow joins whose lateral references need an unformable outer
@ 2026-09-04 01:36  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 10+ messages in thread

From: Richard Guo @ 2026-09-04 01:36 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Disallow joins whose lateral references need an unformable outer join

If a LATERAL subquery references a PlaceHolderVar that must be
evaluated at an outer join, the subquery's lateral_relids include that
outer join's relid, since the lateral parameter is the outer join's
output.  join_is_legal() verified that the rels named by a proposed
join's minimum parameterization could still be joined to from outside,
but it did not consider such outer-join relids.  As a result, when
identity 3 permitted a commuted join order, we could approve a join
that includes part of that outer join's required input, even though
the outer join could then be completed only above the proposed join,
leaving the lateral parameter forever unsatisfiable.

In assert-enabled builds this tripped the backstop Assert in
try_nestloop_path().  In production builds the bogus join generated
only paths whose parameterization can never be satisfied, so they
could never appear in a complete plan.  This not only wasted planning
effort, but also could mislead the clauseless-join heuristics into
thinking that legality of this join means that some other join rel
need not be formed, and that could lead to failure to find any plan at
all.

Such joins used to be rejected by the have_dangerous_phv() check in
join_is_legal(), which refused any join where a PlaceHolderVar's eval
set overlapped the other side of the join without being contained in
it.  Commit a16ef313f removed that restriction, exposing this
oversight.

To fix, teach join_is_legal() to reject a proposed join if its minimum
parameterization includes an outer-join relid whose outer join cannot
be formed strictly outside the join, that is, when any rel needed to
form that outer join must become part of the proposed join's own join
tree.

Back-patch to v18, where commit a16ef313f removed the
have_dangerous_phv() restriction.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49ZveAYx9T5b0=gxn0pnvqq=MbN-Z4KLbLAE+kH06mWVA@mail.gmail.com
Backpatch-through: 18

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/55fa1b06780ab16a2d48532a3b68451f6b6cc3d8

Modified Files
--------------
src/backend/optimizer/path/joinrels.c | 26 +++++++++++++++++++++++
src/test/regress/expected/join.out    | 40 +++++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql         | 20 ++++++++++++++++++
3 files changed, 86 insertions(+)



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

* pgsql: Disallow joins whose lateral references need an unformable outer
@ 2026-09-04 01:36  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 10+ messages in thread

From: Richard Guo @ 2026-09-04 01:36 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Disallow joins whose lateral references need an unformable outer join

If a LATERAL subquery references a PlaceHolderVar that must be
evaluated at an outer join, the subquery's lateral_relids include that
outer join's relid, since the lateral parameter is the outer join's
output.  join_is_legal() verified that the rels named by a proposed
join's minimum parameterization could still be joined to from outside,
but it did not consider such outer-join relids.  As a result, when
identity 3 permitted a commuted join order, we could approve a join
that includes part of that outer join's required input, even though
the outer join could then be completed only above the proposed join,
leaving the lateral parameter forever unsatisfiable.

In assert-enabled builds this tripped the backstop Assert in
try_nestloop_path().  In production builds the bogus join generated
only paths whose parameterization can never be satisfied, so they
could never appear in a complete plan.  This not only wasted planning
effort, but also could mislead the clauseless-join heuristics into
thinking that legality of this join means that some other join rel
need not be formed, and that could lead to failure to find any plan at
all.

Such joins used to be rejected by the have_dangerous_phv() check in
join_is_legal(), which refused any join where a PlaceHolderVar's eval
set overlapped the other side of the join without being contained in
it.  Commit a16ef313f removed that restriction, exposing this
oversight.

To fix, teach join_is_legal() to reject a proposed join if its minimum
parameterization includes an outer-join relid whose outer join cannot
be formed strictly outside the join, that is, when any rel needed to
form that outer join must become part of the proposed join's own join
tree.

Back-patch to v18, where commit a16ef313f removed the
have_dangerous_phv() restriction.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49ZveAYx9T5b0=gxn0pnvqq=MbN-Z4KLbLAE+kH06mWVA@mail.gmail.com
Backpatch-through: 18

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/10a84539b9474310c849c4a6dc965f496864ec94

Modified Files
--------------
src/backend/optimizer/path/joinrels.c | 26 +++++++++++++++++++++++
src/test/regress/expected/join.out    | 40 +++++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql         | 20 ++++++++++++++++++
3 files changed, 86 insertions(+)



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

* pgsql: Disallow joins whose lateral references need an unformable outer
@ 2026-09-04 01:36  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 10+ messages in thread

From: Richard Guo @ 2026-09-04 01:36 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Disallow joins whose lateral references need an unformable outer join

If a LATERAL subquery references a PlaceHolderVar that must be
evaluated at an outer join, the subquery's lateral_relids include that
outer join's relid, since the lateral parameter is the outer join's
output.  join_is_legal() verified that the rels named by a proposed
join's minimum parameterization could still be joined to from outside,
but it did not consider such outer-join relids.  As a result, when
identity 3 permitted a commuted join order, we could approve a join
that includes part of that outer join's required input, even though
the outer join could then be completed only above the proposed join,
leaving the lateral parameter forever unsatisfiable.

In assert-enabled builds this tripped the backstop Assert in
try_nestloop_path().  In production builds the bogus join generated
only paths whose parameterization can never be satisfied, so they
could never appear in a complete plan.  This not only wasted planning
effort, but also could mislead the clauseless-join heuristics into
thinking that legality of this join means that some other join rel
need not be formed, and that could lead to failure to find any plan at
all.

Such joins used to be rejected by the have_dangerous_phv() check in
join_is_legal(), which refused any join where a PlaceHolderVar's eval
set overlapped the other side of the join without being contained in
it.  Commit a16ef313f removed that restriction, exposing this
oversight.

To fix, teach join_is_legal() to reject a proposed join if its minimum
parameterization includes an outer-join relid whose outer join cannot
be formed strictly outside the join, that is, when any rel needed to
form that outer join must become part of the proposed join's own join
tree.

Back-patch to v18, where commit a16ef313f removed the
have_dangerous_phv() restriction.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49ZveAYx9T5b0=gxn0pnvqq=MbN-Z4KLbLAE+kH06mWVA@mail.gmail.com
Backpatch-through: 18

Branch
------
REL_18_STABLE

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

Modified Files
--------------
src/backend/optimizer/path/joinrels.c | 26 +++++++++++++++++++++++
src/test/regress/expected/join.out    | 40 +++++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql         | 20 ++++++++++++++++++
3 files changed, 86 insertions(+)



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

* pgsql: Disallow joins whose lateral references can never be satisfied
@ 2026-10-14 05:34  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 10+ messages in thread

From: Richard Guo @ 2026-10-14 05:34 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Disallow joins whose lateral references can never be satisfied

If a proposed join has lateral references to rels outside it,
join_is_legal() checks that those rels can still be joined to from
outside.  To do that it collects the rels that must end up on the
inner side of an outer join with the proposed join, directly or
indirectly, and rejects the join if any of its lateral references
point to one of them.

That search followed only outer joins, from min_lefthand to
min_righthand.  It failed to notice that a rel that laterally
references the proposed join, or any rel found by the search, cannot
supply a parameter to the proposed join either, and that the same
holds for rels on the inner side of an outer join with such a rel.  As
a result, we could approve a join whose lateral references could never
be satisfied.

Such a join can never appear in a complete plan.  Worse, as the
existing comment warns, its apparent legality could mislead the
clauseless-join heuristics into thinking that some other join rel need
not be formed.  In particular, have_join_order_restriction() would not
force a clauseless outer join if one of its input rels appeared to
have a legal clause join elsewhere, and if that outer join was
required, we would fail with "failed to build any N-way joins".

To fix, also include in the search any input rel of the current join
search that laterally references a rel already found.  The input rels
are taken from root->initial_rels rather than from the query's base
rels, because an input can be the joinrel of a sub-joinlist, as
happens for a full join or when from_collapse_limit or
join_collapse_limit prevents flattening.  Such a joinrel is joined as
a unit, so it has to be included as a whole.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_p6iWbsjNcYpd6-6mWFnog3UJ2fZKrxWAQTsyNoyeQTw@mail.gmail.com
Backpatch-through: 14

Branch
------
master

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

Modified Files
--------------
src/backend/optimizer/path/allpaths.c |   3 +-
src/backend/optimizer/path/joinrels.c |  21 +++++++
src/test/regress/expected/join.out    | 102 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql         |  44 +++++++++++++++
4 files changed, 169 insertions(+), 1 deletion(-)



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

* pgsql: Disallow joins whose lateral references can never be satisfied
@ 2026-10-14 05:34  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 10+ messages in thread

From: Richard Guo @ 2026-10-14 05:34 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Disallow joins whose lateral references can never be satisfied

If a proposed join has lateral references to rels outside it,
join_is_legal() checks that those rels can still be joined to from
outside.  To do that it collects the rels that must end up on the
inner side of an outer join with the proposed join, directly or
indirectly, and rejects the join if any of its lateral references
point to one of them.

That search followed only outer joins, from min_lefthand to
min_righthand.  It failed to notice that a rel that laterally
references the proposed join, or any rel found by the search, cannot
supply a parameter to the proposed join either, and that the same
holds for rels on the inner side of an outer join with such a rel.  As
a result, we could approve a join whose lateral references could never
be satisfied.

Such a join can never appear in a complete plan.  Worse, as the
existing comment warns, its apparent legality could mislead the
clauseless-join heuristics into thinking that some other join rel need
not be formed.  In particular, have_join_order_restriction() would not
force a clauseless outer join if one of its input rels appeared to
have a legal clause join elsewhere, and if that outer join was
required, we would fail with "failed to build any N-way joins".

To fix, also include in the search any input rel of the current join
search that laterally references a rel already found.  The input rels
are taken from root->initial_rels rather than from the query's base
rels, because an input can be the joinrel of a sub-joinlist, as
happens for a full join or when from_collapse_limit or
join_collapse_limit prevents flattening.  Such a joinrel is joined as
a unit, so it has to be included as a whole.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_p6iWbsjNcYpd6-6mWFnog3UJ2fZKrxWAQTsyNoyeQTw@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/6161b2c3d1627f3eb79b3953301f0c39ac4bb54c

Modified Files
--------------
src/backend/optimizer/path/allpaths.c |   3 +-
src/backend/optimizer/path/joinrels.c |  21 +++++++
src/test/regress/expected/join.out    | 108 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql         |  44 ++++++++++++++
4 files changed, 175 insertions(+), 1 deletion(-)



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

* pgsql: Disallow joins whose lateral references can never be satisfied
@ 2026-10-14 05:34  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 10+ messages in thread

From: Richard Guo @ 2026-10-14 05:34 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Disallow joins whose lateral references can never be satisfied

If a proposed join has lateral references to rels outside it,
join_is_legal() checks that those rels can still be joined to from
outside.  To do that it collects the rels that must end up on the
inner side of an outer join with the proposed join, directly or
indirectly, and rejects the join if any of its lateral references
point to one of them.

That search followed only outer joins, from min_lefthand to
min_righthand.  It failed to notice that a rel that laterally
references the proposed join, or any rel found by the search, cannot
supply a parameter to the proposed join either, and that the same
holds for rels on the inner side of an outer join with such a rel.  As
a result, we could approve a join whose lateral references could never
be satisfied.

Such a join can never appear in a complete plan.  Worse, as the
existing comment warns, its apparent legality could mislead the
clauseless-join heuristics into thinking that some other join rel need
not be formed.  In particular, have_join_order_restriction() would not
force a clauseless outer join if one of its input rels appeared to
have a legal clause join elsewhere, and if that outer join was
required, we would fail with "failed to build any N-way joins".

To fix, also include in the search any input rel of the current join
search that laterally references a rel already found.  The input rels
are taken from root->initial_rels rather than from the query's base
rels, because an input can be the joinrel of a sub-joinlist, as
happens for a full join or when from_collapse_limit or
join_collapse_limit prevents flattening.  Such a joinrel is joined as
a unit, so it has to be included as a whole.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_p6iWbsjNcYpd6-6mWFnog3UJ2fZKrxWAQTsyNoyeQTw@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/02aa28583bf1c8a9e5b6654959d897d9859b4960

Modified Files
--------------
src/backend/optimizer/path/allpaths.c |   3 +-
src/backend/optimizer/path/joinrels.c |  21 +++++++
src/test/regress/expected/join.out    | 102 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql         |  44 +++++++++++++++
4 files changed, 169 insertions(+), 1 deletion(-)



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

* pgsql: Disallow joins whose lateral references can never be satisfied
@ 2026-10-14 05:34  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 10+ messages in thread

From: Richard Guo @ 2026-10-14 05:34 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Disallow joins whose lateral references can never be satisfied

If a proposed join has lateral references to rels outside it,
join_is_legal() checks that those rels can still be joined to from
outside.  To do that it collects the rels that must end up on the
inner side of an outer join with the proposed join, directly or
indirectly, and rejects the join if any of its lateral references
point to one of them.

That search followed only outer joins, from min_lefthand to
min_righthand.  It failed to notice that a rel that laterally
references the proposed join, or any rel found by the search, cannot
supply a parameter to the proposed join either, and that the same
holds for rels on the inner side of an outer join with such a rel.  As
a result, we could approve a join whose lateral references could never
be satisfied.

Such a join can never appear in a complete plan.  Worse, as the
existing comment warns, its apparent legality could mislead the
clauseless-join heuristics into thinking that some other join rel need
not be formed.  In particular, have_join_order_restriction() would not
force a clauseless outer join if one of its input rels appeared to
have a legal clause join elsewhere, and if that outer join was
required, we would fail with "failed to build any N-way joins".

To fix, also include in the search any input rel of the current join
search that laterally references a rel already found.  The input rels
are taken from root->initial_rels rather than from the query's base
rels, because an input can be the joinrel of a sub-joinlist, as
happens for a full join or when from_collapse_limit or
join_collapse_limit prevents flattening.  Such a joinrel is joined as
a unit, so it has to be included as a whole.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_p6iWbsjNcYpd6-6mWFnog3UJ2fZKrxWAQTsyNoyeQTw@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/7432ecba189895c37716ec333a087cf35d9ec424

Modified Files
--------------
src/backend/optimizer/path/allpaths.c |   3 +-
src/backend/optimizer/path/joinrels.c |  21 +++++++
src/test/regress/expected/join.out    | 102 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql         |  44 +++++++++++++++
4 files changed, 169 insertions(+), 1 deletion(-)



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

* pgsql: Disallow joins whose lateral references can never be satisfied
@ 2026-10-14 05:34  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 10+ messages in thread

From: Richard Guo @ 2026-10-14 05:34 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Disallow joins whose lateral references can never be satisfied

If a proposed join has lateral references to rels outside it,
join_is_legal() checks that those rels can still be joined to from
outside.  To do that it collects the rels that must end up on the
inner side of an outer join with the proposed join, directly or
indirectly, and rejects the join if any of its lateral references
point to one of them.

That search followed only outer joins, from min_lefthand to
min_righthand.  It failed to notice that a rel that laterally
references the proposed join, or any rel found by the search, cannot
supply a parameter to the proposed join either, and that the same
holds for rels on the inner side of an outer join with such a rel.  As
a result, we could approve a join whose lateral references could never
be satisfied.

Such a join can never appear in a complete plan.  Worse, as the
existing comment warns, its apparent legality could mislead the
clauseless-join heuristics into thinking that some other join rel need
not be formed.  In particular, have_join_order_restriction() would not
force a clauseless outer join if one of its input rels appeared to
have a legal clause join elsewhere, and if that outer join was
required, we would fail with "failed to build any N-way joins".

To fix, also include in the search any input rel of the current join
search that laterally references a rel already found.  The input rels
are taken from root->initial_rels rather than from the query's base
rels, because an input can be the joinrel of a sub-joinlist, as
happens for a full join or when from_collapse_limit or
join_collapse_limit prevents flattening.  Such a joinrel is joined as
a unit, so it has to be included as a whole.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_p6iWbsjNcYpd6-6mWFnog3UJ2fZKrxWAQTsyNoyeQTw@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/9e8f4ce286bf03306fc0ad29374095782b2a8136

Modified Files
--------------
src/backend/optimizer/path/allpaths.c |   3 +-
src/backend/optimizer/path/joinrels.c |  21 +++++++
src/test/regress/expected/join.out    | 102 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql         |  44 +++++++++++++++
4 files changed, 169 insertions(+), 1 deletion(-)



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

* pgsql: Disallow joins whose lateral references can never be satisfied
@ 2026-10-14 05:34  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 10+ messages in thread

From: Richard Guo @ 2026-10-14 05:34 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Disallow joins whose lateral references can never be satisfied

If a proposed join has lateral references to rels outside it,
join_is_legal() checks that those rels can still be joined to from
outside.  To do that it collects the rels that must end up on the
inner side of an outer join with the proposed join, directly or
indirectly, and rejects the join if any of its lateral references
point to one of them.

That search followed only outer joins, from min_lefthand to
min_righthand.  It failed to notice that a rel that laterally
references the proposed join, or any rel found by the search, cannot
supply a parameter to the proposed join either, and that the same
holds for rels on the inner side of an outer join with such a rel.  As
a result, we could approve a join whose lateral references could never
be satisfied.

Such a join can never appear in a complete plan.  Worse, as the
existing comment warns, its apparent legality could mislead the
clauseless-join heuristics into thinking that some other join rel need
not be formed.  In particular, have_join_order_restriction() would not
force a clauseless outer join if one of its input rels appeared to
have a legal clause join elsewhere, and if that outer join was
required, we would fail with "failed to build any N-way joins".

To fix, also include in the search any input rel of the current join
search that laterally references a rel already found.  The input rels
are taken from root->initial_rels rather than from the query's base
rels, because an input can be the joinrel of a sub-joinlist, as
happens for a full join or when from_collapse_limit or
join_collapse_limit prevents flattening.  Such a joinrel is joined as
a unit, so it has to be included as a whole.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_p6iWbsjNcYpd6-6mWFnog3UJ2fZKrxWAQTsyNoyeQTw@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/333d566c4f51f1b8b002175032586423c581d3d6

Modified Files
--------------
src/backend/optimizer/path/allpaths.c |   3 +-
src/backend/optimizer/path/joinrels.c |  21 +++++++
src/test/regress/expected/join.out    | 102 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql         |  44 +++++++++++++++
4 files changed, 169 insertions(+), 1 deletion(-)



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

* pgsql: Disallow joins whose lateral references can never be satisfied
@ 2026-10-14 05:34  Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 10+ messages in thread

From: Richard Guo @ 2026-10-14 05:34 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Disallow joins whose lateral references can never be satisfied

If a proposed join has lateral references to rels outside it,
join_is_legal() checks that those rels can still be joined to from
outside.  To do that it collects the rels that must end up on the
inner side of an outer join with the proposed join, directly or
indirectly, and rejects the join if any of its lateral references
point to one of them.

That search followed only outer joins, from min_lefthand to
min_righthand.  It failed to notice that a rel that laterally
references the proposed join, or any rel found by the search, cannot
supply a parameter to the proposed join either, and that the same
holds for rels on the inner side of an outer join with such a rel.  As
a result, we could approve a join whose lateral references could never
be satisfied.

Such a join can never appear in a complete plan.  Worse, as the
existing comment warns, its apparent legality could mislead the
clauseless-join heuristics into thinking that some other join rel need
not be formed.  In particular, have_join_order_restriction() would not
force a clauseless outer join if one of its input rels appeared to
have a legal clause join elsewhere, and if that outer join was
required, we would fail with "failed to build any N-way joins".

To fix, also include in the search any input rel of the current join
search that laterally references a rel already found.  The input rels
are taken from root->initial_rels rather than from the query's base
rels, because an input can be the joinrel of a sub-joinlist, as
happens for a full join or when from_collapse_limit or
join_collapse_limit prevents flattening.  Such a joinrel is joined as
a unit, so it has to be included as a whole.

Back-patch to all supported branches.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_p6iWbsjNcYpd6-6mWFnog3UJ2fZKrxWAQTsyNoyeQTw@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/60b2a2f5264d9cb2e34b725453e3f8315dba5054

Modified Files
--------------
src/backend/optimizer/path/allpaths.c |   3 +-
src/backend/optimizer/path/joinrels.c |  21 +++++++
src/test/regress/expected/join.out    | 108 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql         |  44 ++++++++++++++
4 files changed, 175 insertions(+), 1 deletion(-)



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


end of thread, other threads:[~2026-10-14 05:34 UTC | newest]

Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-04 01:36 pgsql: Disallow joins whose lateral references need an unformable outer Richard Guo <rguo@postgresql.org>
2026-09-04 01:36 pgsql: Disallow joins whose lateral references need an unformable outer Richard Guo <rguo@postgresql.org>
2026-09-04 01:36 pgsql: Disallow joins whose lateral references need an unformable outer Richard Guo <rguo@postgresql.org>
2026-10-14 05:34 pgsql: Disallow joins whose lateral references can never be satisfied Richard Guo <rguo@postgresql.org>
2026-10-14 05:34 pgsql: Disallow joins whose lateral references can never be satisfied Richard Guo <rguo@postgresql.org>
2026-10-14 05:34 pgsql: Disallow joins whose lateral references can never be satisfied Richard Guo <rguo@postgresql.org>
2026-10-14 05:34 pgsql: Disallow joins whose lateral references can never be satisfied Richard Guo <rguo@postgresql.org>
2026-10-14 05:34 pgsql: Disallow joins whose lateral references can never be satisfied Richard Guo <rguo@postgresql.org>
2026-10-14 05:34 pgsql: Disallow joins whose lateral references can never be satisfied Richard Guo <rguo@postgresql.org>
2026-10-14 05:34 pgsql: Disallow joins whose lateral references can never be satisfied 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