Re: Own messages for constraints?

From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: hubert depesz lubaczewski <depesz(at)gmail(dot)com>
Cc: Kacper Chrapa <k(dot)chrapa(at)wp(dot)pl>, pgsql-general(at)postgresql(dot)org
Subject: Re: Own messages for constraints?
Date: 2007-03-19 18:31:00
Message-ID: 1174329060.23455.373.camel@dogma.v10.wvs
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 2007-03-19 at 19:08 +0100, hubert depesz lubaczewski wrote:
> On 3/19/07, Jeff Davis <pgsql(at)j-davis(dot)com> wrote:
> > You can use an AFTER trigger instead of a CHECK constraint (but that may
> > have a performance impact - test for your application).
>
> are you sure you meant AFTER? why? generally data-checks should be in
> before triggers. i guess.
>

If you do the check BEFORE, you have to make sure that no other BEFORE
triggers that execute afterward modify the data again.

Assuming your AFTER trigger is on INSERT and UPDATE, there is no way for
a subsequent AFTER trigger to modify the data to be invalid. So an AFTER
trigger is more of an assurance that your data is valid.

Note that AFTER triggers need to queue up, so if you do a huge update
and have an AFTER trigger, it might use a lot of memory. BEFORE triggers
don't have that problem. If you're very concerned about this you could
use a BEFORE trigger and just make sure that no other trigger will cause
a problem.

Regards,
Jeff Davis

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-03-19 18:31:03 Re: Possible planner bug?
Previous Message hubert depesz lubaczewski 2007-03-19 18:26:13 Re: Own messages for constraints?