Skip to content

Commit 2b8f74d

Browse files
committed
Improve documentation about CASE and constant subexpressions.
The possibility that constant subexpressions of a CASE might be evaluated at planning time was touched on in 9.17.1 (CASE expressions), but it really ought to be explained in 4.2.14 (Expression Evaluation Rules) which is the primary discussion of such topics. Add text and an example there, and revise the <note> under CASE to link there. Back-patch to all supported branches, since it's acted like this for a long time (though 9.2+ is probably worse because of its more aggressive use of constant-folding via replanning of nominally-prepared statements). Pre-9.4, also back-patch text added in commit 0ce627d about CASE versus aggregate functions. Tom Lane and David Johnston, per discussion of bug #12273.
1 parent 4a48b4c commit 2b8f74d

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

doc/src/sgml/func.sgml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11124,11 +11124,13 @@ SELECT ... WHERE CASE WHEN x &lt;&gt; 0 THEN y/x &gt; 1.5 ELSE false END;
1112411124

1112511125
<note>
1112611126
<para>
11127-
As described in <xref linkend="xfunc-volatility">, functions and
11128-
operators marked <literal>IMMUTABLE</literal> can be evaluated when
11129-
the query is planned rather than when it is executed. This means
11130-
that constant parts of a subexpression that is not evaluated during
11131-
query execution might still be evaluated during query planning.
11127+
As described in <xref linkend="syntax-express-eval">, there are various
11128+
situations in which subexpressions of an expression are evaluated at
11129+
different times, so that the principle that <quote><token>CASE</token>
11130+
evaluates only necessary subexpressions</quote> is not ironclad. For
11131+
example a constant <literal>1/0</> subexpression will usually result in
11132+
a division-by-zero failure at planning time, even if it's within
11133+
a <token>CASE</token> arm that would never be entered at run time.
1113211134
</para>
1113311135
</note>
1113411136
</sect2>

doc/src/sgml/syntax.sgml

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,9 +2428,36 @@ SELECT ... WHERE CASE WHEN x &gt; 0 THEN y/x &gt; 1.5 ELSE false END;
24282428
</para>
24292429

24302430
<para>
2431-
A limitation of this technique is that a <literal>CASE</> cannot
2431+
<literal>CASE</> is not a cure-all for such issues, however.
2432+
One limitation of the technique illustrated above is that it does not
2433+
prevent early evaluation of constant subexpressions.
2434+
As described in <xref linkend="xfunc-volatility">, functions and
2435+
operators marked <literal>IMMUTABLE</literal> can be evaluated when
2436+
the query is planned rather than when it is executed. Thus for example
2437+
<programlisting>
2438+
SELECT CASE WHEN x &gt; 0 THEN x ELSE 1/0 END FROM tab;
2439+
</programlisting>
2440+
is likely to result in a division-by-zero failure due to the planner
2441+
trying to simplify the constant subexpression,
2442+
even if every row in the table has <literal>x &gt; 0</> so that the
2443+
<literal>ELSE</> arm would never be entered at run time.
2444+
</para>
2445+
2446+
<para>
2447+
While that particular example might seem silly, related cases that don't
2448+
obviously involve constants can occur in queries executed within
2449+
functions, since the values of function arguments and local variables
2450+
can be inserted into queries as constants for planning purposes.
2451+
Within <application>PL/pgSQL</> functions, for example, using an
2452+
<literal>IF</>-<literal>THEN</>-<literal>ELSE</> statement to protect
2453+
a risky computation is much safer than just nesting it in a
2454+
<literal>CASE</> expression.
2455+
</para>
2456+
2457+
<para>
2458+
Another limitation of the same kind is that a <literal>CASE</> cannot
24322459
prevent evaluation of an aggregate expression contained within it,
2433-
because aggregate expressions are computed before <quote>scalar</>
2460+
because aggregate expressions are computed before other
24342461
expressions in a <literal>SELECT</> list or <literal>HAVING</> clause
24352462
are considered. For example, the following query can cause a
24362463
division-by-zero error despite seemingly having protected against it:

0 commit comments

Comments
 (0)