pg.ddx.io pgsql-committers@postgresql.org mailing list archivehelp / color / mirror / Atom feed
pgsql: Give antijoins made from sublinks a relid 5+ messages / 1 participants [nested] [flat]
* pgsql: Give antijoins made from sublinks a relid @ 2026-10-14 03:08 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 5+ messages in thread From: Richard Guo @ 2026-10-14 03:08 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Give antijoins made from sublinks a relid When pull_up_sublinks() converts a sublink to an antijoin, it builds a JoinExpr with no RTE, on the grounds that nothing above the join can reference its RHS. That overlooks PlaceHolderVars. A PHV with no Vars of its own is evaluated at its phrels, and for the outputs of a pulled-up subquery phrels is all the relids in the subquery's jointree, including the RHS of such a join. A qual using the PHV thus appears to reference the RHS rels, and can be pushed down into the RHS as a parameterized join clause. There it has the opposite of the intended effect: outer rows that fail the qual find no match and are emitted, giving wrong results. For an outer join, including an antijoin made by reducing a left join, this cannot happen because the join's relid is among the qual's relids. Before v16, the qual's nullable_relids prevented it. To fix, make a join RTE for an antijoin built from a sublink, so that it has a relid like any other outer join. Semijoins are left as they are: a qual evaluated within a semijoin's RHS does not change the result. Back-patch to v16. Before that, the qual's nullable_relids included the antijoin's RHS, which kept the qual from being moved there; that protection went away when nullable_relids was replaced by outer-join relids. Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com> Discussion: https://postgr.es/m/CAMbWs4_rEdcG89BgC-AO7b+Bf2z8w3976+4zP==Urt=CXB1hsg@mail.gmail.com Backpatch-through: 16 Branch ------ REL_17_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/854f81f07c7dbd16189d6cd381eb53360f0a7b03 Modified Files -------------- src/backend/optimizer/plan/initsplan.c | 21 +++++++-------- src/backend/optimizer/plan/subselect.c | 48 +++++++++++++++++++++++++++++++++- src/include/nodes/pathnodes.h | 3 +-- src/include/nodes/primnodes.h | 5 ++-- src/test/regress/expected/join.out | 29 ++++++++++++++++++++ src/test/regress/sql/join.sql | 18 +++++++++++++ 6 files changed, 107 insertions(+), 17 deletions(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Give antijoins made from sublinks a relid @ 2026-10-14 03:08 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 5+ messages in thread From: Richard Guo @ 2026-10-14 03:08 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Give antijoins made from sublinks a relid When pull_up_sublinks() converts a sublink to an antijoin, it builds a JoinExpr with no RTE, on the grounds that nothing above the join can reference its RHS. That overlooks PlaceHolderVars. A PHV with no Vars of its own is evaluated at its phrels, and for the outputs of a pulled-up subquery phrels is all the relids in the subquery's jointree, including the RHS of such a join. A qual using the PHV thus appears to reference the RHS rels, and can be pushed down into the RHS as a parameterized join clause. There it has the opposite of the intended effect: outer rows that fail the qual find no match and are emitted, giving wrong results. For an outer join, including an antijoin made by reducing a left join, this cannot happen because the join's relid is among the qual's relids. Before v16, the qual's nullable_relids prevented it. To fix, make a join RTE for an antijoin built from a sublink, so that it has a relid like any other outer join. Semijoins are left as they are: a qual evaluated within a semijoin's RHS does not change the result. Back-patch to v16. Before that, the qual's nullable_relids included the antijoin's RHS, which kept the qual from being moved there; that protection went away when nullable_relids was replaced by outer-join relids. Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com> Discussion: https://postgr.es/m/CAMbWs4_rEdcG89BgC-AO7b+Bf2z8w3976+4zP==Urt=CXB1hsg@mail.gmail.com Backpatch-through: 16 Branch ------ REL_16_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/ca4273866be4a5f7ab1420fd123aa97f2588363d Modified Files -------------- src/backend/optimizer/plan/initsplan.c | 21 +++++++-------- src/backend/optimizer/plan/subselect.c | 48 +++++++++++++++++++++++++++++++++- src/include/nodes/pathnodes.h | 3 +-- src/include/nodes/primnodes.h | 5 ++-- src/test/regress/expected/join.out | 29 ++++++++++++++++++++ src/test/regress/sql/join.sql | 18 +++++++++++++ 6 files changed, 107 insertions(+), 17 deletions(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Give antijoins made from sublinks a relid @ 2026-10-14 03:10 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 5+ messages in thread From: Richard Guo @ 2026-10-14 03:10 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Give antijoins made from sublinks a relid When pull_up_sublinks() converts a sublink to an antijoin, it builds a JoinExpr with no RTE, on the grounds that nothing above the join can reference its RHS. That overlooks PlaceHolderVars. A PHV with no Vars of its own is evaluated at its phrels, and for the outputs of a pulled-up subquery phrels is all the relids in the subquery's jointree, including the RHS of such a join. A qual using the PHV thus appears to reference the RHS rels, and can be pushed down into the RHS as a parameterized join clause. There it has the opposite of the intended effect: outer rows that fail the qual find no match and are emitted, giving wrong results. For an outer join, including an antijoin made by reducing a left join, this cannot happen because the join's relid is among the qual's relids. Before v16, the qual's nullable_relids prevented it. To fix, make a join RTE for an antijoin built from a sublink, so that it has a relid like any other outer join. Semijoins are left as they are: a qual evaluated within a semijoin's RHS does not change the result. Back-patch to v16. Before that, the qual's nullable_relids included the antijoin's RHS, which kept the qual from being moved there; that protection went away when nullable_relids was replaced by outer-join relids. Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com> Discussion: https://postgr.es/m/CAMbWs4_rEdcG89BgC-AO7b+Bf2z8w3976+4zP==Urt=CXB1hsg@mail.gmail.com Backpatch-through: 16 Branch ------ REL_18_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/64fb20a52394a8f5a81bcff7bb39e0a1cde7d0b8 Modified Files -------------- src/backend/optimizer/plan/initsplan.c | 21 +++++++-------- src/backend/optimizer/plan/subselect.c | 48 +++++++++++++++++++++++++++++++++- src/include/nodes/pathnodes.h | 3 +-- src/include/nodes/primnodes.h | 5 ++-- src/test/regress/expected/join.out | 29 ++++++++++++++++++++ src/test/regress/sql/join.sql | 18 +++++++++++++ 6 files changed, 107 insertions(+), 17 deletions(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Give antijoins made from sublinks a relid @ 2026-10-14 03:11 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 5+ messages in thread From: Richard Guo @ 2026-10-14 03:11 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Give antijoins made from sublinks a relid When pull_up_sublinks() converts a sublink to an antijoin, it builds a JoinExpr with no RTE, on the grounds that nothing above the join can reference its RHS. That overlooks PlaceHolderVars. A PHV with no Vars of its own is evaluated at its phrels, and for the outputs of a pulled-up subquery phrels is all the relids in the subquery's jointree, including the RHS of such a join. A qual using the PHV thus appears to reference the RHS rels, and can be pushed down into the RHS as a parameterized join clause. There it has the opposite of the intended effect: outer rows that fail the qual find no match and are emitted, giving wrong results. For an outer join, including an antijoin made by reducing a left join, this cannot happen because the join's relid is among the qual's relids. Before v16, the qual's nullable_relids prevented it. To fix, make a join RTE for an antijoin built from a sublink, so that it has a relid like any other outer join. Semijoins are left as they are: a qual evaluated within a semijoin's RHS does not change the result. Back-patch to v16. Before that, the qual's nullable_relids included the antijoin's RHS, which kept the qual from being moved there; that protection went away when nullable_relids was replaced by outer-join relids. Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com> Discussion: https://postgr.es/m/CAMbWs4_rEdcG89BgC-AO7b+Bf2z8w3976+4zP==Urt=CXB1hsg@mail.gmail.com Backpatch-through: 16 Branch ------ REL_19_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/9d7d4424c1ee14a9ad6b607b9b8f086207eed910 Modified Files -------------- src/backend/optimizer/README | 8 ++--- src/backend/optimizer/plan/initsplan.c | 21 +++++------- src/backend/optimizer/plan/subselect.c | 51 +++++++++++++++++++++++++-- src/backend/optimizer/util/relnode.c | 8 ++--- src/include/nodes/pathnodes.h | 3 +- src/include/nodes/primnodes.h | 5 +-- src/test/regress/expected/join.out | 63 ++++++++++++++++++++++++++++++++++ src/test/regress/sql/join.sql | 43 +++++++++++++++++++++++ 8 files changed, 174 insertions(+), 28 deletions(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Give antijoins made from sublinks a relid @ 2026-10-14 03:12 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 5+ messages in thread From: Richard Guo @ 2026-10-14 03:12 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Give antijoins made from sublinks a relid When pull_up_sublinks() converts a sublink to an antijoin, it builds a JoinExpr with no RTE, on the grounds that nothing above the join can reference its RHS. That overlooks PlaceHolderVars. A PHV with no Vars of its own is evaluated at its phrels, and for the outputs of a pulled-up subquery phrels is all the relids in the subquery's jointree, including the RHS of such a join. A qual using the PHV thus appears to reference the RHS rels, and can be pushed down into the RHS as a parameterized join clause. There it has the opposite of the intended effect: outer rows that fail the qual find no match and are emitted, giving wrong results. For an outer join, including an antijoin made by reducing a left join, this cannot happen because the join's relid is among the qual's relids. Before v16, the qual's nullable_relids prevented it. To fix, make a join RTE for an antijoin built from a sublink, so that it has a relid like any other outer join. Semijoins are left as they are: a qual evaluated within a semijoin's RHS does not change the result. Back-patch to v16. Before that, the qual's nullable_relids included the antijoin's RHS, which kept the qual from being moved there; that protection went away when nullable_relids was replaced by outer-join relids. Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com> Discussion: https://postgr.es/m/CAMbWs4_rEdcG89BgC-AO7b+Bf2z8w3976+4zP==Urt=CXB1hsg@mail.gmail.com Backpatch-through: 16 Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/0d6103e537e1299046e5906149f9a7d2108be2cf Modified Files -------------- src/backend/optimizer/README | 8 ++--- src/backend/optimizer/plan/initsplan.c | 21 +++++------- src/backend/optimizer/plan/subselect.c | 51 +++++++++++++++++++++++++-- src/backend/optimizer/util/relnode.c | 8 ++--- src/include/nodes/pathnodes.h | 3 +- src/include/nodes/primnodes.h | 5 +-- src/test/regress/expected/join.out | 63 ++++++++++++++++++++++++++++++++++ src/test/regress/sql/join.sql | 43 +++++++++++++++++++++++ 8 files changed, 174 insertions(+), 28 deletions(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2026-10-14 03:12 UTC | newest] Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2026-10-14 03:08 pgsql: Give antijoins made from sublinks a relid Richard Guo <rguo@postgresql.org> 2026-10-14 03:08 pgsql: Give antijoins made from sublinks a relid Richard Guo <rguo@postgresql.org> 2026-10-14 03:10 pgsql: Give antijoins made from sublinks a relid Richard Guo <rguo@postgresql.org> 2026-10-14 03:11 pgsql: Give antijoins made from sublinks a relid Richard Guo <rguo@postgresql.org> 2026-10-14 03:12 pgsql: Give antijoins made from sublinks a relid 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