Re: Evaluation of if conditions - Mailing list pgsql-sql

From Tom Lane
Subject Re: Evaluation of if conditions
Date
Msg-id 28985.1157602218@sss.pgh.pa.us
Whole thread Raw
In response to Evaluation of if conditions  (Daniel CAUNE <d.caune@free.fr>)
List pgsql-sql
Daniel CAUNE <d.caune@free.fr> writes:
> How does the IF statement evaluate conditions?  Does it evaluate conditions
> following their declaration order from left to right?  In case of
> or-conditions, does the IF statement stop evaluating conditions whenever a
> first or-condition is true?

> The following snippet seems to be invalid, which let me think that PL/PGSQL
> evaluates all the conditions:

>   IF (TG_OP = 'INSERT') OR
>      (OLD.bar = ...) THEN

It's not that all the conditions get evaluated by an OR, it's that
plpgsql needs to send all the parameter values that the IF-expression
needs down to the core SQL engine.  So it fails on "OLD.bar" not being
defined, long before the expression evaluator gets to think about
whether TG_OP = 'INSERT' or not.

So, yeah, you want to rewrite it as two separate IF-tests.
        regards, tom lane


pgsql-sql by date:

Previous
From: Joe
Date:
Subject: Re: Evaluation of if conditions
Next
From: Kaloyan Iliev
Date:
Subject: Problem with FOR UPDATE