Re: update trigger not working - Mailing list pgsql-general

From Oliver Elphick
Subject Re: update trigger not working
Date
Msg-id 1129776753.19925.49.camel@linda.lfix.co.uk
Whole thread Raw
In response to update trigger not working  (CSN <cool_screen_name90001@yahoo.com>)
List pgsql-general
On Wed, 2005-10-19 at 19:26 -0700, CSN wrote:
> I'm trying to set up a trigger that simply updates a
> field's corresponding timestamp to now() whenever the
> field is updated. But it's not working. Trying to
> debug, I commented out the inner IF and END and the
> log seemed to indicate infinite recursion occurred.

Yes.  On update, you are updating again...for ever.  What you need to do
is to modify the NEW record in a BEFORE trigger.

> CREATE or REPLACE function update_ts() returns trigger
> as $end$
>
> BEGIN
>
> IF (TG_OP='UPDATE') THEN
>
>     IF (OLD.stuff != NEW.stuff) THEN
>         UPDATE table1
>             set stuff_ts=now()
>             where id=NEW.id;

Instead of this, just do:

                  NEW.stuff_ts = now();

>     END IF;
>
> END IF;
>
> RETURN NULL;

RETURN NEW;

>
> END;
>
> $end$ language plpgsql;
>
> CREATE TRIGGER update_ts AFTER UPDATE ON table1 FOR

and make this a BEFORE trigger

> EACH ROW EXECUTE PROCEDURE update_ts();

--
Oliver Elphick                                          olly@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
GPG: 1024D/A54310EA  92C8 39E7 280E 3631 3F0E  1EC0 5664 7A2F A543 10EA
                 ========================================
   Do you want to know God?   http://www.lfix.co.uk/knowing_god.html


pgsql-general by date:

Previous
From: Mike Nolan
Date:
Subject: Re: update trigger not working
Next
From: Douglas McNaught
Date:
Subject: Re: update trigger not working