pl/pgsql short circuit evaluation? - Mailing list pgsql-novice

From david@gardnerit.net
Subject pl/pgsql short circuit evaluation?
Date
Msg-id 20090226191556.GA3610@monster
Whole thread Raw
Responses Re: pl/pgsql short circuit evaluation?
List 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;

pgsql-novice by date:

Previous
From: John DeSoi
Date:
Subject: Re: Version details for psql/postmaster
Next
From: Tom Lane
Date:
Subject: Re: pl/pgsql short circuit evaluation?