Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aWRx9-0002ML-DH for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Feb 2016 17:01:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aWRx9-0000IF-00 for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Feb 2016 17:01:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aWRx8-0000Fd-4A for pgsql-bugs@postgresql.org; Thu, 18 Feb 2016 17:01:18 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aWRx3-0002tV-Qo for pgsql-bugs@postgresql.org; Thu, 18 Feb 2016 17:01:17 +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 u1IH19ai030408; Thu, 18 Feb 2016 12:01:09 -0500 From: Tom Lane To: "David G. Johnston" cc: harry.townsend@eflowglobal.com, "pgsql-bugs@postgresql.org" Subject: Re: BUG #13973: Constants resolved in then/else clauses In-reply-to: References: <20160218135755.2655.72680@wrigleys.postgresql.org> Comments: In-reply-to "David G. Johnston" message dated "Thu, 18 Feb 2016 09:48:51 -0700" Date: Thu, 18 Feb 2016 12:01:09 -0500 Message-ID: <30407.1455814869@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 "David G. Johnston" writes: > On Thu, Feb 18, 2016 at 6:57 AM, wrote: >> I attempted to create a safety check in a query using a "case when" >> statement such that if the condition evaluated to false, it would return (1 >> / 0) in order to nullify the entire transaction. > ​So, there is a note in the documentation that exactly addresses what you > are trying to do....​ > http://www.postgresql.org/docs/current/static/functions-conditional.html > ​""" > ​As described in Section 4.2.14, there are various situations in which > subexpressions of an expression are evaluated at different times, so that > the principle that "CASE evaluates only necessary subexpressions" 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. > """ (emphasis mine) Yeah. What you need to do is ensure that the failure-causing thing doesn't look like a constant subexpression. I'd suggest a more useful approach is create function fail() returns int as $$begin raise exception ...; end$$ language plpgsql volatile; .... CASE WHEN THEN 0 ELSE fail() END ... The "volatile" marker on the function teaches the planner that the function has side-effects (viz, an exception) and so must not be speculatively evaluated. This'd also have the advantage of producing a much more useful error message (you might wanna consider adding parameters to the function, such as text to go into the error message). 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