Skip to content

Commit ef8472b

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 a47f38e commit ef8472b

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

doc/src/sgml/func.sgml

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

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

doc/src/sgml/syntax.sgml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,6 +2336,55 @@ SELECT ... WHERE CASE WHEN x &gt; 0 THEN y/x &gt; 1.5 ELSE false END;
23362336
example, it would be better to sidestep the problem by writing
23372337
<literal>y &gt; 1.5*x</> instead.)
23382338
</para>
2339+
2340+
<para>
2341+
<literal>CASE</> is not a cure-all for such issues, however.
2342+
One limitation of the technique illustrated above is that it does not
2343+
prevent early evaluation of constant subexpressions.
2344+
As described in <xref linkend="xfunc-volatility">, functions and
2345+
operators marked <literal>IMMUTABLE</literal> can be evaluated when
2346+
the query is planned rather than when it is executed. Thus for example
2347+
<programlisting>
2348+
SELECT CASE WHEN x &gt; 0 THEN x ELSE 1/0 END FROM tab;
2349+
</programlisting>
2350+
is likely to result in a division-by-zero failure due to the planner
2351+
trying to simplify the constant subexpression,
2352+
even if every row in the table has <literal>x &gt; 0</> so that the
2353+
<literal>ELSE</> arm would never be entered at run time.
2354+
</para>
2355+
2356+
<para>
2357+
While that particular example might seem silly, related cases that don't
2358+
obviously involve constants can occur in queries executed within
2359+
functions, since the values of function arguments and local variables
2360+
can be inserted into queries as constants for planning purposes.
2361+
Within <application>PL/pgSQL</> functions, for example, using an
2362+
<literal>IF</>-<literal>THEN</>-<literal>ELSE</> statement to protect
2363+
a risky computation is much safer than just nesting it in a
2364+
<literal>CASE</> expression.
2365+
</para>
2366+
2367+
<para>
2368+
Another limitation of the same kind is that a <literal>CASE</> cannot
2369+
prevent evaluation of an aggregate expression contained within it,
2370+
because aggregate expressions are computed before other
2371+
expressions in a <literal>SELECT</> list or <literal>HAVING</> clause
2372+
are considered. For example, the following query can cause a
2373+
division-by-zero error despite seemingly having protected against it:
2374+
<programlisting>
2375+
SELECT CASE WHEN min(employees) > 0
2376+
THEN avg(expenses / employees)
2377+
END
2378+
FROM departments;
2379+
</programlisting>
2380+
The <function>min()</> and <function>avg()</> aggregates are computed
2381+
concurrently over all the input rows, so if any row
2382+
has <structfield>employees</> equal to zero, the division-by-zero error
2383+
will occur before there is any opportunity to test the result of
2384+
<function>min()</>. Instead, use a <literal>WHERE</>
2385+
clause to prevent problematic input rows from
2386+
reaching an aggregate function in the first place.
2387+
</para>
23392388
</sect2>
23402389
</sect1>
23412390

0 commit comments

Comments
 (0)