Commit d3701bc
committed
Avoid pushing quals down into sub-queries that have grouping sets.
The trouble with doing this is that an apparently-constant subquery
output column isn't really constant if it is a grouping column that
appears in only some of the grouping sets. A qual using such a
column would be subject to incorrect const-folding after push-down,
as seen in bug #16585 from Paul Sivash.
To fix, just disable qual pushdown altogether if the sub-query has
nonempty groupingSets. While we could imagine far less restrictive
solutions, there is not much point in working harder right now,
because subquery_planner() won't move HAVING clauses to WHERE within
such a subquery. If the qual stays in HAVING it's not going to be
a lot more useful than if we'd kept it at the outer level.
Having said that, this restriction could be removed if we used a
parsetree representation that distinguished such outputs from actual
constants, which is something I hope to do in future. Hence, make
the patch a minimal addition rather than integrating it more tightly
(e.g. by renumbering the existing items in subquery_is_pushdown_safe's
comment).
Back-patch to 9.5 where grouping sets were introduced.
Discussion: https://postgr.es/m/16585-9d8c340d23ade8c1@postgresql.org1 parent 190fb57 commit d3701bcCopy full SHA for d3701bc
File tree
Expand file treeCollapse file tree
3 files changed
+63
-0
lines changedOpen diff view settings
Filter options
- src
- backend/optimizer/path
- test/regress
- expected
- sql
Expand file treeCollapse file tree
3 files changed
+63
-0
lines changedOpen diff view settings
Collapse file
src/backend/optimizer/path/allpaths.c
Copy file name to clipboardExpand all lines: src/backend/optimizer/path/allpaths.c+15Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
2278 | 2278 | |
2279 | 2279 | |
2280 | 2280 | |
| 2281 | + |
| 2282 | + |
| 2283 | + |
| 2284 | + |
| 2285 | + |
| 2286 | + |
| 2287 | + |
| 2288 | + |
| 2289 | + |
| 2290 | + |
| 2291 | + |
2281 | 2292 | |
2282 | 2293 | |
2283 | 2294 | |
| ||
2322 | 2333 | |
2323 | 2334 | |
2324 | 2335 | |
| 2336 | + |
| 2337 | + |
| 2338 | + |
| 2339 | + |
2325 | 2340 | |
2326 | 2341 | |
2327 | 2342 | |
|
Collapse file
src/test/regress/expected/groupingsets.out
Copy file name to clipboardExpand all lines: src/test/regress/expected/groupingsets.out+32Lines changed: 32 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
426 | 426 | |
427 | 427 | |
428 | 428 | |
| 429 | + |
| 430 | + |
| 431 | + |
| 432 | + |
| 433 | + |
| 434 | + |
| 435 | + |
| 436 | + |
| 437 | + |
| 438 | + |
| 439 | + |
| 440 | + |
| 441 | + |
| 442 | + |
| 443 | + |
| 444 | + |
| 445 | + |
| 446 | + |
| 447 | + |
| 448 | + |
| 449 | + |
| 450 | + |
| 451 | + |
| 452 | + |
| 453 | + |
| 454 | + |
| 455 | + |
| 456 | + |
| 457 | + |
| 458 | + |
| 459 | + |
| 460 | + |
429 | 461 | |
430 | 462 | |
431 | 463 | |
|
Collapse file
src/test/regress/sql/groupingsets.sql
Copy file name to clipboardExpand all lines: src/test/regress/sql/groupingsets.sql+16Lines changed: 16 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
161 | 161 | |
162 | 162 | |
163 | 163 | |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
164 | 180 | |
165 | 181 | |
166 | 182 | |
|
0 commit comments