plpgsql and logical expression evaluation - Mailing list pgsql-general

From wstrzalka
Subject plpgsql and logical expression evaluation
Date
Msg-id b26f6299-b09e-4bfb-850c-72fe2fa899e9@26g2000hsk.googlegroups.com
Whole thread Raw
Responses Re: plpgsql and logical expression evaluation  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
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.
So the B will be not checked. In plpgsql it's different - all the
expressions are evaluated.


Why I don't like it? One of the samples is trigger procedure called
with body like this:

IF (TG_OP = 'INSERT' OR (TG_OP = 'UPDATE' AND NEW.status <>
OLD.status)) THEN
        -- DO SOMETHING
END IF;

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;


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


pgsql-general by date:

Previous
From: mateo21@siteduzero.com
Date:
Subject: Bitmap Heap Scan takes a lot of time
Next
From: "Corin Schedler"
Date:
Subject: tsearch2 problem