Re: entry log

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Robin Helgelin <lobbin(at)gmail(dot)com>
Cc: "PostgreSQL general" <pgsql-general(at)postgresql(dot)org>
Subject: Re: entry log
Date: 2007-08-19 20:22:33
Message-ID: 9C99915F-2707-4208-8AE6-EAB36A311F7A@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Aug 19, 2007, at 14:04 , Robin Helgelin wrote:

> When I started with MySQL I exploited their "bug" with timestamp
> fields and always had a entered and updated field on my tables.
>
As I'm blissfully ignorant of MySQL's peculiarities, without a more
detailed explanation of what you're trying to do, I'm not sure if
this suggestion will help, but here I go anyway:

If you want created and updated timestamps, you can do something like
this:

CREATE TABLE foos
(
foo text PRIMARY KEY
, created_at TIMESTAMP WITH TIME ZONE NOT NULL
DEFAULT CURRENT_TIMESTAMP
, updated_at TIMESTAMP WITH TIME ZONE NOT NULL
DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO foos (foo) VALUES ('a foo');
SELECT *
FROM foos;
foo | created_at | updated_at
-------+-------------------------------+-------------------------------
a foo | 2007-08-19 15:18:27.271103-05 | 2007-08-19 15:18:27.271103-05
(1 row)

UPDATE foos
SET updated_at = DEFAULT
, foo = 'foo b'
WHERE foo = 'a foo';
SELECT *
FROM foos;
foo | created_at | updated_at
-------+-------------------------------+-------------------------------
foo b | 2007-08-19 15:18:27.271103-05 | 2007-08-19 15:18:35.575783-05
(1 row)

> My question, is this interesting information enough to save on the
> table itself? If so, I guess this could easily be solved with a
> trigger, however, should one instead create a log table and log
> changes, etc?

As you mention, you could use a trigger instead of explicitly setting
updated_at to DEFAULT, which might be more convenient because you
don't need remember to set the updated_at column explicitly on update.

Whether or not this information is *interesting* is really up to the
specifics of your application, rather than answerable in a general
sense.

Hope that helps.

Michael Glaesemann
grzm seespotcode net

In response to

  • entry log at 2007-08-19 19:04:58 from Robin Helgelin

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Azevedo 2007-08-19 20:24:45 POSTGRE CRASH AND CURRVAL PROBLEM HELP!
Previous Message Bill Thoen 2007-08-19 19:40:18 Re: Searching for Duplicates and Hosed the System