However, making sure it's always updated can be tedious. This is where Triggers in Postgres comes in handy.
CREATE OR REPLACE FUNCTION update_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.timestamp = now();
RETURN NEW;
END;
$$ language 'plpgsql';
This only needs to be created once for the entire database if all tables use the same name for this column as a naming convention. Note that "NEW" is a special context / environment variable created on each call. It holds the "new database row for INSERT/UPDATE operations in row-level triggers".
Then for each table (e.g. book):
CREATE TRIGGER book_timestamp BEFORE INSERT OR UPDATE ON book
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
References:
- http://www.revsys.com/blog/2006/aug/04/a
utomatically-updating-a-timestamp-column-i n-postgresql/ - http://www.postgresql.org/docs/8.2/stati
c/plpgsql-trigger.html
No comments:
Post a Comment