Re: Automatic mod time?

From: "Jules Alberts" <julesa(at)arbodienst-limburg(dot)nl>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Automatic mod time?
Date: 2002-03-13 09:19:26
Message-ID: 200203130926.g2D9QEfd020929@artemis.cuci.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 11 Mar 2002 at 15:50, Steve Lane wrote:

> It seems like this should be simple but I haven¹t found any obvious
> pointer in the documents.
>
> I need a timestamp field that¹s updated every time a record is touched,
> to show the last mod date.
>
> I have this working with a trigger just fine. But this means adding a
> new trigger for each table that needs this.
>
> Is there an easier way?

hi Steve,

i'm a complete newbie, so use this at your own risk... i ran into the
same question (+ i also want to keep track of the user touching the
record), and did it with inheritance:

---------------------------------------------------
-- this is a templatetable for the audit fields
create table au_col (
mut_id varchar(100) not null default current_user,
mut_timestamp timestamp not null default CURRENT_TIMESTAMP
);

-- function for updating the values
create function au_col()
returns opaque
as 'begin
old.mut_id = current_user;
old.mut_timestamp = CURRENT_TIMESTAMP;
return old;
end;'
language 'plpgsql';

-- trigger to call the function
create trigger au_col
before update or delete
on au_col
for each row
execute procedure au_col();

-- now, for every table you create, inherit it from au_col
create table land (
id serial primary key,
code char(2) unique not null,
name varchar(100) not null
) inherits (au_col);
---------------------------------------------------

HTH, HAND,

--
Jules

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Boris Köster 2002-03-13 09:25:09 duplicating and date problem
Previous Message Martijn van Oosterhout 2002-03-13 08:53:48 Re: transacction problem