Re: [GENERAL] trigger Before or After

From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "avpro avpro *EXTERN*" <avprowebeden(at)gmail(dot)com>, "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: [GENERAL] trigger Before or After
Date: 2014-11-11 08:19:43
Message-ID: A737B7A37273E048B164557ADEF4A58B17D92E58@ntex2010i.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-performance

avpro avpro wrote:
> in the pgsql documentation
> (http://www.postgresql.org/docs/9.1/static/sql-createtrigger.html)
>
>
> i haven't seen anything referring to: how is affected the data inserted in the new table by a trigger
> Before Insert compared with a trigger After Insert? and anything related to performance

In your example (the trigger updates a second table) it should make
no difference if the trigger is BEFORE or AFTER INSERT.

The difference is that in a BEFORE trigger you can modify the values that
will be inserted before the INSERT actually happens.

> I read somewhere (I don't find the link anymore) that if the trigger is After Insert, the data
> available in the table LOG might not be available anymore to run the trigger. is that correct? or I
> might understood wrong?

I don't quite understand.
You will have access to the OLD and NEW values in both BEFORE and AFTER triggers.
In an AFTER trigger, the table row has already been modified.

> what's the difference related to performance concerning a trigger Before Insert compared with a
> trigger After Insert?

I don't think that there is a big difference, but you can easily test it:
Insert 100000 rows with a BEFORE trigger on the table and compare the
time it takes to inserting 100000 rows with an AFTER trigger.

Yours,
Laurenz Albe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message jaime soler 2014-11-11 10:11:09 Re: reindex table deadlock
Previous Message avpro avpro 2014-11-11 06:38:11 trigger Before or After

Browse pgsql-performance by date

  From Date Subject
Next Message Adrian Klaver 2014-11-11 13:58:49 Re: trigger Before or After
Previous Message avpro avpro 2014-11-11 06:38:11 trigger Before or After