summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2014-12-18 16:38:55 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2014-12-18 16:38:55 -0500
commit2b8f74d79722ecf7c27445f088d0e50bcb81ed06 (patch)
treead46951d39df996480fbd0659a3f68848d262a72 /doc
parent4a48b4cb390656471ddf52f469ecb56d4e399df1 (diff)
downloadpostgresql-2b8f74d79722ecf7c27445f088d0e50bcb81ed06.tar.gz
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 0ce627d4 about CASE versus aggregate functions. Tom Lane and David Johnston, per discussion of bug #12273.
Diffstat (limited to 'doc')
-rw-r--r--doc/src/sgml/func.sgml12
-rw-r--r--doc/src/sgml/syntax.sgml31
2 files changed, 36 insertions, 7 deletions
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 386fd425b0..1d597d4d1b 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -11124,11 +11124,13 @@ SELECT ... WHERE CASE WHEN x &lt;&gt; 0 THEN y/x &gt; 1.5 ELSE false END;
<note>
<para>
- As described in <xref linkend="xfunc-volatility">, functions and
- operators marked <literal>IMMUTABLE</literal> can be evaluated when
- the query is planned rather than when it is executed. This means
- that constant parts of a subexpression that is not evaluated during
- query execution might still be evaluated during query planning.
+ As described in <xref linkend="syntax-express-eval">, there are various
+ situations in which subexpressions of an expression are evaluated at
+ different times, so that the principle that <quote><token>CASE</token>
+ evaluates only necessary subexpressions</quote> is not ironclad. For
+ example a constant <literal>1/0</> subexpression will usually result in
+ a division-by-zero failure at planning time, even if it's within
+ a <token>CASE</token> arm that would never be entered at run time.
</para>
</note>
</sect2>
diff --git a/doc/src/sgml/syntax.sgml b/doc/src/sgml/syntax.sgml
index 399ae07075..8284519df9 100644
--- a/doc/src/sgml/syntax.sgml
+++ b/doc/src/sgml/syntax.sgml
@@ -2428,9 +2428,36 @@ SELECT ... WHERE CASE WHEN x &gt; 0 THEN y/x &gt; 1.5 ELSE false END;
</para>
<para>
- A limitation of this technique is that a <literal>CASE</> cannot
+ <literal>CASE</> is not a cure-all for such issues, however.
+ One limitation of the technique illustrated above is that it does not
+ prevent early evaluation of constant subexpressions.
+ As described in <xref linkend="xfunc-volatility">, functions and
+ operators marked <literal>IMMUTABLE</literal> can be evaluated when
+ the query is planned rather than when it is executed. Thus for example
+<programlisting>
+SELECT CASE WHEN x &gt; 0 THEN x ELSE 1/0 END FROM tab;
+</programlisting>
+ is likely to result in a division-by-zero failure due to the planner
+ trying to simplify the constant subexpression,
+ even if every row in the table has <literal>x &gt; 0</> so that the
+ <literal>ELSE</> arm would never be entered at run time.
+ </para>
+
+ <para>
+ While that particular example might seem silly, related cases that don't
+ obviously involve constants can occur in queries executed within
+ functions, since the values of function arguments and local variables
+ can be inserted into queries as constants for planning purposes.
+ Within <application>PL/pgSQL</> functions, for example, using an
+ <literal>IF</>-<literal>THEN</>-<literal>ELSE</> statement to protect
+ a risky computation is much safer than just nesting it in a
+ <literal>CASE</> expression.
+ </para>
+
+ <para>
+ Another limitation of the same kind is that a <literal>CASE</> cannot
prevent evaluation of an aggregate expression contained within it,
- because aggregate expressions are computed before <quote>scalar</>
+ because aggregate expressions are computed before other
expressions in a <literal>SELECT</> list or <literal>HAVING</> clause
are considered. For example, the following query can cause a
division-by-zero error despite seemingly having protected against it: