public inbox for [email protected]
help / color / mirror / Atom feedpgsql: Fix incorrect handling of subquery pullup in the presence of gro
5+ messages / 2 participants
[nested] [flat]
* pgsql: Fix incorrect handling of subquery pullup in the presence of gro
@ 2018-01-12 17:25 Tom Lane <[email protected]>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2018-01-12 17:25 UTC (permalink / raw)
To: pgsql-committers
Fix incorrect handling of subquery pullup in the presence of grouping sets.
If we flatten a subquery whose target list contains constants or
expressions, when those output columns are used in GROUPING SET columns,
the planner was capable of doing the wrong thing by merging a pulled-up
expression into the surrounding expression during const-simplification.
Then the late processing that attempts to match subexpressions to grouping
sets would fail to match those subexpressions to grouping sets, with the
effect that they'd not go to null when expected.
To fix, wrap such subquery outputs in PlaceHolderVars, ensuring that
they preserve their separate identity throughout the planner's expression
processing. This is a bit of a band-aid, because the wrapper defeats
const-simplification even in places where it would be safe to allow.
But a nicer fix would likely be too invasive to back-patch, and the
consequences of the missed optimizations probably aren't large in most
cases.
Back-patch to 9.5 where grouping sets were introduced.
Heikki Linnakangas, with small mods and better test cases by me;
additional review by Andrew Gierth
Discussion: https://postgr.es/m/[email protected]
Branch
------
REL_10_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/d3ca1a6c3721b910fa8c05f41397727a53409fd3
Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 48 +++++++++++++++++++++++++-----
src/test/regress/expected/groupingsets.out | 45 ++++++++++++++++++++++++++++
src/test/regress/sql/groupingsets.sql | 20 +++++++++++++
3 files changed, 105 insertions(+), 8 deletions(-)
^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Fix incorrect handling of subquery pullup in the presence of gro
@ 2018-01-12 17:25 Tom Lane <[email protected]>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2018-01-12 17:25 UTC (permalink / raw)
To: pgsql-committers
Fix incorrect handling of subquery pullup in the presence of grouping sets.
If we flatten a subquery whose target list contains constants or
expressions, when those output columns are used in GROUPING SET columns,
the planner was capable of doing the wrong thing by merging a pulled-up
expression into the surrounding expression during const-simplification.
Then the late processing that attempts to match subexpressions to grouping
sets would fail to match those subexpressions to grouping sets, with the
effect that they'd not go to null when expected.
To fix, wrap such subquery outputs in PlaceHolderVars, ensuring that
they preserve their separate identity throughout the planner's expression
processing. This is a bit of a band-aid, because the wrapper defeats
const-simplification even in places where it would be safe to allow.
But a nicer fix would likely be too invasive to back-patch, and the
consequences of the missed optimizations probably aren't large in most
cases.
Back-patch to 9.5 where grouping sets were introduced.
Heikki Linnakangas, with small mods and better test cases by me;
additional review by Andrew Gierth
Discussion: https://postgr.es/m/[email protected]
Branch
------
REL9_6_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/6520d4a9692882a7d5233dac40c4b7ff07f55049
Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 48 +++++++++++++++++++++++++-----
src/test/regress/expected/groupingsets.out | 45 ++++++++++++++++++++++++++++
src/test/regress/sql/groupingsets.sql | 20 +++++++++++++
3 files changed, 105 insertions(+), 8 deletions(-)
^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Fix incorrect handling of subquery pullup in the presence of gro
@ 2018-01-12 17:25 Tom Lane <[email protected]>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2018-01-12 17:25 UTC (permalink / raw)
To: pgsql-committers
Fix incorrect handling of subquery pullup in the presence of grouping sets.
If we flatten a subquery whose target list contains constants or
expressions, when those output columns are used in GROUPING SET columns,
the planner was capable of doing the wrong thing by merging a pulled-up
expression into the surrounding expression during const-simplification.
Then the late processing that attempts to match subexpressions to grouping
sets would fail to match those subexpressions to grouping sets, with the
effect that they'd not go to null when expected.
To fix, wrap such subquery outputs in PlaceHolderVars, ensuring that
they preserve their separate identity throughout the planner's expression
processing. This is a bit of a band-aid, because the wrapper defeats
const-simplification even in places where it would be safe to allow.
But a nicer fix would likely be too invasive to back-patch, and the
consequences of the missed optimizations probably aren't large in most
cases.
Back-patch to 9.5 where grouping sets were introduced.
Heikki Linnakangas, with small mods and better test cases by me;
additional review by Andrew Gierth
Discussion: https://postgr.es/m/[email protected]
Branch
------
REL9_5_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/ff99d7761aa4f1943689dcd6ec7a5759f7fdfabc
Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 48 +++++++++++++++++++++++++-----
src/test/regress/expected/groupingsets.out | 45 ++++++++++++++++++++++++++++
src/test/regress/sql/groupingsets.sql | 20 +++++++++++++
3 files changed, 105 insertions(+), 8 deletions(-)
^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Fix incorrect handling of subquery pullup in the presence of gro
@ 2018-01-12 17:25 Tom Lane <[email protected]>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2018-01-12 17:25 UTC (permalink / raw)
To: pgsql-committers
Fix incorrect handling of subquery pullup in the presence of grouping sets.
If we flatten a subquery whose target list contains constants or
expressions, when those output columns are used in GROUPING SET columns,
the planner was capable of doing the wrong thing by merging a pulled-up
expression into the surrounding expression during const-simplification.
Then the late processing that attempts to match subexpressions to grouping
sets would fail to match those subexpressions to grouping sets, with the
effect that they'd not go to null when expected.
To fix, wrap such subquery outputs in PlaceHolderVars, ensuring that
they preserve their separate identity throughout the planner's expression
processing. This is a bit of a band-aid, because the wrapper defeats
const-simplification even in places where it would be safe to allow.
But a nicer fix would likely be too invasive to back-patch, and the
consequences of the missed optimizations probably aren't large in most
cases.
Back-patch to 9.5 where grouping sets were introduced.
Heikki Linnakangas, with small mods and better test cases by me;
additional review by Andrew Gierth
Discussion: https://postgr.es/m/[email protected]
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/90947674fc984f5639e3b1bf013435a023aa713b
Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 48 +++++++++++++++++++++++++-----
src/test/regress/expected/groupingsets.out | 45 ++++++++++++++++++++++++++++
src/test/regress/sql/groupingsets.sql | 20 +++++++++++++
3 files changed, 105 insertions(+), 8 deletions(-)
^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Fix incorrect handling of subquery pullup
@ 2025-03-13 07:36 Richard Guo <[email protected]>
0 siblings, 0 replies; 5+ messages in thread
From: Richard Guo @ 2025-03-13 07:36 UTC (permalink / raw)
To: [email protected]
Fix incorrect handling of subquery pullup
When pulling up a subquery, if the subquery's target list items are
used in grouping set columns, we need to wrap them in PlaceHolderVars.
This ensures that expressions retain their separate identity so that
they will match grouping set columns when appropriate.
In 90947674f, we decided to wrap subquery outputs that are non-var
expressions in PlaceHolderVars. This prevents const-simplification
from merging them into the surrounding expressions after subquery
pullup, which could otherwise lead to failing to match those
subexpressions to grouping set columns, with the effect that they'd
not go to null when expected.
However, that left some loose ends. If the subquery's target list
contains two or more identical Var expressions, we can still fail to
match the Var expression to the expected grouping set expression.
This is not related to const-simplification, but rather to how we
match expressions to lower target items in setrefs.c.
For sort/group expressions, we use ressortgroupref matching, which
works well. For other expressions, we primarily rely on comparing the
expressions to determine if they are the same. Therefore, we need a
way to prevent setrefs.c from matching the expression to some other
identical ones.
To fix, wrap all subquery outputs in PlaceHolderVars if the parent
query uses grouping sets, ensuring that they preserve their separate
identity throughout the whole planning process.
Reported-by: Dean Rasheed <[email protected]>
Author: Richard Guo <[email protected]>
Reviewed-by: Dean Rasheed <[email protected]>
Discussion: https://postgr.es/m/CAMbWs4-meSahaanKskpBn0KKxdHAXC1_EJCVWHxEodqirrGJnw@mail.gmail.com
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/cc5d98525d43c22b98f360ef0f2c8d7dc57f04dc
Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 93 ++++++++++++++++--------------
src/backend/rewrite/rewriteManip.c | 2 +-
src/test/regress/expected/groupingsets.out | 29 ++++++++++
src/test/regress/sql/groupingsets.sql | 11 ++++
src/tools/pgindent/typedefs.list | 1 +
5 files changed, 93 insertions(+), 43 deletions(-)
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2025-03-13 07:36 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-01-12 17:25 pgsql: Fix incorrect handling of subquery pullup in the presence of gro Tom Lane <[email protected]>
2018-01-12 17:25 pgsql: Fix incorrect handling of subquery pullup in the presence of gro Tom Lane <[email protected]>
2018-01-12 17:25 pgsql: Fix incorrect handling of subquery pullup in the presence of gro Tom Lane <[email protected]>
2018-01-12 17:25 pgsql: Fix incorrect handling of subquery pullup in the presence of gro Tom Lane <[email protected]>
2025-03-13 07:36 pgsql: Fix incorrect handling of subquery pullup Richard Guo <[email protected]>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox