Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9118d03

Browse files
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 me
1 parentb1fdc72 commit9118d03

File tree

4 files changed

+478
-44
lines changed

4 files changed

+478
-44
lines changed

‎doc/src/sgml/ref/select.sgml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,36 @@ UNBOUNDED FOLLOWING
993993
cases it is not possible to specify new names with <literal>AS</>;
994994
the output column names will be the same as the table columns' names.
995995
</para>
996+
997+
<para>
998+
According to the SQL standard, the expressions in the output list should
999+
be computed before applying <literal>DISTINCT</literal>, <literal>ORDER
1000+
BY</literal>, or <literal>LIMIT</literal>. This is obviously necessary
1001+
when using <literal>DISTINCT</literal>, since otherwise it's not clear
1002+
what values are being made distinct. However, in many cases it is
1003+
convenient if output expressions are computed after <literal>ORDER
1004+
BY</literal> and <literal>LIMIT</literal>; particularly if the output list
1005+
contains any volatile or expensive functions. With that behavior, the
1006+
order of function evaluations is more intuitive and there will not be
1007+
evaluations corresponding to rows that never appear in the output.
1008+
<productname>PostgreSQL</> will effectively evaluate output expressions
1009+
after sorting and limiting, so long as those expressions are not
1010+
referenced in <literal>DISTINCT</literal>, <literal>ORDER BY</literal>
1011+
or <literal>GROUP BY</literal>. (As a counterexample, <literal>SELECT
1012+
f(x) FROM tab ORDER BY 1</> clearly must evaluate <function>f(x)</>
1013+
before sorting.) Output expressions that contain set-returning functions
1014+
are effectively evaluated after sorting and before limiting, so
1015+
that <literal>LIMIT</literal> will act to cut off the output from a
1016+
set-returning function.
1017+
</para>
1018+
1019+
<note>
1020+
<para>
1021+
<productname>PostgreSQL</> versions before 9.6 did not provide any
1022+
guarantees about the timing of evaluation of output expressions versus
1023+
sorting and limiting; it depended on the form of the chosen query plan.
1024+
</para>
1025+
</note>
9961026
</refsect2>
9971027

9981028
<refsect2 id="sql-distinct">

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp