pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David Johnston <david.g.johnston@gmail.com>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #12273: CASE Expression BUG
Date: Thu, 18 Dec 2014 15:10:05 -0500
Message-ID: <2065.1418933405@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKFQuwbPwB92rKdqaXO8qyp2KV_oDKNX3Hzae4XSCYeHZLctag@mail.gmail.com>
References: <20141218144436.1237.96696@wrigleys.postgresql.org>
	<16630.1418918898@sss.pgh.pa.us>
	<1418921627518-5831325.post@n5.nabble.com>
	<17992.1418922882@sss.pgh.pa.us>
	<CAKFQuwbPwB92rKdqaXO8qyp2KV_oDKNX3Hzae4XSCYeHZLctag@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

David Johnston <david.g.johnston@gmail.com> writes:
> On Thu, Dec 18, 2014 at 10:14 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Hmm ... I'd just been looking at 4.2.14:
>> http://www.postgresql.org/docs/9.4/static/sql-expressions.html#SYNTAX-EXPRESS-EVAL
>> and thinking that maybe it should mention this.  Perhaps we ought to
>> relocate the text about constant subexpressions into 4.2.14 (and add an
>> example), and then link there from 9.17.1.

> +1

> Something like:
> Before "A limitation of this technique [...]"
> The are two limitations to this technique: planner optimizations may occur
> and aggregate expressions will be evaluated.

Yeah, I've just been working on some text to put there.  I'm still
wordsmithing it, but right now it's as attached.

> The question is how detailed do we need to get here...is it an issue
> specific to casting or is there some other interplay happening?

It's not particularly specific to casting, any constant subexpression that
could throw errors is at risk.  I'm using divide-by-zero as the canonical
example in this area.

			regards, tom lane

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

case-docs-fix.patch (text/x-diff, 3.5K) download diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index ef69b94..5ef1911 100644 *** a/doc/src/sgml/func.sgml --- b/doc/src/sgml/func.sgml *************** SELECT ... WHERE CASE WHEN x &lt;&gt; 0 *** 11179,11189 **** <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. </para> </note> </sect2> --- 11179,11192 ---- <note> <para> ! 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> ! does not evaluate any subexpressions that are not needed to determine ! the result</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 6f8b7e8..d2ece37 100644 *** a/doc/src/sgml/syntax.sgml --- b/doc/src/sgml/syntax.sgml *************** SELECT ... WHERE CASE WHEN x &gt; 0 THEN *** 2439,2445 **** </para> <para> ! A limitation of this technique is that a <literal>CASE</> cannot prevent evaluation of an aggregate expression contained within it, because aggregate expressions are computed before <quote>scalar</> expressions in a <literal>SELECT</> list or <literal>HAVING</> clause --- 2439,2472 ---- </para> <para> ! <literal>CASE</> is not a panacea 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 parameters and local variables ! can be inserted into queries as constants for planning purposes. ! Thus for example in <application>PL/pgSQL</> functions, 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</> expressions in a <literal>SELECT</> list or <literal>HAVING</> clause

view thread (10+ messages)  latest in thread

Message-ID: <2065.1418933405@sss.pgh.pa.us>
Permalink:  ../2065.1418933405@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2065.1418933405@sss.pgh.pa.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, david.g.johnston@gmail.com
  Subject: Re: BUG #12273: CASE Expression BUG
  In-Reply-To: <2065.1418933405@sss.pgh.pa.us>
  References: <20141218144436.1237.96696@wrigleys.postgresql.org> <16630.1418918898@sss.pgh.pa.us> <1418921627518-5831325.post@n5.nabble.com> <17992.1418922882@sss.pgh.pa.us> <CAKFQuwbPwB92rKdqaXO8qyp2KV_oDKNX3Hzae4XSCYeHZLctag@mail.gmail.com> <2065.1418933405@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox