Re: simple auto-updating timestamp ?

From: Andreas <maps(dot)on(at)gmx(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: simple auto-updating timestamp ?
Date: 2003-12-29 18:06:16
Message-ID: 3FF06D18.6020203@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

D. Dante Lorenso wrote:

> You can do this by adding a trigger to your table. Just define the
> trigger
> to be invoked on INSERT and UPDATE for your table. The trigger
> definition
> would look something like this: [...]

Thanks.
So far that works for one table.

Can I have this behaviour somehow inherited by child-tables ?
Like:
CREATE TABLE objects (
id integer primary key,
created_ts timestamp(0) DEFAULT LOCALTIMESTAMP,
update_ts timestamp(0),
deleted_ts timestamp(0), -- things get ignored in normal
processing
...
);

Then create a trigger as in your example that updates this timestamp.

Every other table in the db would inherit (objects) to get those
standard fields that I'd like to have everywhere. It'd be nice not
having to bother about the "methods" of the objects-class for every
child-class.

> CREATE FUNCTION "public"."set_update_ts" () RETURNS trigger AS'
> BEGIN
> NEW.update_ts = NOW();
> RETURN NEW;
> END; 'LANGUAGE 'plpgsql' IMMUTABLE CALLED ON NULL INPUT SECURITY
> INVOKER;

I entered your code into psql and checked it afterwards with pgadmin3.
pgadmin shows some parts different to the code that I pushed through psql :
1) create OR REPLACE ...
2) immuntable; <-- End of line What does this part behind
"immutable" do ?

> Of course, this function would end up setting the update_ts to NOW()
> every
> time you insert or update your table. And you could never set the value
> to anything other than NOW() because your trigger would catch it and
> set it
> back to NOW() again.

That is what I had in mind. I'd like to see when there was the last
update to a record.

... Andreas

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-12-29 18:50:41 Re: An out of memory error when doing a vacuum full
Previous Message Sebastian Böck 2003-12-29 17:36:27 Re: what about uniqueness of inherited primary keys