forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit1349d27
committed
Improve performance of ORDER BY / DISTINCT aggregates
ORDER BY / DISTINCT aggreagtes have, since implemented in Postgres, beenexecuted by always performing a sort in nodeAgg.c to sort the tuples inthe current group into the correct order before calling the transitionfunction on the sorted tuples. This was not great as often there might bean index that could have provided pre-sorted input and allowed thetransition functions to be called as the rows come in, rather than havingto store them in a tuplestore in order to sort them once all the tuplesfor the group have arrived.Here we change the planner so it requests a path with a sort order whichsupports the most amount of ORDER BY / DISTINCT aggregate functions andadd new code to the executor to allow it to support the processing ofORDER BY / DISTINCT aggregates where the tuples are already sorted in thecorrect order.Since there can be many ORDER BY / DISTINCT aggregates in any given querylevel, it's very possible that we can't find an order that suits all ofthese aggregates. The sort order that the planner chooses is simply theone that suits the most aggregate functions. We take the most strictlysorted variation of each order and see how many aggregate functions canuse that, then we try again with the order of the remaining aggregates tosee if another order would suit more aggregate functions. For example:SELECT agg(a ORDER BY a),agg2(a ORDER BY a,b) ...would request the sort order to be {a, b} because {a} is a subset of thesort order of {a,b}, but;SELECT agg(a ORDER BY a),agg2(a ORDER BY c) ...would just pick a plan ordered by {a} (we give precedence to aggregateswhich are earlier in the targetlist).SELECT agg(a ORDER BY a),agg2(a ORDER BY b),agg3(a ORDER BY b) ...would choose to order by {b} since two aggregates suit that vs just onethat requires input ordered by {a}.Author: David RowleyReviewed-by: Ronan Dunklau, James Coleman, Ranier Vilela, Richard Guo, Tom LaneDiscussion:https://postgr.es/m/CAApHDvpHzfo92%3DR4W0%2BxVua3BUYCKMckWAmo-2t_KiXN-wYH%3Dw%40mail.gmail.com1 parenta69959f commit1349d27
File tree
24 files changed
+849
-138
lines changed- contrib/postgres_fdw
- expected
- sql
- src
- backend
- executor
- jit/llvm
- optimizer
- path
- plan
- prep
- parser
- include
- catalog
- executor
- nodes
- optimizer
- test/regress
- expected
- sql
24 files changed
+849
-138
lines changedLines changed: 20 additions & 12 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
3295 | 3295 |
| |
3296 | 3296 |
| |
3297 | 3297 |
| |
3298 |
| - | |
3299 |
| - | |
| 3298 | + | |
| 3299 | + | |
3300 | 3300 |
| |
3301 | 3301 |
| |
3302 | 3302 |
| |
3303 |
| - | |
3304 |
| - | |
3305 |
| - | |
3306 |
| - | |
| 3303 | + | |
| 3304 | + | |
| 3305 | + | |
| 3306 | + | |
| 3307 | + | |
| 3308 | + | |
| 3309 | + | |
3307 | 3310 |
| |
3308 | 3311 |
| |
3309 | 3312 |
| |
| |||
3329 | 3332 |
| |
3330 | 3333 |
| |
3331 | 3334 |
| |
| 3335 | + | |
3332 | 3336 |
| |
3333 | 3337 |
| |
3334 | 3338 |
| |
| |||
3345 | 3349 |
| |
3346 | 3350 |
| |
3347 | 3351 |
| |
| 3352 | + | |
3348 | 3353 |
| |
3349 | 3354 |
| |
3350 | 3355 |
| |
| |||
3366 | 3371 |
| |
3367 | 3372 |
| |
3368 | 3373 |
| |
3369 |
| - | |
3370 |
| - | |
| 3374 | + | |
| 3375 | + | |
3371 | 3376 |
| |
3372 | 3377 |
| |
3373 | 3378 |
| |
3374 |
| - | |
3375 |
| - | |
3376 |
| - | |
3377 |
| - | |
| 3379 | + | |
| 3380 | + | |
| 3381 | + | |
| 3382 | + | |
| 3383 | + | |
| 3384 | + | |
| 3385 | + | |
3378 | 3386 |
| |
3379 | 3387 |
| |
3380 | 3388 |
| |
|
Lines changed: 2 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
943 | 943 |
| |
944 | 944 |
| |
945 | 945 |
| |
| 946 | + | |
946 | 947 |
| |
947 | 948 |
| |
948 | 949 |
| |
| 950 | + | |
949 | 951 |
| |
950 | 952 |
| |
951 | 953 |
| |
|
Lines changed: 45 additions & 7 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
3666 | 3666 |
| |
3667 | 3667 |
| |
3668 | 3668 |
| |
| 3669 | + | |
| 3670 | + | |
3669 | 3671 |
| |
3670 |
| - | |
| 3672 | + | |
3671 | 3673 |
| |
3672 | 3674 |
| |
3673 | 3675 |
| |
3674 | 3676 |
| |
3675 |
| - | |
| 3677 | + | |
| 3678 | + | |
| 3679 | + | |
3676 | 3680 |
| |
3677 | 3681 |
| |
3678 | 3682 |
| |
3679 | 3683 |
| |
3680 | 3684 |
| |
3681 | 3685 |
| |
3682 | 3686 |
| |
| 3687 | + | |
| 3688 | + | |
| 3689 | + | |
| 3690 | + | |
| 3691 | + | |
| 3692 | + | |
| 3693 | + | |
3683 | 3694 |
| |
3684 | 3695 |
| |
3685 | 3696 |
| |
| |||
3689 | 3700 |
| |
3690 | 3701 |
| |
3691 | 3702 |
| |
| 3703 | + | |
3692 | 3704 |
| |
3693 | 3705 |
| |
3694 | 3706 |
| |
3695 | 3707 |
| |
3696 |
| - | |
| 3708 | + | |
| 3709 | + | |
3697 | 3710 |
| |
3698 | 3711 |
| |
3699 | 3712 |
| |
| |||
3705 | 3718 |
| |
3706 | 3719 |
| |
3707 | 3720 |
| |
| 3721 | + | |
| 3722 | + | |
3708 | 3723 |
| |
3709 | 3724 |
| |
3710 | 3725 |
| |
3711 | 3726 |
| |
3712 |
| - | |
| 3727 | + | |
| 3728 | + | |
3713 | 3729 |
| |
3714 | 3730 |
| |
3715 | 3731 |
| |
| |||
3725 | 3741 |
| |
3726 | 3742 |
| |
3727 | 3743 |
| |
| 3744 | + | |
3728 | 3745 |
| |
3729 |
| - | |
3730 | 3746 |
| |
3731 | 3747 |
| |
3732 | 3748 |
| |
| |||
3748 | 3764 |
| |
3749 | 3765 |
| |
3750 | 3766 |
| |
| 3767 | + | |
| 3768 | + | |
| 3769 | + | |
| 3770 | + | |
| 3771 | + | |
| 3772 | + | |
| 3773 | + | |
| 3774 | + | |
| 3775 | + | |
| 3776 | + | |
| 3777 | + | |
| 3778 | + | |
| 3779 | + | |
| 3780 | + | |
| 3781 | + | |
3751 | 3782 |
| |
3752 | 3783 |
| |
3753 | 3784 |
| |
| |||
3808 | 3839 |
| |
3809 | 3840 |
| |
3810 | 3841 |
| |
| 3842 | + | |
| 3843 | + | |
| 3844 | + | |
| 3845 | + | |
| 3846 | + | |
| 3847 | + | |
3811 | 3848 |
| |
3812 | 3849 |
| |
3813 | 3850 |
| |
| |||
3857 | 3894 |
| |
3858 | 3895 |
| |
3859 | 3896 |
| |
3860 |
| - | |
| 3897 | + | |
| 3898 | + | |
3861 | 3899 |
| |
3862 | 3900 |
| |
3863 | 3901 |
| |
| |||
3887 | 3925 |
| |
3888 | 3926 |
| |
3889 | 3927 |
| |
3890 |
| - | |
| 3928 | + | |
3891 | 3929 |
| |
3892 | 3930 |
| |
3893 | 3931 |
| |
|
Lines changed: 102 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
502 | 502 |
| |
503 | 503 |
| |
504 | 504 |
| |
| 505 | + | |
| 506 | + | |
505 | 507 |
| |
506 | 508 |
| |
507 | 509 |
| |
| |||
1786 | 1788 |
| |
1787 | 1789 |
| |
1788 | 1790 |
| |
| 1791 | + | |
| 1792 | + | |
| 1793 | + | |
| 1794 | + | |
| 1795 | + | |
| 1796 | + | |
| 1797 | + | |
| 1798 | + | |
| 1799 | + | |
| 1800 | + | |
| 1801 | + | |
| 1802 | + | |
| 1803 | + | |
| 1804 | + | |
| 1805 | + | |
| 1806 | + | |
| 1807 | + | |
| 1808 | + | |
| 1809 | + | |
| 1810 | + | |
| 1811 | + | |
| 1812 | + | |
1789 | 1813 |
| |
1790 | 1814 |
| |
1791 | 1815 |
| |
| |||
4402 | 4426 |
| |
4403 | 4427 |
| |
4404 | 4428 |
| |
| 4429 | + | |
| 4430 | + | |
| 4431 | + | |
| 4432 | + | |
| 4433 | + | |
| 4434 | + | |
| 4435 | + | |
| 4436 | + | |
| 4437 | + | |
| 4438 | + | |
| 4439 | + | |
| 4440 | + | |
| 4441 | + | |
| 4442 | + | |
| 4443 | + | |
| 4444 | + | |
| 4445 | + | |
| 4446 | + | |
| 4447 | + | |
| 4448 | + | |
| 4449 | + | |
| 4450 | + | |
| 4451 | + | |
| 4452 | + | |
| 4453 | + | |
| 4454 | + | |
| 4455 | + | |
| 4456 | + | |
| 4457 | + | |
| 4458 | + | |
| 4459 | + | |
| 4460 | + | |
| 4461 | + | |
| 4462 | + | |
| 4463 | + | |
| 4464 | + | |
| 4465 | + | |
| 4466 | + | |
| 4467 | + | |
| 4468 | + | |
| 4469 | + | |
| 4470 | + | |
| 4471 | + | |
| 4472 | + | |
| 4473 | + | |
| 4474 | + | |
| 4475 | + | |
| 4476 | + | |
| 4477 | + | |
| 4478 | + | |
| 4479 | + | |
| 4480 | + | |
| 4481 | + | |
| 4482 | + | |
| 4483 | + | |
| 4484 | + | |
| 4485 | + | |
| 4486 | + | |
| 4487 | + | |
| 4488 | + | |
| 4489 | + | |
| 4490 | + | |
| 4491 | + | |
| 4492 | + | |
| 4493 | + | |
| 4494 | + | |
| 4495 | + | |
| 4496 | + | |
| 4497 | + | |
| 4498 | + | |
| 4499 | + | |
| 4500 | + | |
| 4501 | + | |
| 4502 | + | |
| 4503 | + | |
| 4504 | + | |
| 4505 | + | |
| 4506 | + | |
4405 | 4507 |
| |
4406 | 4508 |
| |
4407 | 4509 |
| |
|
0 commit comments
Comments
(0)