X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 857055351A for ; Thu, 7 Apr 2005 22:44:06 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 21818-07 for ; Thu, 7 Apr 2005 21:43:29 +0000 (GMT) Received: from relais.videotron.ca (relais.videotron.ca [24.201.245.36]) by svr1.postgresql.org (Postfix) with ESMTP id 57C5453239 for ; Thu, 7 Apr 2005 22:43:28 +0100 (BST) Received: from [192.168.0.2] ([24.201.88.196]) by VL-MO-MR011.ip.videotron.ca (iPlanet Messaging Server 5.2 HotFix 1.21 (built Sep 8 2003)) with ESMTP id <0IEL00AX2IZMC3@VL-MO-MR011.ip.videotron.ca> for pgsql-sql@postgresql.org; Thu, 07 Apr 2005 17:42:58 -0400 (EDT) Date: Thu, 07 Apr 2005 17:45:57 -0400 From: Carlos Moreno Subject: Question on triggers and plpgsql To: pgsql-sql@postgresql.org Message-id: <4255AA15.8000508@mochima.com> MIME-version: 1.0 Content-type: text/plain; charset=us-ascii; format=flowed Content-transfer-encoding: 7BIT X-Accept-Language: en-us, en User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7) Gecko/20040616 X-Virus-Scanned: by amavisd-new at hub.org X-Archive-Number: 200504/82 X-Sequence-Number: 20975 Hello, A question from a complete newbie on this area. I'm trying to implement a mechanism that would allow me to keep track of the last time each row of a table was modified. I have many applications modifying the data, and I would like to avoid having to modify each of those applications (with the risk of forgetting one of them). So, I figured a better approach would be a trigger that gets activated on update (before update, to be specific). Below is what I came up with, but being the very first time I do (or even read about) something with triggers or with plpgsql, I'd like to check if there are any obvious red flags, or if what I'm doing is hopelessly wrong. I added a column last_modified (timestamp data type), and create the following function: create function set_last_modified() returns trigger as ' begin new.last_modified = now(); return new; end; ' language plpgsql; (this is similar to an example from the PG documentation; I'm not sure the keyword "new" is the right thing to use in my case, but it would look like it's a standard way to refer to the "new row" that is about to replace the old one) Then, I created the trigger as follows: create trigger last_modified_on_update before update on table_name for each row execute procedure set_last_modified(); The thing seems to work -- I had to go in a shell as user postgres and execute the command: $ createlang -d dbname plpgsql (I'm not sure I understand why that is necessary, or what implications -- positive or negative -- it may have) Am I doing the right thing? Have I introduced some sort of catastrophe waiting to happen? Thanks for any guidance you may offer to this PL/PGSQL beginner! Carlos --