[Cialug] Postgres trigger help.

Kevin C. Smith kevin at linuxsmith.com
Tue Aug 14 18:12:42 CDT 2007


>
> CREATE FUNCTION set_last_update() RETURNS opaque AS '
> BEGIN
> NEW.last_updated:=now();
> RETURN NEW;
> END;
> ' LANGUAGE 'plpgsql';
>
> CREATE TRIGGER set_last_update AFTER INSERT OR UPDATE ON people FOR EACH
> ROW
> EXECUTE PROCEDURE set_last_update();
>

Okay I see maybe two issues (disclaimer I don't know what the hell I'm
talking
about).

Wouldn't a trigger AFTER insert or update become in infinite loop, and maybe
postgresql therefore ignores it? Just a guess.

I also think that were you have 'opaque' you need 'trigger'.

I tested this and it worked:


CREATE OR REPLACE FUNCTION set_last_update()
  RETURNS "trigger" AS
BEGIN
  NEW.last_updated = now();
  RETURN NEW;
END;
  LANGUAGE 'plpgsql' VOLATILE;


CREATE TRIGGER set_last_update
  BEFORE INSERT OR UPDATE
  ON test_table
  FOR EACH ROW
  EXECUTE PROCEDURE set_last_update();


Good luck

-- 
Kevin C. Smith



More information about the Cialug mailing list