Re: Trigger for Audit Table - Mailing list pgsql-general

From ksherlock@gmail.com
Subject Re: Trigger for Audit Table
Date
Msg-id 1173475671.310159.287660@q40g2000cwq.googlegroups.com
Whole thread Raw
In response to Trigger for Audit Table  (Bill Moseley <moseley@hank.org>)
Responses Re: Trigger for Audit Table  (Bill Moseley <moseley@hank.org>)
List pgsql-general
You can/should create it as an AFTER UPDATE trigger.  The OLD row will
contain the previous values.

eg:
  INSERT INTO template_history
  ( template_id, path, content, last_updated_time, person )
  values
  (OLD.id, OLD.path, OLD.content, OLD.last_updated_time, OLD.person);

On Mar 9, 2:45 pm, mose...@hank.org (Bill Moseley) wrote:

> My trigger is very simple:
>
>     CREATE OR REPLACE FUNCTION audit_template() RETURNS TRIGGER AS '
>         BEGIN
>             INSERT INTO template_history
>                         ( template_id, path, content, last_updated_time, person )
>                         select
>                             id, path, content, last_updated_time, person
>                         from
>                             template where id = 1;
>
>             RETURN NEW;
>         END'
>     language 'plpgsql';
>
>     CREATE TRIGGER template_history_add BEFORE UPDATE ON template
>         for each row execute procedure audit_template();
>
> I realize this is a *BEFORE* UPDATE trigger, but I have this vague
> memory of seeing a post stating that you can't be sure the existing
> row has not been updated yet. Perhaps that was just a concern if
> another trigger was to modify the row.  But, I can't seem to find that
> post now which is why I'm asking for the sanity check.
>
> Are there potential problems with this setup?
>
> --
> Bill Moseley
> mose...@hank.org
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings



pgsql-general by date:

Previous
From: D Unit
Date:
Subject: Re: Solaris and Ident
Next
From: Stefan Berglund
Date:
Subject: Is This A Set Based Solution?