Question on triggers and plpgsql

Поиск
Список
Период
Сортировка
От Carlos Moreno
Тема Question on triggers and plpgsql
Дата
Msg-id 4255AA15.8000508@mochima.com
обсуждение исходный текст
Ответы Re: Question on triggers and plpgsql  (John DeSoi <desoi@pgedit.com>)
Список pgsql-sql
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
--



В списке pgsql-sql по дате отправления:

Предыдущее
От: Jeff Boes
Дата:
Сообщение: Re: DROP TYPE without error?
Следующее
От: Enrico Weigelt
Дата:
Сообщение: Re: Table PARTITION