Re: Trigger - Mailing list pgsql-admin

From Erik Wienhold
Subject Re: Trigger
Date
Msg-id ytjhuiiriv4f7j4jidsej2uuonfklccpozu2z4fzwcgsn2vniz@tgmttjb47bap
Whole thread Raw
In response to Re: Trigger  (Thiemo Kellner <thiemo@gelassene-pferde.biz>)
List pgsql-admin
On 2024-04-03 16:42 +0200, Thiemo Kellner wrote:
> Am 03.04.2024 um 12:30 schrieb Rajesh Kumar:
> > I have one table with lot of columns (all vehicle details) and also have
> > updated_at column. Everytime there is a change in data, I want to backup
> > the original data into a new table?
> > 
> > What's the best option? A function and trigger before update?
> 
> I would say so.

An AFTER trigger is better suited for that because it runs after
constraints are checked and the UPDATE has completed.  This avoids
unnecessary inserts into the audit table in case the UPDATE fails.

The docs have examples on table auditing[1].

[1] https://www.postgresql.org/docs/current/plpgsql-trigger.html#PLPGSQL-TRIGGER-AUDIT-EXAMPLE

-- 
Erik



pgsql-admin by date:

Previous
From: Ron Johnson
Date:
Subject: Re: About Autovacuum Query
Next
From: jaya kumar
Date:
Subject: Re: About Autovacuum Query