Обсуждение: [MASSMAIL]Trigger

Поиск
Список
Период
Сортировка

[MASSMAIL]Trigger

От
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?

Re: Trigger

От
Thiemo Kellner
Дата:

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.



Re: Trigger

От
Erik Wienhold
Дата:
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