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

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

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: Tue, 22 Apr 2008 02:41:50 -0700 (PDT)
  • Message-id: <b26f6299-b09e-4bfb-850c-72fe2fa899e9(at)26g2000hsk(dot)googlegroups(dot)com>

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?




Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group