Skip site navigation (1) Skip section navigation (2)

plpgsql and logical expression evaluation

From: wstrzalka <wstrzalka(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: plpgsql and logical expression evaluation
Date: 2008-04-22 09:41:50
Message-ID: b26f6299-b09e-4bfb-850c-72fe2fa899e9@26g2000hsk.googlegroups.com (view raw or flat)
Thread:
Lists: 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?


Responses

pgsql-general by date

Next:From: D. Dante LorensoDate: 2008-04-22 10:01:41
Subject: How to modify ENUM datatypes?
Previous:From: Clemens SchwaighoferDate: 2008-04-22 08:43:23
Subject: Re: Postgres Encoding conversion problem

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group