pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Karel Zak <zakkr@zf.jcu.cz>
To: Graham Vickrage <graham@digitalplanit.com>
Cc: Postgres SQL <pgsql-sql@postgresql.org>
Subject: Re: trigger on table
Date: Thu, 14 Feb 2002 13:43:18 +0100
Message-ID: <20020214134318.A8046@zf.jcu.cz> (raw)
In-Reply-To: <NDBBJABDILOPAOOMFJHOIEMHDDAA.graham@digitalplanit.com>
References: <NDBBJABDILOPAOOMFJHOIEMHDDAA.graham@digitalplanit.com>

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@zf.jcu.cz>
 http://home.zf.jcu.cz/~zakkr/
 
 C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz



view thread (5+ messages)  latest in thread

Message-ID: <20020214134318.A8046@zf.jcu.cz>
Permalink:  ../20020214134318.A8046@zf.jcu.cz/
Also on:    postgresql.org/message-id/20020214134318.A8046@zf.jcu.cz

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: zakkr@zf.jcu.cz, graham@digitalplanit.com
  Subject: Re: trigger on table
  In-Reply-To: <20020214134318.A8046@zf.jcu.cz>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox