pl/pgsql short circuit evaluation?

From: david(at)gardnerit(dot)net
To: pgsql-novice(at)postgresql(dot)org
Subject: pl/pgsql short circuit evaluation?
Date: 2009-02-26 19:15:56
Message-ID: 20090226191556.GA3610@monster
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

First does pl/pgSQL do short circuit evaluation?

If yes, then I ran into a weird issue where I'm writing a trigger that
needs to perform an operation (in this case two other pl/pgsql functions)
on inserts, and updates but only if certain columns have changed.

The following works on updates, but fails on inserts because OLD is undefined:

CREATE OR REPLACE FUNCTION update_path_trigger() RETURNS trigger AS
$BODY$
declare
begin
IF (TG_OP = 'INSERT' OR NEW.parentuid<>OLD.parentuid OR NEW.name<>OLD.name) THEN
NEW.path=full_path(NEW.parentuid)||'/'||NEW.name;
PERFORM cascade_child_path(NEW);
END IF;

return NEW;
end;
$BODY$
LANGUAGE 'plpgsql' VOLATILE COST 1;

The following is a functional work-around, but I believe the above should have worked.:
CREATE OR REPLACE FUNCTION update_path_trigger() RETURNS trigger AS
$BODY$
declare
update_path BOOLEAN;
begin
IF TG_OP = 'INSERT' THEN
update_path := true;
ELSIF NEW.parentuid<>OLD.parentuid OR NEW.name<>OLD.name THEN
update_path := true;
ELSE
update_path := false;
END IF;

IF update_path THEN
NEW.path=full_path(NEW.parentuid)||'/'||NEW.name;
PERFORM cascade_child_path(NEW);
END IF;

return NEW;
end;
$BODY$
LANGUAGE 'plpgsql' VOLATILE COST 1;

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2009-02-26 19:56:50 Re: pl/pgsql short circuit evaluation?
Previous Message John DeSoi 2009-02-26 14:20:44 Re: Version details for psql/postmaster