forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit4a29eab
committed
Remove pessimistic cost penalization from Incremental Sort
When incremental sorts were added in v13 a 1.5x pessimism factor was addedto the cost modal. Seemingly this was done because the cost modal onlyhas an estimate of the total number of input rows and the number ofpresorted groups. It assumes that the input rows will be evenlydistributed throughout the presorted groups. The 1.5x pessimism factorwas added to slightly reduce the likelihood of incremental sorts beingused in the hope to avoid performance regressions where an incrementalsort plan was picked and turned out slower due to a large skew in thenumber of rows in the presorted groups.An additional quirk with the path generation code meant that we couldconsider both a sort and an incremental sort on paths with presorted keys.This meant that with the pessimism factor, it was possible that we optedto perform a sort rather than an incremental sort when the given path hadpresorted keys.Here we remove the 1.5x pessimism factor to allow incremental sorts tohave a fairer chance at being chosen against a full sort.Previously we would generally create a sort path on the cheapest inputpath (if that wasn't sorted already) and incremental sort paths on anypath which had presorted keys. This meant that if the cheapest input pathwasn't completely sorted but happened to have presorted keys, we wouldcreate a full sort path *and* an incremental sort path on that input path.Here we change this logic so that if there are presorted keys, we onlycreate an incremental sort path, and create sort paths only when a fullsort is required.Both the removal of the cost pessimism factor and the changes made to thepath generation make it more likely that incremental sorts will now bechosen. That, of course, as with teaching the planner any new tricks,means an increased likelihood that the planner will perform an incrementalsort when it's not the best method. Our standard escape hatch for thesecases is an enable_* GUC. enable_incremental_sort already exists forthis.This came out of a report by Pavel Luzanov where he mentioned that themaster branch was choosing to perform a Seq Scan -> Sort -> GroupAggregate for his query with an ORDER BY aggregate function. The v15 planfor his query performed an Index Scan -> Group Aggregate, of course, theaggregate performed the final sort internally in nodeAgg.c for theaggregate's ORDER BY. The ideal plan would have been to use the index,which provided partially sorted input then use an incremental sort toprovide the aggregate with the sorted input. This was not being chosendue to the pessimism in the incremental sort cost modal, so here we removethat and rationalize the path generation so that sort and incremental sortplans don't have to needlessly compete. We assume that it's senselessto ever use a full sort on a given input path where an incremental sortcan be performed.Reported-by: Pavel LuzanovReviewed-by: Richard GuoDiscussion:https://postgr.es/m/9f61ddbf-2989-1536-b31e-6459370a6baa%40postgrespro.ru1 parent8b6b043 commit4a29eab
File tree
8 files changed
+220
-419
lines changed- contrib/postgres_fdw
- expected
- sql
- src
- backend/optimizer
- path
- plan
- test/regress
- expected
- sql
8 files changed
+220
-419
lines changedLines changed: 2 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2796 | 2796 |
| |
2797 | 2797 |
| |
2798 | 2798 |
| |
| 2799 | + | |
2799 | 2800 |
| |
2800 | 2801 |
| |
2801 | 2802 |
| |
| |||
2814 | 2815 |
| |
2815 | 2816 |
| |
2816 | 2817 |
| |
| 2818 | + | |
2817 | 2819 |
| |
2818 | 2820 |
| |
2819 | 2821 |
| |
|
Lines changed: 2 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
750 | 750 |
| |
751 | 751 |
| |
752 | 752 |
| |
| 753 | + | |
753 | 754 |
| |
754 | 755 |
| |
755 | 756 |
| |
| 757 | + | |
756 | 758 |
| |
757 | 759 |
| |
758 | 760 |
| |
|
Lines changed: 38 additions & 56 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
3207 | 3207 |
| |
3208 | 3208 |
| |
3209 | 3209 |
| |
3210 |
| - | |
3211 |
| - | |
3212 |
| - | |
| 3210 | + | |
| 3211 | + | |
| 3212 | + | |
| 3213 | + | |
| 3214 | + | |
| 3215 | + | |
| 3216 | + | |
| 3217 | + | |
| 3218 | + | |
| 3219 | + | |
| 3220 | + | |
| 3221 | + | |
| 3222 | + | |
| 3223 | + | |
| 3224 | + | |
| 3225 | + | |
3213 | 3226 |
| |
3214 | 3227 |
| |
3215 | 3228 |
| |
3216 | 3229 |
| |
3217 | 3230 |
| |
3218 | 3231 |
| |
3219 |
| - | |
| 3232 | + | |
3220 | 3233 |
| |
3221 |
| - | |
3222 |
| - | |
3223 |
| - | |
3224 |
| - | |
3225 |
| - | |
3226 |
| - | |
3227 |
| - | |
3228 |
| - | |
3229 |
| - | |
3230 |
| - | |
3231 |
| - | |
3232 |
| - | |
3233 |
| - | |
3234 |
| - | |
3235 |
| - | |
3236 |
| - | |
3237 |
| - | |
3238 |
| - | |
3239 |
| - | |
3240 |
| - | |
3241 |
| - | |
3242 |
| - | |
3243 |
| - | |
3244 |
| - | |
3245 |
| - | |
3246 |
| - | |
3247 |
| - | |
3248 |
| - | |
3249 |
| - | |
3250 |
| - | |
3251 |
| - | |
3252 |
| - | |
3253 |
| - | |
3254 |
| - | |
3255 |
| - | |
3256 |
| - | |
3257 |
| - | |
3258 |
| - | |
3259 |
| - | |
3260 |
| - | |
3261 |
| - | |
3262 |
| - | |
3263 |
| - | |
3264 |
| - | |
3265 |
| - | |
3266 |
| - | |
3267 |
| - | |
3268 |
| - | |
3269 |
| - | |
3270 |
| - | |
3271 |
| - | |
3272 |
| - | |
| 3234 | + | |
| 3235 | + | |
| 3236 | + | |
| 3237 | + | |
| 3238 | + | |
| 3239 | + | |
3273 | 3240 |
| |
| 3241 | + | |
| 3242 | + | |
| 3243 | + | |
| 3244 | + | |
| 3245 | + | |
| 3246 | + | |
| 3247 | + | |
| 3248 | + | |
| 3249 | + | |
| 3250 | + | |
| 3251 | + | |
| 3252 | + | |
| 3253 | + | |
| 3254 | + | |
| 3255 | + | |
3274 | 3256 |
| |
3275 | 3257 |
| |
3276 | 3258 |
| |
|
Lines changed: 18 additions & 20 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1959 | 1959 |
| |
1960 | 1960 |
| |
1961 | 1961 |
| |
1962 |
| - | |
1963 |
| - | |
| 1962 | + | |
| 1963 | + | |
1964 | 1964 |
| |
1965 | 1965 |
| |
1966 | 1966 |
| |
| |||
1969 | 1969 |
| |
1970 | 1970 |
| |
1971 | 1971 |
| |
1972 |
| - | |
1973 | 1972 |
| |
1974 | 1973 |
| |
1975 |
| - | |
| 1974 | + | |
1976 | 1975 |
| |
1977 | 1976 |
| |
1978 | 1977 |
| |
| |||
2025 | 2024 |
| |
2026 | 2025 |
| |
2027 | 2026 |
| |
2028 |
| - | |
2029 |
| - | |
| 2027 | + | |
2030 | 2028 |
| |
2031 | 2029 |
| |
2032 | 2030 |
| |
2033 |
| - | |
| 2031 | + | |
2034 | 2032 |
| |
2035 | 2033 |
| |
2036 | 2034 |
| |
| |||
2039 | 2037 |
| |
2040 | 2038 |
| |
2041 | 2039 |
| |
2042 |
| - | |
2043 |
| - | |
2044 |
| - | |
2045 |
| - | |
2046 |
| - | |
| 2040 | + | |
| 2041 | + | |
2047 | 2042 |
| |
2048 | 2043 |
| |
2049 |
| - | |
| 2044 | + | |
2050 | 2045 |
| |
2051 | 2046 |
| |
2052 | 2047 |
| |
2053 | 2048 |
| |
2054 | 2049 |
| |
2055 | 2050 |
| |
2056 |
| - | |
2057 |
| - | |
| 2051 | + | |
| 2052 | + | |
2058 | 2053 |
| |
2059 | 2054 |
| |
2060 | 2055 |
| |
2061 | 2056 |
| |
2062 | 2057 |
| |
2063 | 2058 |
| |
2064 | 2059 |
| |
2065 |
| - | |
2066 |
| - | |
2067 |
| - | |
| 2060 | + | |
| 2061 | + | |
2068 | 2062 |
| |
2069 | 2063 |
| |
2070 | 2064 |
| |
2071 | 2065 |
| |
2072 |
| - | |
2073 |
| - | |
| 2066 | + | |
2074 | 2067 |
| |
2075 | 2068 |
| |
| 2069 | + | |
| 2070 | + | |
| 2071 | + | |
| 2072 | + | |
| 2073 | + | |
2076 | 2074 |
| |
2077 | 2075 |
| |
2078 | 2076 |
| |
|
0 commit comments
Comments
(0)