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?