update Timestamp updated whenever the table is updated

From: "Campbell, Lance" <lance(at)illinois(dot)edu>
To: "pgsql-admin(at)postgresql(dot)org" <pgsql-admin(at)postgresql(dot)org>
Subject: update Timestamp updated whenever the table is updated
Date: 2013-02-12 21:30:15
Message-ID: B75CD08C73BD3543B97E4EF3964B7D701F15773D@CITESMBX1.ad.uillinois.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

PostgreSQL 9.2.3
I would like to have a generic strategy for updating a timestamp field on some tables whenever the table is updated. Is there a recommended strategy for doing this other than via the SQL UPDATE command? I think I read something about RULES. In the below example I have a table called test_table. I would ideally like to update the field called "updated_timestamp" whenever an update occurs by doing:

UPDATE test_table SET updated_timestamp=now() WHERE id=?

While looking at the concept of RULES how do I say after an update happens update that particular record and not all of the records in test_table.

Example table:
CREATE TABLE test_table
(
id integer NOT NULL,
field1 character varying NOT NULL,
field2 character varying NOT NULL,
updated_timestamp timestamp with time zone DEFAULT now(),
created_timestamp timestamp with time zone DEFAULT now()
);

Thanks,

Lance Campbell
Software Architect
Web Services at Public Affairs
217-333-0382
[University of Illinois at Urbana-Champaign logo]<http://illinois.edu/>

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Kevin Grittner 2013-02-12 22:00:42 Re: update Timestamp updated whenever the table is updated
Previous Message Tom Lane 2013-02-07 19:48:18 Re: Postgres 9.1 statistics in pg_stat_database