From: Carlos Moreno <moreno@mochima.com>
To: pgsql-sql@postgresql.org
Subject: Question on triggers and plpgsql
Date: Thu, 07 Apr 2005 17:45:57 -0400
Message-ID: <4255AA15.8000508@mochima.com> (raw)
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
--
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: moreno@mochima.com
Subject: Re: Question on triggers and plpgsql
In-Reply-To: <4255AA15.8000508@mochima.com>
* 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