Re: Trigger

From: Erik Wienhold <ewie(at)ewie(dot)name>
To: Thiemo Kellner <thiemo(at)gelassene-pferde(dot)biz>
Cc: pgsql-admin(at)lists(dot)postgresql(dot)org
Subject: Re: Trigger
Date: 2024-04-03 16:05:46
Message-ID: ytjhuiiriv4f7j4jidsej2uuonfklccpozu2z4fzwcgsn2vniz@tgmttjb47bap
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

  • Re: Trigger at 2024-04-03 14:42:08 from Thiemo Kellner

Browse pgsql-admin by date

  From Date Subject
Next Message jaya kumar 2024-04-03 16:20:44 Re: About Autovacuum Query
Previous Message Ron Johnson 2024-04-03 15:04:02 Re: About Autovacuum Query