- Notifications
You must be signed in to change notification settings - Fork4.9k
Commit065ce49
committed
Fix issue with ORDER BY / DISTINCT aggregates and FILTER
1349d27 added support so that aggregate functions with an ORDER BY orDISTINCT clause could make use of presorted inputs to avoid an implicitsort within nodeAgg.c. That commit failed to consider that a FILTERclause may exist that filters rows before the aggregate functionarguments are evaluated. That can be problematic if an aggregateargument contains an expression which could error out during evaluation.It's perfectly valid to want to have a FILTER clause which eliminatessuch values, and with the pre-sorted path added in1349d27, it waspossible that the planner would produce a plan with a Sort node abovethe Aggregate to perform the sort on the aggregate's arguments long beforethe Aggregate node would filter out the non-matching values.Here we fix this by inspecting ORDER BY / DISTINCT aggregate functionswhich have a FILTER clause to see if the aggregate's arguments areanything more complex than a Var or a Const. Evaluating these isn'tgoing to cause an error. If we find any non-Var, non-Const parametersthen the planner will now opt to perform the sort in the Aggregate nodefor these aggregates, i.e. disable the presorted aggregate optimization.An alternative fix would have been to completely disallow the presortedoptimization for Aggrefs with any FILTER clause, but that wasn't done asthat could cause large performance regressions for queries that seesignificant gains from1349d27 due to presorted results coming in froman Index Scan.Backpatch to 16, where1349d27 was introducedAuthor: David Rowley <dgrowleyml@gmail.com>Reported-by: Kaimeh <kkaimeh@gmail.com>Diagnosed-by: Tom Lane <tgl@sss.pgh.pa.us>Discussion:https://postgr.es/m/CAK-%2BJz9J%3DQ06-M7cDJoPNeYbz5EZDqkjQbJnmRyQyzkbRGsYkA%40mail.gmail.comBackpatch-through: 161 parentecb8e56 commit065ce49
File tree
4 files changed
+115
-16
lines changed- src
- backend/optimizer/plan
- test/regress
- expected
- sql
4 files changed
+115
-16
lines changedLines changed: 47 additions & 4 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
3280 | 3280 |
| |
3281 | 3281 |
| |
3282 | 3282 |
| |
3283 |
| - | |
3284 |
| - | |
3285 |
| - | |
3286 |
| - | |
| 3283 | + | |
| 3284 | + | |
| 3285 | + | |
| 3286 | + | |
| 3287 | + | |
| 3288 | + | |
| 3289 | + | |
| 3290 | + | |
| 3291 | + | |
| 3292 | + | |
| 3293 | + | |
| 3294 | + | |
| 3295 | + | |
| 3296 | + | |
| 3297 | + | |
| 3298 | + | |
| 3299 | + | |
| 3300 | + | |
| 3301 | + | |
| 3302 | + | |
| 3303 | + | |
| 3304 | + | |
| 3305 | + | |
| 3306 | + | |
| 3307 | + | |
| 3308 | + | |
| 3309 | + | |
| 3310 | + | |
| 3311 | + | |
| 3312 | + | |
| 3313 | + | |
| 3314 | + | |
| 3315 | + | |
| 3316 | + | |
| 3317 | + | |
| 3318 | + | |
| 3319 | + | |
| 3320 | + | |
| 3321 | + | |
| 3322 | + | |
| 3323 | + | |
| 3324 | + | |
| 3325 | + | |
| 3326 | + | |
| 3327 | + | |
| 3328 | + | |
| 3329 | + | |
3287 | 3330 |
| |
3288 | 3331 |
| |
3289 | 3332 |
| |
|
Lines changed: 37 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1586 | 1586 |
| |
1587 | 1587 |
| |
1588 | 1588 |
| |
| 1589 | + | |
| 1590 | + | |
| 1591 | + | |
| 1592 | + | |
| 1593 | + | |
| 1594 | + | |
| 1595 | + | |
| 1596 | + | |
| 1597 | + | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
| 1609 | + | |
| 1610 | + | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
1589 | 1626 |
| |
1590 | 1627 |
| |
1591 | 1628 |
| |
|
Lines changed: 12 additions & 12 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
835 | 835 |
| |
836 | 836 |
| |
837 | 837 |
| |
838 |
| - | |
839 |
| - | |
840 |
| - | |
841 |
| - | |
842 |
| - | |
843 |
| - | |
844 |
| - | |
845 |
| - | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
846 | 845 |
| |
847 |
| - | |
848 |
| - | |
849 |
| - | |
| 846 | + | |
850 | 847 |
| |
851 | 848 |
| |
| 849 | + | |
| 850 | + | |
| 851 | + | |
852 | 852 |
| |
853 |
| - | |
| 853 | + | |
854 | 854 |
| |
855 | 855 |
| |
856 | 856 |
| |
|
Lines changed: 19 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
595 | 595 |
| |
596 | 596 |
| |
597 | 597 |
| |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
598 | 617 |
| |
599 | 618 |
| |
600 | 619 |
| |
|
0 commit comments
Comments
(0)