Re: trigger on table - Mailing list pgsql-sql

From Karel Zak
Subject Re: trigger on table
Date
Msg-id 20020214134318.A8046@zf.jcu.cz
Whole thread Raw
In response to trigger on table  ("Graham Vickrage" <graham@digitalplanit.com>)
Responses Re: trigger on table  ("Christopher Kings-Lynne" <chriskl@familyhealth.com.au>)
List pgsql-sql
On Thu, Feb 14, 2002 at 12:17:02PM -0000, Graham Vickrage wrote:
> I am trying to create a trigger on a table that simply sets the last_updated
> field when any updates are made to that table.
> 
> I have tried the following: -
> 
> CREATE FUNCTION set_item_last_updated () RETURNS OPAQUE AS '
> BEGIN
> UPDATE item SET last_updated = now(); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> RETURN OLD;
> END;' LANGUAGE 'plpgsql';
> 
> CREATE TRIGGER item_last_updated AFTER UPDATE ON item                                 ^^^^^^^
> FOR EACH ROW EXECUTE PROCEDURE set_item_last_updated();
> 
> When I try to execute this it hangs and postmaster eventually runs out of
> memory.
You create trigger on update and inside this trigger you do update 
again. It's cycle...
> Is there a way to do it just using sql not plpsql?
It works with PL/SQL, try:
CREATE FUNCTION set_item_last_updated () RETURNS OPAQUE AS '       BEGIN               NEW.last_updated := ''now'';
         RETURN NEW;       END;
 
' LANGUAGE 'plpgsql';
CREATE TRIGGER item_last_updated BEFORE UPDATE ON itemFOR EACH ROW EXECUTE PROCEDURE set_item_last_updated ();
       Karel

-- Karel Zak  <zakkr@zf.jcu.cz>http://home.zf.jcu.cz/~zakkr/C, PostgreSQL, PHP, WWW, http://docs.linux.cz,
http://mape.jcu.cz


pgsql-sql by date:

Previous
From: "PGExplorer"
Date:
Subject: Re: trigger on table
Next
From: "Ross J. Reedstrom"
Date:
Subject: Re: How long does it take?