Re: fail-safe sql update triggers - Mailing list pgsql-admin

From Igor Neyman
Subject Re: fail-safe sql update triggers
Date
Msg-id F4C27E77F7A33E4CA98C19A9DC6722A20671BA9C@EXCHANGE.corp.perceptron.com
Whole thread Raw
In response to fail-safe sql update triggers  (Michael Monnerie<michael.monnerie@is.it-management.at>)
List pgsql-admin
> -----Original Message-----
> From: Michael Monnerie [mailto:michael.monnerie@is.it-management.at]
> Sent: Friday, September 03, 2010 9:03 AM
> To: pgsql-admin@postgresql.org
> Subject: fail-safe sql update triggers
>
> I want to log all activity from a table to a old_table.
> Creating an ON INSERT trigger is simple, it just needs to
>
> INSERT INTO old_filter SELECT NEW.*;
>
> in a procedure that is called via the trigger. But what about
> updates? There is no simple
>
> UPDATE old_filter SET NEW.* WHERE id=NEW.id;
>
> so I would need to declare each column like
>
> UPDATE old_filter SET field1=NEW.field1,f2=NEW.f2,.... where
> id=NEW.id;
>
> But that is error prone, because when the "filter" table is
> changed to have a new column, the UPDATE statement would not
> contain it. Is there a fail-proof shortcut?
>
>
> mit freundlichen Grüßen
> Michael Monnerie, Ing. BSc
>

Michael,

You are seeing only half of the problem.
When you modify your "filter" table (i.e. add a column), not only UPDATE statement in your trigger function should be
modifiedto reflect the change in the original "filter" table, but also your history table "old_filter" should be
modifiedas well: new column should be added. 

So, in short there is no "easy" way around.
When "source" table is modified, "destination" table and trigger function should be also modified.

Regards,
Igor Neyman

pgsql-admin by date:

Previous
From: Lew
Date:
Subject: Re: problem with foreign key
Next
From: fel
Date:
Subject: [NOVICE] - SAN\NAS\DAS - Need help