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

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 (view raw or flat)
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

pgsql-novice by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group