Automatically updating a timestamp upon UPDATE query?

From: Ed Loehr <eloehr(at)austin(dot)rr(dot)com>
To: pggeneral <pgsql-general(at)postgreSQL(dot)org>
Subject: Automatically updating a timestamp upon UPDATE query?
Date: 2001-01-12 17:59:54
Message-ID: 3A5F461A.DFCCB1DA@austin.rr.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

How do you most easily create a timestamp column that updates to the
current time on every UPDATE statement? I know you can use triggers, but
I thought there was a default value that did this for you. 'current'
does not seem appropriate, nor have I made it work (see below for my
example).

http://www.postgresql.org/docs/postgres/x1137.htm :
"'now' is resolved when the value is inserted, 'current' is
resolved everytime the value is retrieved."

Any clues?

Regards,
Ed Loehr

BTW, here's the sequence I used to conclude 'current' doesn't seem to
work:

DROP TABLE foo;
CREATE TABLE foo (
note VARCHAR,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP NOT NULL DEFAULT 'current'
);

SELECT now();
INSERT INTO foo (note) VALUES ('Default timestamp test record');
SELECT created FROM foo;
SELECT now();
SELECT * FROM foo;

-- Wait a few seconds for the time to clearly change seconds...

SELECT now();
UPDATE foo SET note = 'Did the update_time change with an UPDATE query?';
SELECT now();
SELECT * FROM foo;
SELECT updated FROM foo;

Responses

Browse pgsql-general by date

  From Date Subject
Next Message hedy Xu 2001-01-12 18:51:43 A pg_dump error.
Previous Message The Hermit Hacker 2001-01-12 17:53:10 Re: Re: mnogosearch -- pgsql seem so slow, please help me find out why