- Notifications
You must be signed in to change notification settings - Fork4.9k
Commit608b167
committed
Allow user control of CTE materialization, and change the default behavior.
Historically we've always materialized the full output of a CTE query,treating WITH as an optimization fence (so that, for example, restrictionsfrom the outer query cannot be pushed into it). This is appropriate whenthe CTE query is INSERT/UPDATE/DELETE, or is recursive; but when the CTEquery is non-recursive and side-effect-free, there's no hazard of changingthe query results by pushing restrictions down.Another argument for materialization is that it can avoid duplicatecomputation of an expensive WITH query --- but that only applies ifthe WITH query is called more than once in the outer query. Even thenit could still be a net loss, if each call has restrictions thatwould allow just a small part of the WITH query to be computed.Hence, let's change the behavior for WITH queries that are non-recursiveand side-effect-free. By default, we will inline them into the outerquery (removing the optimization fence) if they are called just once.If they are called more than once, we will keep the old behavior bydefault, but the user can override this and force inlining by specifyingNOT MATERIALIZED. Lastly, the user can force the old behavior byspecifying MATERIALIZED; this would mainly be useful when the query haddeliberately been employing WITH as an optimization fence to prevent apoor choice of plan.Andreas Karlsson, Andrew Gierth, David FetterDiscussion:https://postgr.es/m/87sh48ffhb.fsf@news-spur.riddles.org.uk1 parent79730e2 commit608b167
File tree
23 files changed
+580
-44
lines changed- contrib
- pg_stat_statements
- postgres_fdw
- expected
- sql
- doc/src/sgml
- ref
- src
- backend
- nodes
- optimizer/plan
- parser
- utils/adt
- include
- catalog
- nodes
- test/regress
- expected
- sql
23 files changed
+580
-44
lines changedLines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2927 | 2927 |
| |
2928 | 2928 |
| |
2929 | 2929 |
| |
| 2930 | + | |
2930 | 2931 |
| |
2931 | 2932 |
| |
2932 | 2933 |
| |
|
Lines changed: 2 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1888 | 1888 |
| |
1889 | 1889 |
| |
1890 | 1890 |
| |
1891 |
| - | |
| 1891 | + | |
1892 | 1892 |
| |
1893 | 1893 |
| |
1894 | 1894 |
| |
| |||
1905 | 1905 |
| |
1906 | 1906 |
| |
1907 | 1907 |
| |
1908 |
| - | |
| 1908 | + | |
1909 | 1909 |
| |
1910 | 1910 |
| |
1911 | 1911 |
| |
|
Lines changed: 2 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
493 | 493 |
| |
494 | 494 |
| |
495 | 495 |
| |
496 |
| - | |
497 |
| - | |
| 496 | + | |
| 497 | + | |
498 | 498 |
| |
499 | 499 |
| |
500 | 500 |
| |
|
Lines changed: 78 additions & 6 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2199 | 2199 |
| |
2200 | 2200 |
| |
2201 | 2201 |
| |
2202 |
| - | |
2203 |
| - | |
2204 |
| - | |
| 2202 | + | |
| 2203 | + | |
| 2204 | + | |
| 2205 | + | |
2205 | 2206 |
| |
2206 | 2207 |
| |
2207 | 2208 |
| |
2208 | 2209 |
| |
2209 |
| - | |
2210 |
| - | |
2211 |
| - | |
| 2210 | + | |
| 2211 | + | |
| 2212 | + | |
| 2213 | + | |
| 2214 | + | |
2212 | 2215 |
| |
2213 | 2216 |
| |
2214 | 2217 |
| |
2215 | 2218 |
| |
2216 | 2219 |
| |
2217 | 2220 |
| |
| 2221 | + | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
| 2226 | + | |
| 2227 | + | |
| 2228 | + | |
| 2229 | + | |
| 2230 | + | |
| 2231 | + | |
| 2232 | + | |
| 2233 | + | |
| 2234 | + | |
| 2235 | + | |
| 2236 | + | |
| 2237 | + | |
| 2238 | + | |
| 2239 | + | |
| 2240 | + | |
| 2241 | + | |
| 2242 | + | |
| 2243 | + | |
| 2244 | + | |
| 2245 | + | |
| 2246 | + | |
| 2247 | + | |
| 2248 | + | |
| 2249 | + | |
| 2250 | + | |
| 2251 | + | |
| 2252 | + | |
| 2253 | + | |
| 2254 | + | |
| 2255 | + | |
| 2256 | + | |
| 2257 | + | |
| 2258 | + | |
| 2259 | + | |
| 2260 | + | |
| 2261 | + | |
| 2262 | + | |
| 2263 | + | |
| 2264 | + | |
| 2265 | + | |
| 2266 | + | |
| 2267 | + | |
| 2268 | + | |
| 2269 | + | |
| 2270 | + | |
| 2271 | + | |
| 2272 | + | |
| 2273 | + | |
| 2274 | + | |
| 2275 | + | |
| 2276 | + | |
| 2277 | + | |
| 2278 | + | |
| 2279 | + | |
| 2280 | + | |
| 2281 | + | |
| 2282 | + | |
| 2283 | + | |
| 2284 | + | |
| 2285 | + | |
| 2286 | + | |
| 2287 | + | |
| 2288 | + | |
| 2289 | + | |
2218 | 2290 |
| |
2219 | 2291 |
| |
2220 | 2292 |
| |
|
Lines changed: 48 additions & 9 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
72 | 72 |
| |
73 | 73 |
| |
74 | 74 |
| |
75 |
| - | |
| 75 | + | |
76 | 76 |
| |
77 | 77 |
| |
78 | 78 |
| |
| |||
93 | 93 |
| |
94 | 94 |
| |
95 | 95 |
| |
96 |
| - | |
| 96 | + | |
| 97 | + | |
97 | 98 |
| |
98 | 99 |
| |
99 | 100 |
| |
| |||
272 | 273 |
| |
273 | 274 |
| |
274 | 275 |
| |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
275 | 285 |
| |
276 | 286 |
| |
277 |
| - | |
| 287 | + | |
278 | 288 |
| |
279 | 289 |
| |
280 | 290 |
| |
281 | 291 |
| |
282 | 292 |
| |
283 | 293 |
| |
284 | 294 |
| |
285 |
| - | |
286 |
| - | |
287 |
| - | |
288 |
| - | |
289 |
| - | |
290 |
| - | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
291 | 324 |
| |
292 | 325 |
| |
293 | 326 |
| |
| |||
2087 | 2120 |
| |
2088 | 2121 |
| |
2089 | 2122 |
| |
| 2123 | + | |
| 2124 | + | |
| 2125 | + | |
| 2126 | + | |
| 2127 | + | |
| 2128 | + | |
2090 | 2129 |
| |
2091 | 2130 |
| |
2092 | 2131 |
| |
|
Lines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2536 | 2536 |
| |
2537 | 2537 |
| |
2538 | 2538 |
| |
| 2539 | + | |
2539 | 2540 |
| |
2540 | 2541 |
| |
2541 | 2542 |
| |
|
Lines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2791 | 2791 |
| |
2792 | 2792 |
| |
2793 | 2793 |
| |
| 2794 | + | |
2794 | 2795 |
| |
2795 | 2796 |
| |
2796 | 2797 |
| |
|
Lines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2989 | 2989 |
| |
2990 | 2990 |
| |
2991 | 2991 |
| |
| 2992 | + | |
2992 | 2993 |
| |
2993 | 2994 |
| |
2994 | 2995 |
| |
|
Lines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
418 | 418 |
| |
419 | 419 |
| |
420 | 420 |
| |
| 421 | + | |
421 | 422 |
| |
422 | 423 |
| |
423 | 424 |
| |
|
Lines changed: 2 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
646 | 646 |
| |
647 | 647 |
| |
648 | 648 |
| |
649 |
| - | |
650 |
| - | |
| 649 | + | |
| 650 | + | |
651 | 651 |
| |
652 | 652 |
| |
653 | 653 |
| |
|
0 commit comments
Comments
(0)