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;