Elegant way to monitor for changes in a trigger and migrate record to history table

From: "David B" <postgresql(at)thegatelys(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Elegant way to monitor for changes in a trigger and migrate record to history table
Date: 2004-02-11 01:18:46
Message-ID: GOEGKICPFOPNLIEIHGFJMEHHCHAA.postgresql@thegatelys.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Folks,

Perhaps you can help....hell I'm sure you can!
I want to monitor for changes in a table and migrate the OLD. record to
audit table.

Is there an elegant or generic way to do this so I can use across multiple
tables with little change.

E.g.
IF TG_OP = 'UPDATE' THEN
INSERT INTO cust_hist ( col1, col2, col3, col4 ) VALUES ( OLD.col1,
OLD.col2, OLD.col4 ) ;
END IF ;

This hardly seems scalable for a table with 50-100 columns.
I'm thinking that perhaps there is a way to use RECORD or some such variable
to be able to do something like:

INSERT INTO cust_hist ( OLD.* ) ;

I guess something like this might also work:
INSERT INTO cust_hist SELECT * FROM cust WHERE cust_id = OLD.cust_id ;
But it strikes me as very very bad from a performance standpoint.

Would love to hear your thoughts.
-D

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com)
Version: 6.0.574 / Virus Database: 364 - Release Date: 1/29/2004

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Mark Gibson 2004-02-11 08:39:24 Re: Elegant way to monitor for changes in a trigger and migrate
Previous Message Ramiro Arenas R 2004-02-10 20:45:49 Unable to convert date to tm