forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit9eacee2
committed
Add Result Cache executor node (take 2)
Here we add a new executor node type named "Result Cache". The plannercan include this node type in the plan to have the executor cache theresults from the inner side of parameterized nested loop joins. Thisallows caching of tuples for sets of parameters so that in the event thatthe node sees the same parameter values again, it can just return thecached tuples instead of rescanning the inner side of the join all overagain. Internally, result cache uses a hash table in order to quicklyfind tuples that have been previously cached.For certain data sets, this can significantly improve the performance ofjoins. The best cases for using this new node type are for join problemswhere a large portion of the tuples from the inner side of the join haveno join partner on the outer side of the join. In such cases, hash joinwould have to hash values that are never looked up, thus bloating the hashtable and possibly causing it to multi-batch. Merge joins would have toskip over all of the unmatched rows. If we use a nested loop join with aresult cache, then we only cache tuples that have at least one joinpartner on the outer side of the join. The benefits of using aparameterized nested loop with a result cache increase when there arefewer distinct values being looked up and the number of lookups of eachvalue is large. Also, hash probes to lookup the cache can be much fasterthan the hash probe in a hash join as it's common that the result cache'shash table is much smaller than the hash join's due to result cache onlycaching useful tuples rather than all tuples from the inner side of thejoin. This variation in hash probe performance is more significant whenthe hash join's hash table no longer fits into the CPU's L3 cache, but theresult cache's hash table does. The apparent "random" access of hashbuckets with each hash probe can cause a poor L3 cache hit ratio for largehash tables. Smaller hash tables generally perform better.The hash table used for the cache limits itself to not exceeding work_mem* hash_mem_multiplier in size. We maintain a dlist of keys for this cacheand when we're adding new tuples and realize we've exceeded the memorybudget, we evict cache entries starting with the least recently used onesuntil we have enough memory to add the new tuples to the cache.For parameterized nested loop joins, we now consider using one of theseresult cache nodes in between the nested loop node and its inner node. Wedetermine when this might be useful based on cost, which is primarilydriven off of what the expected cache hit ratio will be. Estimating thecache hit ratio relies on having good distinct estimates on the nestedloop's parameters.For now, the planner will only consider using a result cache forparameterized nested loop joins. This works for both normal joins andalso for LATERAL type joins to subqueries. It is possible to use this newnode for other uses in the future. For example, to cache results fromcorrelated subqueries. However, that's not done here due to somedifficulties obtaining a distinct estimation on the outer plan tocalculate the estimated cache hit ratio. Currently we plan the inner planbefore planning the outer plan so there is no good way to know if a resultcache would be useful or not since we can't estimate the number of timesthe subplan will be called until the outer plan is generated.The functionality being added here is newly introducing a dependency onthe return value of estimate_num_groups() during the join search.Previously, during the join search, we only ever needed to performselectivity estimations. With this commit, we need to useestimate_num_groups() in order to estimate what the hit ratio on theresult cache will be. In simple terms, if we expect 10 distinct valuesand we expect 1000 outer rows, then we'll estimate the hit ratio to be99%. Since cache hits are very cheap compared to scanning the underlyingnodes on the inner side of the nested loop join, then this willsignificantly reduce the planner's cost for the join. However, it'sfairly easy to see here that things will go bad when estimate_num_groups()incorrectly returns a value that's significantly lower than the actualnumber of distinct values. If this happens then that may cause us to makeuse of a nested loop join with a result cache instead of some other jointype, such as a merge or hash join. Our distinct estimations have beenknown to be a source of trouble in the past, so the extra reliance on themhere could cause the planner to choose slower plans than it did previousto having this feature. Distinct estimations are also fairly hard toestimate accurately when several tables have been joined already or when aWHERE clause filters out a set of values that are correlated to theexpressions we're estimating the number of distinct value for.For now, the costing we perform during query planning for result cachesdoes put quite a bit of faith in the distinct estimations being accurate.When these are accurate then we should generally see faster executiontimes for plans containing a result cache. However, in the real world, wemay find that we need to either change the costings to put less trust inthe distinct estimations being accurate or perhaps even disable thisfeature by default. There's always an element of risk when we teach thequery planner to do new tricks that it decides to use that new trick atthe wrong time and causes a regression. Users may opt to get the oldbehavior by turning the feature off using the enable_resultcache GUC.Currently, this is enabled by default. It remains to be seen if we'llmaintain that setting for the release.Additionally, the name "Result Cache" is the best name I could think offor this new node at the time I started writing the patch. Nobody seemsto strongly dislike the name. A few people did suggest other names but noother name seemed to dominate in the brief discussion that there was aboutnames. Let's allow the beta period to see if the current name pleasesenough people. If there's some consensus on a better name, then we canchange it before the release. Please see the 2nd discussion link belowfor the discussion on the "Result Cache" name.Author: David RowleyReviewed-by: Andy Fan, Justin Pryzby, Zhihong Yu, Hou ZhijieTested-By: Konstantin KnizhnikDiscussion:https://postgr.es/m/CAApHDvrPcQyQdWERGYWx8J%2B2DLUNgXu%2BfOSbQ1UscxrunyXyrQ%40mail.gmail.comDiscussion:https://postgr.es/m/CAApHDvq=yQXr5kqhRviT2RhNKwToaWr9JAN5t+5_PzhuRJ3wvg@mail.gmail.com1 parentfe246d1 commit9eacee2
File tree
46 files changed
+2709
-76
lines changed- contrib/postgres_fdw
- expected
- sql
- doc/src/sgml
- src
- backend
- commands
- executor
- nodes
- optimizer
- path
- plan
- util
- utils/misc
- include
- executor
- lib
- nodes
- optimizer
- test/regress
- expected
- sql
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
46 files changed
+2709
-76
lines changedLines changed: 15 additions & 10 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1602 | 1602 |
| |
1603 | 1603 |
| |
1604 | 1604 |
| |
| 1605 | + | |
1605 | 1606 |
| |
1606 | 1607 |
| |
1607 | 1608 |
| |
| |||
1628 | 1629 |
| |
1629 | 1630 |
| |
1630 | 1631 |
| |
| 1632 | + | |
1631 | 1633 |
| |
1632 | 1634 |
| |
1633 | 1635 |
| |
| |||
2139 | 2141 |
| |
2140 | 2142 |
| |
2141 | 2143 |
| |
2142 |
| - | |
2143 |
| - | |
| 2144 | + | |
| 2145 | + | |
2144 | 2146 |
| |
2145 | 2147 |
| |
2146 | 2148 |
| |
2147 | 2149 |
| |
2148 | 2150 |
| |
2149 | 2151 |
| |
2150 |
| - | |
2151 |
| - | |
2152 |
| - | |
2153 |
| - | |
2154 |
| - | |
2155 |
| - | |
2156 |
| - | |
2157 |
| - | |
| 2152 | + | |
| 2153 | + | |
| 2154 | + | |
| 2155 | + | |
| 2156 | + | |
| 2157 | + | |
| 2158 | + | |
| 2159 | + | |
| 2160 | + | |
| 2161 | + | |
| 2162 | + | |
2158 | 2163 |
| |
2159 | 2164 |
| |
2160 | 2165 |
| |
|
Lines changed: 2 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
502 | 502 |
| |
503 | 503 |
| |
504 | 504 |
| |
| 505 | + | |
505 | 506 |
| |
506 | 507 |
| |
507 | 508 |
| |
508 | 509 |
| |
| 510 | + | |
509 | 511 |
| |
510 | 512 |
| |
511 | 513 |
| |
|
Lines changed: 22 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1770 | 1770 |
| |
1771 | 1771 |
| |
1772 | 1772 |
| |
1773 |
| - | |
1774 |
| - | |
| 1773 | + | |
| 1774 | + | |
| 1775 | + | |
1775 | 1776 |
| |
1776 | 1777 |
| |
1777 | 1778 |
| |
| |||
4925 | 4926 |
| |
4926 | 4927 |
| |
4927 | 4928 |
| |
| 4929 | + | |
| 4930 | + | |
| 4931 | + | |
| 4932 | + | |
| 4933 | + | |
| 4934 | + | |
| 4935 | + | |
| 4936 | + | |
| 4937 | + | |
| 4938 | + | |
| 4939 | + | |
| 4940 | + | |
| 4941 | + | |
| 4942 | + | |
| 4943 | + | |
| 4944 | + | |
| 4945 | + | |
| 4946 | + | |
| 4947 | + | |
4928 | 4948 |
| |
4929 | 4949 |
| |
4930 | 4950 |
| |
|
Lines changed: 144 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
108 | 108 |
| |
109 | 109 |
| |
110 | 110 |
| |
| 111 | + | |
| 112 | + | |
111 | 113 |
| |
112 | 114 |
| |
113 | 115 |
| |
| |||
1284 | 1286 |
| |
1285 | 1287 |
| |
1286 | 1288 |
| |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
1287 | 1292 |
| |
1288 | 1293 |
| |
1289 | 1294 |
| |
| |||
1996 | 2001 |
| |
1997 | 2002 |
| |
1998 | 2003 |
| |
| 2004 | + | |
| 2005 | + | |
| 2006 | + | |
| 2007 | + | |
1999 | 2008 |
| |
2000 | 2009 |
| |
2001 | 2010 |
| |
| |||
3063 | 3072 |
| |
3064 | 3073 |
| |
3065 | 3074 |
| |
| 3075 | + | |
| 3076 | + | |
| 3077 | + | |
| 3078 | + | |
| 3079 | + | |
| 3080 | + | |
| 3081 | + | |
| 3082 | + | |
| 3083 | + | |
| 3084 | + | |
| 3085 | + | |
| 3086 | + | |
| 3087 | + | |
| 3088 | + | |
| 3089 | + | |
| 3090 | + | |
| 3091 | + | |
| 3092 | + | |
| 3093 | + | |
| 3094 | + | |
| 3095 | + | |
| 3096 | + | |
| 3097 | + | |
| 3098 | + | |
| 3099 | + | |
| 3100 | + | |
| 3101 | + | |
| 3102 | + | |
| 3103 | + | |
| 3104 | + | |
| 3105 | + | |
| 3106 | + | |
| 3107 | + | |
| 3108 | + | |
| 3109 | + | |
| 3110 | + | |
| 3111 | + | |
| 3112 | + | |
| 3113 | + | |
| 3114 | + | |
| 3115 | + | |
| 3116 | + | |
| 3117 | + | |
| 3118 | + | |
| 3119 | + | |
| 3120 | + | |
| 3121 | + | |
| 3122 | + | |
| 3123 | + | |
| 3124 | + | |
| 3125 | + | |
| 3126 | + | |
| 3127 | + | |
| 3128 | + | |
| 3129 | + | |
| 3130 | + | |
| 3131 | + | |
| 3132 | + | |
| 3133 | + | |
| 3134 | + | |
| 3135 | + | |
| 3136 | + | |
| 3137 | + | |
| 3138 | + | |
| 3139 | + | |
| 3140 | + | |
| 3141 | + | |
| 3142 | + | |
| 3143 | + | |
| 3144 | + | |
| 3145 | + | |
| 3146 | + | |
| 3147 | + | |
| 3148 | + | |
| 3149 | + | |
| 3150 | + | |
| 3151 | + | |
| 3152 | + | |
| 3153 | + | |
| 3154 | + | |
| 3155 | + | |
| 3156 | + | |
| 3157 | + | |
| 3158 | + | |
| 3159 | + | |
| 3160 | + | |
| 3161 | + | |
| 3162 | + | |
| 3163 | + | |
| 3164 | + | |
| 3165 | + | |
| 3166 | + | |
| 3167 | + | |
| 3168 | + | |
| 3169 | + | |
| 3170 | + | |
| 3171 | + | |
| 3172 | + | |
| 3173 | + | |
| 3174 | + | |
| 3175 | + | |
| 3176 | + | |
| 3177 | + | |
| 3178 | + | |
| 3179 | + | |
| 3180 | + | |
| 3181 | + | |
| 3182 | + | |
| 3183 | + | |
| 3184 | + | |
| 3185 | + | |
| 3186 | + | |
| 3187 | + | |
| 3188 | + | |
| 3189 | + | |
| 3190 | + | |
| 3191 | + | |
| 3192 | + | |
| 3193 | + | |
| 3194 | + | |
| 3195 | + | |
| 3196 | + | |
| 3197 | + | |
| 3198 | + | |
| 3199 | + | |
| 3200 | + | |
| 3201 | + | |
| 3202 | + | |
| 3203 | + | |
| 3204 | + | |
| 3205 | + | |
| 3206 | + | |
| 3207 | + | |
| 3208 | + | |
| 3209 | + | |
3066 | 3210 |
| |
3067 | 3211 |
| |
3068 | 3212 |
| |
|
Lines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
61 | 61 |
| |
62 | 62 |
| |
63 | 63 |
| |
| 64 | + | |
64 | 65 |
| |
65 | 66 |
| |
66 | 67 |
| |
|
Lines changed: 5 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
44 | 44 |
| |
45 | 45 |
| |
46 | 46 |
| |
| 47 | + | |
47 | 48 |
| |
48 | 49 |
| |
49 | 50 |
| |
| |||
254 | 255 |
| |
255 | 256 |
| |
256 | 257 |
| |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
257 | 262 |
| |
258 | 263 |
| |
259 | 264 |
| |
|
0 commit comments
Comments
(0)