Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y1hOm-0003OA-Uf for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Dec 2014 20:10:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y1hOm-0002Th-Ev for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Dec 2014 20:10:12 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y1hOl-0002Sh-Iz for pgsql-bugs@postgresql.org; Thu, 18 Dec 2014 20:10:11 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y1hOh-0004dd-V5 for pgsql-bugs@postgresql.org; Thu, 18 Dec 2014 20:10:09 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id sBIKA5il002066; Thu, 18 Dec 2014 15:10:05 -0500 From: Tom Lane To: David Johnston cc: "pgsql-bugs@postgresql.org" Subject: Re: BUG #12273: CASE Expression BUG In-reply-to: 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> Comments: In-reply-to David Johnston message dated "Thu, 18 Dec 2014 12:40:26 -0700" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2030.1418933322.0@sss.pgh.pa.us> Date: Thu, 18 Dec 2014 15:10:05 -0500 Message-ID: <2065.1418933405@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="US-ASCII" Content-ID: <2030.1418933322.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable David Johnston writes: > On Thu, Dec 18, 2014 at 10:14 AM, Tom Lane wrote: >> Hmm ... I'd just been looking at 4.2.14: >> http://www.postgresql.org/docs/9.4/static/sql-expressions.html#SYNTAX-EX= PRESS-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 ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="case-docs-fix.patch"; charset="us-ascii" Content-ID: <2030.1418933322.2@sss.pgh.pa.us> Content-Description: case-docs-fix.patch Content-Transfer-Encoding: quoted-printable 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 <> 0=20 *** 11179,11189 **** =20=20 ! As described in , functions and ! operators marked IMMUTABLE 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. --- 11179,11192 ---- =20=20 ! As described in , there are va= rious ! situations in which subexpressions of an expression are evaluated at ! different times, so that the principle that CASE ! does not evaluate any subexpressions that are not needed to determine ! the result is not ironclad. For example a ! constant 1/0 subexpression will usually result in a ! division-by-zero failure at planning time, even if it's within ! a CASE arm that would never be entered at run time. 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 > 0 THEN *** 2439,2445 **** =20=20 ! A limitation of this technique is that a CASE cannot prevent evaluation of an aggregate expression contained within it, because aggregate expressions are computed before scalar expressions in a SELECT list or HAVING clause --- 2439,2472 ---- =20=20 ! 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 , functions and ! operators marked IMMUTABLE can be evaluated when ! the query is planned rather than when it is executed. Thus for examp= le ! ! SELECT CASE WHEN x > 0 THEN x ELSE 1 / 0 END FROM tab; ! ! 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 x > 0 so that the ! ELSE arm would never be entered at run time. ! !=20 ! ! While that particular example might seem silly, related cases that do= n'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 PL/pgSQL functions, using an ! IF-THEN-ELSE statement to protect ! a risky computation is much safer than just nesting it in a ! CASE expression. ! !=20 ! ! Another limitation of the same kind is that a CASE cannot prevent evaluation of an aggregate expression contained within it, because aggregate expressions are computed before scalar expressions in a SELECT list or HAVING clause ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--