pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Grittner <kgrittn@ymail.com>
To: Campbell, Lance <lance@illinois.edu>
To: pgsql-admin@postgresql.org <pgsql-admin@postgresql.org>
Subject: Re: update Timestamp updated whenever the table is updated
Date: Tue, 12 Feb 2013 14:00:42 -0800 (PST)
Message-ID: <1360706442.33888.YahooMailNeo@web162906.mail.bf1.yahoo.com> (raw)
In-Reply-To: <B75CD08C73BD3543B97E4EF3964B7D701F15773D@CITESMBX1.ad.uillinois.edu>
References: <B75CD08C73BD3543B97E4EF3964B7D701F15773D@CITESMBX1.ad.uillinois.edu>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-admin>

"Campbell, Lance" <lance@illinois.edu> wrote:

> 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?

> 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()
> );

A BEFORE UPDATE trigger is probably what you want.  You could write
a single trigger function which could be attached to all tables
with the updated_timestamp column.  For example:

CREATE FUNCTION set_updated_timestamp()
  RETURNS TRIGGER
  LANGUAGE plpgsql
AS $$
BEGIN
  NEW.updated_timestamp := now();
  RETURN NEW;
END;
$$;

Note that the above function depends on a column name, but not a
table name.  You link it to each table like this:

CREATE TRIGGER test_table_update_timestamp
  BEFORE UPDATE ON test_table
  FOR EACH ROW EXECUTE PROCEDURE set_updated_timestamp();

> I think I read something about RULES.

You generally want to avoid RULES, especially where a trigger works
so well.

-- 
Kevin Grittner
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


-- 
Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin



view thread (2+ messages)

Message-ID: <1360706442.33888.YahooMailNeo@web162906.mail.bf1.yahoo.com>
Permalink:  ../1360706442.33888.YahooMailNeo@web162906.mail.bf1.yahoo.com/
Also on:    postgresql.org/message-id/1360706442.33888.YahooMailNeo@web162906.mail.bf1.yahoo.com

 · 

reply

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-admin@postgresql.org
  Cc: kgrittn@ymail.com, lance@illinois.edu
  Subject: Re: update Timestamp updated whenever the table is updated
  In-Reply-To: <1360706442.33888.YahooMailNeo@web162906.mail.bf1.yahoo.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