Search Postgresql Archives

Re: update trigger not working

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx
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


---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

               http://archives.postgresql.org

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux