Re: trigger on table

From: Karel Zak <zakkr(at)zf(dot)jcu(dot)cz>
To: Graham Vickrage <graham(at)digitalplanit(dot)com>
Cc: Postgres SQL <pgsql-sql(at)postgresql(dot)org>
Subject: Re: trigger on table
Date: 2002-02-14 12:43:18
Message-ID: 20020214134318.A8046@zf.jcu.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 item
FOR EACH ROW EXECUTE PROCEDURE set_item_last_updated ();

Karel

--
Karel Zak <zakkr(at)zf(dot)jcu(dot)cz>
http://home.zf.jcu.cz/~zakkr/

C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Ross J. Reedstrom 2002-02-14 16:43:41 Re: How long does it take?
Previous Message PGExplorer 2002-02-14 12:28:13 Re: trigger on table