Re: [GENERAL] CASE in where statement. BUG ?? - Mailing list pgsql-bugs

From Tom Lane
Subject Re: [GENERAL] CASE in where statement. BUG ??
Date
Msg-id 9626.1071848439@sss.pgh.pa.us
Whole thread Raw
In response to Re: [GENERAL] CASE in where statement. BUG ??  (Marek Lewczuk <newsy@lewczuk.com>)
List pgsql-bugs
Marek Lewczuk <newsy@lewczuk.com> writes:
> Well, I must say that it is wird action... Why to check both elements of
> CASE expression, if we know for sure that only one will be executed ?

It cannot discover that the WHEN clause is constant-false until after it
has performed syntactic analysis --- which includes assigning datatypes
to all elements of the expression.

In theory perhaps we could do syntactic analysis of the WHEN part, then
stop and do constant-folding to see if we could prove that the WHEN is
always false before we move on to syntactic analysis of the THEN part.
In practice, no one does it that way --- it would arguably be illegal to
do so according to the SQL spec, which draws a very clear distinction
between parse-time checking and execution-time activity.  Early constant
folding would have unpleasant properties for user-defined functions,
too.  You might find that a change you make in one of your functions
doesn't show up in the behavior of some existing rule, because the call
to that function in the rule got constant-folded before the rule was
stored.

            regards, tom lane

pgsql-bugs by date:

Previous
From: "Russell Garrett"
Date:
Subject: Re: Urgent: Key constraints behaving weirdly
Next
From: Rich Hall
Date:
Subject: Re: plpgsql Integer Concat To String