pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Elegant way to monitor for changes in a trigger and migrate record to history table
2+ messages / 2 participants
[nested] [flat]

* Elegant way to monitor for changes in a trigger and migrate record to history table
@ 2004-02-11 01:18  David B <postgresql@thegatelys.com>
  0 siblings, 1 reply; 2+ messages in thread

From: David B @ 2004-02-11 01:18 UTC (permalink / raw)
  To: 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




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Elegant way to monitor for changes in a trigger and migrate
@ 2004-02-11 08:39  Mark Gibson <gibsonm@cromwell.co.uk>
  parent: David B <postgresql@thegatelys.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Mark Gibson @ 2004-02-11 08:39 UTC (permalink / raw)
  To: David B <postgresql@thegatelys.com>; +Cc: pgsql-sql

David B wrote:

>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.
>  
>

You can use a rule to do this:

CREATE RULE cust_audit AS ON UPDATE OR DELETE TO cust DO
  INSERT INTO cust_hist SELECT OLD.*;

cust_hist should be identical to cust without a primary key or any 
constraints/foreign keys etc.

I'm currently working on an auditing system at present, and will be 
releasing it
soon if anyone is interested. It needs some cleaning up first, when I 
have time.

-- 
Mark Gibson <gibsonm |AT| cromwell |DOT| co |DOT| uk>
Web Developer & Database Admin
Cromwell Tools Ltd.
Leicester, England.




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2004-02-11 08:39 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-02-11 01:18 Elegant way to monitor for changes in a trigger and migrate record to history table David B <postgresql@thegatelys.com>
2004-02-11 08:39 ` Re: Elegant way to monitor for changes in a trigger and migrate Mark Gibson <gibsonm@cromwell.co.uk>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox