Re: BUG #13973: Constants resolved in then/else clauses

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: harry(dot)townsend(at)eflowglobal(dot)com
Cc: "pgsql-bugs(at)postgresql(dot)org" <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #13973: Constants resolved in then/else clauses
Date: 2016-02-18 16:48:51
Message-ID: CAKFQuwbQbf1JCJnKnf0eoRp2sCrk2fKNFDdkXiK2_PHxQQy0Jw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Thu, Feb 18, 2016 at 6:57 AM, <harry(dot)townsend(at)eflowglobal(dot)com> wrote:

> The following bug has been logged on the website:
>
> Bug reference: 13973
> Logged by: Harry Townsend
> Email address: harry(dot)townsend(at)eflowglobal(dot)com
> PostgreSQL version: 9.1.20
> Operating system: Windows Server 2008
> Description:
>
> 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. With small conditions,
> this
> works fine. With larger ones, it seems that the constants in the "else"
> clause (didn't test for "then" clause) are evaluated before the condition,
> causing it to fail every time. If the "else" part requires a column value,
> it seems to force the condition to evaluate first though.
>

​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)

​So, not a bug.

David J.

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Alvaro Herrera 2016-02-18 16:50:52 Re: BUG #13973: Constants resolved in then/else clauses
Previous Message Alvaro Herrera 2016-02-18 16:44:43 Re: BUG #13966: Documentation. 'Select' description