From: Tom Lane <tgl@sss.pgh.pa.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: harry.townsend@eflowglobal.com, "pgsql-bugs@postgresql.org" <pgsql-bugs@postgresql.org>
Subject: Re: BUG #13973: Constants resolved in then/else clauses
Date: Thu, 18 Feb 2016 12:01:09 -0500
Message-ID: <30407.1455814869@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKFQuwbQbf1JCJnKnf0eoRp2sCrk2fKNFDdkXiK2_PHxQQy0Jw@mail.gmail.com>
References: <20160218135755.2655.72680@wrigleys.postgresql.org>
<CAKFQuwbQbf1JCJnKnf0eoRp2sCrk2fKNFDdkXiK2_PHxQQy0Jw@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Thu, Feb 18, 2016 at 6:57 AM, <harry.townsend@eflowglobal.com> 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 <test condition> 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
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 #13973: Constants resolved in then/else clauses
In-Reply-To: <30407.1455814869@sss.pgh.pa.us>
References: <20160218135755.2655.72680@wrigleys.postgresql.org> <CAKFQuwbQbf1JCJnKnf0eoRp2sCrk2fKNFDdkXiK2_PHxQQy0Jw@mail.gmail.com> <30407.1455814869@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