forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit9118d03
committed
When appropriate, postpone SELECT output expressions till after ORDER BY.
It is frequently useful for volatile, set-returning, or expensive functionsin a SELECT's targetlist to be postponed till after ORDER BY and LIMIT aredone. Otherwise, the functions might be executed for every row of thetable despite the presence of LIMIT, and/or be executed in an unexpectedorder. For example, inSELECT x, nextval('seq') FROM tab ORDER BY x LIMIT 10;it's probably desirable that the nextval() values are ordered the sameas x, and that nextval() is not run more than 10 times.In the past, Postgres was inconsistent in this area: you would get thedesirable behavior if the ordering were performed via an indexscan, butnot if it had to be done by an explicit sort step. Getting the desiredbehavior reliably required contortions likeSELECT x, nextval('seq') FROM (SELECT x FROM tab ORDER BY x) ss LIMIT 10;This patch conditionally postpones evaluation of pure-output targetexpressions (that is, those that are not used as DISTINCT, ORDER BY, orGROUP BY columns) so that they effectively occur after sorting, even if anexplicit sort step is necessary. Volatile expressions and set-returningexpressions are always postponed, so as to provide consistent semantics.Expensive expressions (costing more than 10 times typical operator cost,which by default would include any user-defined function) are postponedif there is a LIMIT or if there are expressions that must be postponed.We could be more aggressive and postpone any nontrivial expression, butthere are costs associated with doing so: it requires an extra Result plannode which adds some overhead, and postponement changes the volume of datagoing through the sort step, perhaps for the worse. Since we tend not tohave very good estimates of the output width of nontrivial expressions,it's hard to have much confidence in our ability to predict whetherpostponement would increase or decrease the cost of the sort; thereforethis patch doesn't attempt to make decisions conditionally on that.Between these factors and a general desire not to change query behaviorwhen there's not a demonstrable benefit, it seems best to be conservativeabout applying postponement. We might tweak the decision rules in thefuture, though.Konstantin Knizhnik, heavily rewritten by me1 parentb1fdc72 commit9118d03
File tree
4 files changed
+478
-44
lines changed- doc/src/sgml/ref
- src
- backend/optimizer/plan
- test/regress
- expected
- sql
4 files changed
+478
-44
lines changedLines changed: 30 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
993 | 993 |
| |
994 | 994 |
| |
995 | 995 |
| |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
996 | 1026 |
| |
997 | 1027 |
| |
998 | 1028 |
| |
|
0 commit comments
Comments
(0)