Skip to content

Commit 0c54cb0

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 d168d0e commit 0c54cb0

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
@@ -9292,11 +9292,13 @@ SELECT ... WHERE CASE WHEN x &lt;&gt; 0 THEN y/x &gt; 1.5 ELSE false END;
92929292

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

doc/src/sgml/syntax.sgml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,6 +2208,55 @@ SELECT ... WHERE CASE WHEN x &gt; 0 THEN y/x &gt; 1.5 ELSE false END;
22082208
example, it would be better to sidestep the problem by writing
22092209
<literal>y &gt; 1.5*x</> instead.)
22102210
</para>
2211+
2212+
<para>
2213+
<literal>CASE</> is not a cure-all for such issues, however.
2214+
One limitation of the technique illustrated above is that it does not
2215+
prevent early evaluation of constant subexpressions.
2216+
As described in <xref linkend="xfunc-volatility">, functions and
2217+
operators marked <literal>IMMUTABLE</literal> can be evaluated when
2218+
the query is planned rather than when it is executed. Thus for example
2219+
<programlisting>
2220+
SELECT CASE WHEN x &gt; 0 THEN x ELSE 1/0 END FROM tab;
2221+
</programlisting>
2222+
is likely to result in a division-by-zero failure due to the planner
2223+
trying to simplify the constant subexpression,
2224+
even if every row in the table has <literal>x &gt; 0</> so that the
2225+
<literal>ELSE</> arm would never be entered at run time.
2226+
</para>
2227+
2228+
<para>
2229+
While that particular example might seem silly, related cases that don't
2230+
obviously involve constants can occur in queries executed within
2231+
functions, since the values of function arguments and local variables
2232+
can be inserted into queries as constants for planning purposes.
2233+
Within <application>PL/pgSQL</> functions, for example, using an
2234+
<literal>IF</>-<literal>THEN</>-<literal>ELSE</> statement to protect
2235+
a risky computation is much safer than just nesting it in a
2236+
<literal>CASE</> expression.
2237+
</para>
2238+
2239+
<para>
2240+
Another limitation of the same kind is that a <literal>CASE</> cannot
2241+
prevent evaluation of an aggregate expression contained within it,
2242+
because aggregate expressions are computed before other
2243+
expressions in a <literal>SELECT</> list or <literal>HAVING</> clause
2244+
are considered. For example, the following query can cause a
2245+
division-by-zero error despite seemingly having protected against it:
2246+
<programlisting>
2247+
SELECT CASE WHEN min(employees) > 0
2248+
THEN avg(expenses / employees)
2249+
END
2250+
FROM departments;
2251+
</programlisting>
2252+
The <function>min()</> and <function>avg()</> aggregates are computed
2253+
concurrently over all the input rows, so if any row
2254+
has <structfield>employees</> equal to zero, the division-by-zero error
2255+
will occur before there is any opportunity to test the result of
2256+
<function>min()</>. Instead, use a <literal>WHERE</>
2257+
clause to prevent problematic input rows from
2258+
reaching an aggregate function in the first place.
2259+
</para>
22112260
</sect2>
22122261
</sect1>
22132262

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy