Re: plpgsql and logical expression evaluation

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: wstrzalka <wstrzalka(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: plpgsql and logical expression evaluation
Date: 2008-04-23 08:39:54
Message-ID: 20080423083954.GA16761@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Apr 22, 2008 at 02:41:50AM -0700, wstrzalka wrote:
> One of the annoying things in plpgsql is logical expression
> evaluation.
>
> In most (all??) languages I know, logical expression like:
>
> if ( [A_true_expression] or [B_false_expression] ) then
>
> will stop evaluating when the A expression will be evaluated as a
> TRUE.

I guess you should complain to the SQL design comittee, SQL is a
declarative language not a procedural language and the order of the
expressions in AND/OR is not important to the evaluation.

> It don't work for insert as the part designed for UPDATE will be
> evaluated, while there is no OLD for an insert operation.
> So the code looks like that:
>
> IF (TG_OP = 'INSERT') THEN
> -- DO SOMETHING
> ELSIF (TG_OP = 'UPDATE' AND NEW.status <> OLD.status) THEN
> -- DO THE SAME AS ABOVE
> END IF;

You could use a flag variable.

> Is there any reason for that like side effects (please give me any
> example?) or it's just not yet done optimization?

The spec explicitly says it doesn't matter and it removes the
possibility of optimisation (in many queries it is important that the
optimiser can execute conditions in any order it likes).

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2008-04-23 08:42:16 Re: Updating with a subselect
Previous Message A. Kretschmer 2008-04-23 08:38:50 Re: Updating with a subselect