Re: Trigger question - Mailing list pgsql-general

From Tom Lane
Subject Re: Trigger question
Date
Msg-id 11294.975345620@sss.pgh.pa.us
Whole thread Raw
In response to Re: Trigger question  (Nelio Alves Pereira Filho <nelio@ifx.com.br>)
List pgsql-general
Nelio Alves Pereira Filho <nelio@ifx.com.br> writes:
> To accomplish all that, first I need
> to unset some values, so I decided to use the BEFORE trigger to unset
> these values, and the AFTER trigger to do this calculation.

Why don't you just do all the work in the BEFORE trigger?

> And what's the real purpose of the AFTER trigger?

The sequence of events is

    compute contents of tuple to insert/update (per the query)

    apply BEFORE triggers

    check constraints

    insert tuple

    apply AFTER triggers

You *must* use a BEFORE trigger if you want to alter the tuple
that was computed by the query.  The point of an AFTER trigger
is that it won't be run until constraints have been passed,
so you can avoid doing potentially expensive work until you know
the tuple is good.  You might use an AFTER trigger to propagate
changes into other tables, for example.  But you can't change
the tuple.

            regards, tom lane

pgsql-general by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: Bug? 'psql -l' in pg_ctl?
Next
From: "Nick Fankhauser"
Date:
Subject: How to keep Postgres from thrashing for a long time using "LIMIT" (or is there a better way?)!