Re: Keeping historical record changes w/ triggers - best practices?

From: Greg Donald <gdonald(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Keeping historical record changes w/ triggers - best practices?
Date: 2013-02-06 19:10:54
Message-ID: CAO+WgCYQiZDmbj7Cp1MMeXjBY4g6P6Ye+gmWr1Pay910PpDjVA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Feb 6, 2013 at 12:41 PM, Wells Oliver <wellsoliver(at)gmail(dot)com> wrote:
> I have a wide-ish table with 60 columns. I want to make a copy of data
> whenever a record is updated or deleted.
>
> Right now I have a table that's almost identical but with a 'created' column
> (timestamp) and an 'action' column (which gets TG_OP for UPDATE or DELETE).
>
> My idea would be to sort on the created column to see the historical record
> by comparing the columns. My other thought is to create two columns for each
> column in the master table (old_column, new_column, etc), storing the old
> values and the new values, and see what's changed that way.
>
> The other idea, probably a terrible idea, was to use hstore to create a list
> of the old values and new values, and have this history table just be the
> timestamp, action, and two hstore columns.
>
> Surely this has been done thousands of times. What are the thoughts
> regarding best practices in PG?

I handle this using middleware outside the db. Past revisions of a
record (from any table I want to track) are serialized into a JSON
format and stored in a single table. Postgres speaks JSON now, so..

--
Greg Donald

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Wells Oliver 2013-02-06 19:19:33 Re: Keeping historical record changes w/ triggers - best practices?
Previous Message Igor Neyman 2013-02-06 19:05:30 Re: configuring timezone