pg.ddx.io pgsql-committers@postgresql.org mailing list archivehelp / color / mirror / Atom feed
pgsql: Don't replace clone clauses with constant TRUE 5+ messages / 1 participants [nested] [flat]
* pgsql: Don't replace clone clauses with constant TRUE @ 2026-10-29 02:30 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 5+ messages in thread From: Richard Guo @ 2026-10-29 02:30 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Don't replace clone clauses with constant TRUE When reconsider_outer_join_clauses() finds an outer-join clause OUTERVAR = INNERVAR whose OUTERVAR is known to equal a constant, it pushes INNERVAR = CONSTANT into the inner side and replaces the clause with constant TRUE. The replacement assumes that OUTERVAR cannot have gone to NULL where the clause is evaluated. That assumption fails for a clone made for outer join identity 3. When an outer join is pushed into the RHS of a lower outer join, its relid is not added to the joinrel until the commuted pair is complete, so a version of a clause that lacks that join's nulling bit can be applied above it, after it has null-extended OUTERVAR. This is fine for the original clause, because it is strict, which is what allowed the commutation in the first place; but it is not fine for constant TRUE. As a result, inner rows could be joined to null-extended outer rows, producing wrong query results. To fix, throw back a clone as-is rather than the TRUE replacement, as v15 and earlier did. The derived INNERVAR = CONSTANT clause remains valid and is still pushed down. This can leave a redundant join filter in some plans, as seen in an existing regression test. To keep the thrown-back clause from affecting selectivity estimates, including for parameterized scans, where the cached selectivity is not used, restore the redundancy marker removed by b448f1c8d: a norm_selec greater than 1 marks a redundant clause, for which clause_selectivity_ext() always returns 1.0. Back-patch to v16, where clone clauses were introduced. Bug: #19765 Reported-by: Viktor Leis <leis@in.tum.de> Diagnosed-by: Tender Wang <tndrwang@gmail.com> Author: Richard Guo <guofenglinux@gmail.com> Co-authored-by: Nitin Motiani <nitinmotiani@google.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Discussion: https://postgr.es/m/19765-5778b18918040583@postgresql.org Backpatch-through: 16 Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/f400289946f7d0e66a4169cf4aa56079f1422a2b Modified Files -------------- src/backend/optimizer/path/clausesel.c | 6 +++ src/backend/optimizer/path/equivclass.c | 24 ++++++++++++ src/include/nodes/pathnodes.h | 5 ++- src/test/regress/expected/join.out | 68 ++++++++++++++++++++++++++++++++- src/test/regress/sql/join.sql | 24 ++++++++++++ 5 files changed, 125 insertions(+), 2 deletions(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Don't replace clone clauses with constant TRUE @ 2026-10-29 02:30 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 5+ messages in thread From: Richard Guo @ 2026-10-29 02:30 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Don't replace clone clauses with constant TRUE When reconsider_outer_join_clauses() finds an outer-join clause OUTERVAR = INNERVAR whose OUTERVAR is known to equal a constant, it pushes INNERVAR = CONSTANT into the inner side and replaces the clause with constant TRUE. The replacement assumes that OUTERVAR cannot have gone to NULL where the clause is evaluated. That assumption fails for a clone made for outer join identity 3. When an outer join is pushed into the RHS of a lower outer join, its relid is not added to the joinrel until the commuted pair is complete, so a version of a clause that lacks that join's nulling bit can be applied above it, after it has null-extended OUTERVAR. This is fine for the original clause, because it is strict, which is what allowed the commutation in the first place; but it is not fine for constant TRUE. As a result, inner rows could be joined to null-extended outer rows, producing wrong query results. To fix, throw back a clone as-is rather than the TRUE replacement, as v15 and earlier did. The derived INNERVAR = CONSTANT clause remains valid and is still pushed down. This can leave a redundant join filter in some plans, as seen in an existing regression test. To keep the thrown-back clause from affecting selectivity estimates, including for parameterized scans, where the cached selectivity is not used, restore the redundancy marker removed by b448f1c8d: a norm_selec greater than 1 marks a redundant clause, for which clause_selectivity_ext() always returns 1.0. Back-patch to v16, where clone clauses were introduced. Bug: #19765 Reported-by: Viktor Leis <leis@in.tum.de> Diagnosed-by: Tender Wang <tndrwang@gmail.com> Author: Richard Guo <guofenglinux@gmail.com> Co-authored-by: Nitin Motiani <nitinmotiani@google.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Discussion: https://postgr.es/m/19765-5778b18918040583@postgresql.org Backpatch-through: 16 Branch ------ REL_19_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/4567d170f87bedc577ffe8e934f44db00dbdae7f Modified Files -------------- src/backend/optimizer/path/clausesel.c | 6 +++ src/backend/optimizer/path/equivclass.c | 24 ++++++++++++ src/include/nodes/pathnodes.h | 5 ++- src/test/regress/expected/join.out | 68 ++++++++++++++++++++++++++++++++- src/test/regress/sql/join.sql | 24 ++++++++++++ 5 files changed, 125 insertions(+), 2 deletions(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Don't replace clone clauses with constant TRUE @ 2026-10-29 02:30 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 5+ messages in thread From: Richard Guo @ 2026-10-29 02:30 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Don't replace clone clauses with constant TRUE When reconsider_outer_join_clauses() finds an outer-join clause OUTERVAR = INNERVAR whose OUTERVAR is known to equal a constant, it pushes INNERVAR = CONSTANT into the inner side and replaces the clause with constant TRUE. The replacement assumes that OUTERVAR cannot have gone to NULL where the clause is evaluated. That assumption fails for a clone made for outer join identity 3. When an outer join is pushed into the RHS of a lower outer join, its relid is not added to the joinrel until the commuted pair is complete, so a version of a clause that lacks that join's nulling bit can be applied above it, after it has null-extended OUTERVAR. This is fine for the original clause, because it is strict, which is what allowed the commutation in the first place; but it is not fine for constant TRUE. As a result, inner rows could be joined to null-extended outer rows, producing wrong query results. To fix, throw back a clone as-is rather than the TRUE replacement, as v15 and earlier did. The derived INNERVAR = CONSTANT clause remains valid and is still pushed down. This can leave a redundant join filter in some plans, as seen in an existing regression test. To keep the thrown-back clause from affecting selectivity estimates, including for parameterized scans, where the cached selectivity is not used, restore the redundancy marker removed by b448f1c8d: a norm_selec greater than 1 marks a redundant clause, for which clause_selectivity_ext() always returns 1.0. Back-patch to v16, where clone clauses were introduced. Bug: #19765 Reported-by: Viktor Leis <leis@in.tum.de> Diagnosed-by: Tender Wang <tndrwang@gmail.com> Author: Richard Guo <guofenglinux@gmail.com> Co-authored-by: Nitin Motiani <nitinmotiani@google.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Discussion: https://postgr.es/m/19765-5778b18918040583@postgresql.org Backpatch-through: 16 Branch ------ REL_18_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/27a53c857394c43acc24b52018d3905189db78c6 Modified Files -------------- src/backend/optimizer/path/clausesel.c | 6 +++ src/backend/optimizer/path/equivclass.c | 24 ++++++++++++ src/include/nodes/pathnodes.h | 5 ++- src/test/regress/expected/join.out | 68 ++++++++++++++++++++++++++++++++- src/test/regress/sql/join.sql | 24 ++++++++++++ 5 files changed, 125 insertions(+), 2 deletions(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Don't replace clone clauses with constant TRUE @ 2026-10-29 02:30 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 5+ messages in thread From: Richard Guo @ 2026-10-29 02:30 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Don't replace clone clauses with constant TRUE When reconsider_outer_join_clauses() finds an outer-join clause OUTERVAR = INNERVAR whose OUTERVAR is known to equal a constant, it pushes INNERVAR = CONSTANT into the inner side and replaces the clause with constant TRUE. The replacement assumes that OUTERVAR cannot have gone to NULL where the clause is evaluated. That assumption fails for a clone made for outer join identity 3. When an outer join is pushed into the RHS of a lower outer join, its relid is not added to the joinrel until the commuted pair is complete, so a version of a clause that lacks that join's nulling bit can be applied above it, after it has null-extended OUTERVAR. This is fine for the original clause, because it is strict, which is what allowed the commutation in the first place; but it is not fine for constant TRUE. As a result, inner rows could be joined to null-extended outer rows, producing wrong query results. To fix, throw back a clone as-is rather than the TRUE replacement, as v15 and earlier did. The derived INNERVAR = CONSTANT clause remains valid and is still pushed down. This can leave a redundant join filter in some plans, as seen in an existing regression test. To keep the thrown-back clause from affecting selectivity estimates, including for parameterized scans, where the cached selectivity is not used, restore the redundancy marker removed by b448f1c8d: a norm_selec greater than 1 marks a redundant clause, for which clause_selectivity_ext() always returns 1.0. Back-patch to v16, where clone clauses were introduced. Bug: #19765 Reported-by: Viktor Leis <leis@in.tum.de> Diagnosed-by: Tender Wang <tndrwang@gmail.com> Author: Richard Guo <guofenglinux@gmail.com> Co-authored-by: Nitin Motiani <nitinmotiani@google.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Discussion: https://postgr.es/m/19765-5778b18918040583@postgresql.org Backpatch-through: 16 Branch ------ REL_17_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/ba7127f240c03f08e888b1b9a228774210fbcf4f Modified Files -------------- src/backend/optimizer/path/clausesel.c | 6 +++ src/backend/optimizer/path/equivclass.c | 24 ++++++++++++ src/include/nodes/pathnodes.h | 5 ++- src/test/regress/expected/join.out | 68 ++++++++++++++++++++++++++++++++- src/test/regress/sql/join.sql | 24 ++++++++++++ 5 files changed, 125 insertions(+), 2 deletions(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Don't replace clone clauses with constant TRUE @ 2026-10-29 02:30 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 5+ messages in thread From: Richard Guo @ 2026-10-29 02:30 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Don't replace clone clauses with constant TRUE When reconsider_outer_join_clauses() finds an outer-join clause OUTERVAR = INNERVAR whose OUTERVAR is known to equal a constant, it pushes INNERVAR = CONSTANT into the inner side and replaces the clause with constant TRUE. The replacement assumes that OUTERVAR cannot have gone to NULL where the clause is evaluated. That assumption fails for a clone made for outer join identity 3. When an outer join is pushed into the RHS of a lower outer join, its relid is not added to the joinrel until the commuted pair is complete, so a version of a clause that lacks that join's nulling bit can be applied above it, after it has null-extended OUTERVAR. This is fine for the original clause, because it is strict, which is what allowed the commutation in the first place; but it is not fine for constant TRUE. As a result, inner rows could be joined to null-extended outer rows, producing wrong query results. To fix, throw back a clone as-is rather than the TRUE replacement, as v15 and earlier did. The derived INNERVAR = CONSTANT clause remains valid and is still pushed down. This can leave a redundant join filter in some plans, as seen in an existing regression test. To keep the thrown-back clause from affecting selectivity estimates, including for parameterized scans, where the cached selectivity is not used, restore the redundancy marker removed by b448f1c8d: a norm_selec greater than 1 marks a redundant clause, for which clause_selectivity_ext() always returns 1.0. Back-patch to v16, where clone clauses were introduced. Bug: #19765 Reported-by: Viktor Leis <leis@in.tum.de> Diagnosed-by: Tender Wang <tndrwang@gmail.com> Author: Richard Guo <guofenglinux@gmail.com> Co-authored-by: Nitin Motiani <nitinmotiani@google.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Discussion: https://postgr.es/m/19765-5778b18918040583@postgresql.org Backpatch-through: 16 Branch ------ REL_16_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/78bc3b96cce6882b988b3099c6a86aa1aa99ab25 Modified Files -------------- src/backend/optimizer/path/clausesel.c | 6 +++ src/backend/optimizer/path/equivclass.c | 24 ++++++++++++ src/include/nodes/pathnodes.h | 5 ++- src/test/regress/expected/join.out | 68 ++++++++++++++++++++++++++++++++- src/test/regress/sql/join.sql | 24 ++++++++++++ 5 files changed, 125 insertions(+), 2 deletions(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2026-10-29 02:30 UTC | newest] Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2026-10-29 02:30 pgsql: Don't replace clone clauses with constant TRUE Richard Guo <rguo@postgresql.org> 2026-10-29 02:30 pgsql: Don't replace clone clauses with constant TRUE Richard Guo <rguo@postgresql.org> 2026-10-29 02:30 pgsql: Don't replace clone clauses with constant TRUE Richard Guo <rguo@postgresql.org> 2026-10-29 02:30 pgsql: Don't replace clone clauses with constant TRUE Richard Guo <rguo@postgresql.org> 2026-10-29 02:30 pgsql: Don't replace clone clauses with constant TRUE 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