Re: Isolation / Visibility inside a trigger

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Jorge Godoy <jgodoy(at)gmail(dot)com>
Cc: PostgreSQL General ML <pgsql-general(at)postgresql(dot)org>
Subject: Re: Isolation / Visibility inside a trigger
Date: 2006-11-03 13:13:22
Message-ID: 20061103131322.GC27350@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Nov 03, 2006 at 09:49:17AM -0300, Jorge Godoy wrote:
> I'm trying to fix a bug (?) in my design but I'd like to understand my mistake
> first, so that I don't do that again.

<snip>

> But when I converted those to (before) triggers I started having a problem
> where it tries reading data from the soon-to-be-commited row but the functions
> called can't read it, even though the serial column has already been
> incremented and the insert command issued.

"Before" triggers can't see the data changes yet, they are, by
definition, before the commit.

From what you write it doesn't look like you really need to change the
row being written, so you could just as well use "after" trigger, which
don't have this problem...

> - shouldn't the data be available inside the transaction and visible for
> all operations called by the trigger?
>
> - shouldn't I use before triggers when manipulating data and changing
> values (since after triggers ignore results)?

Before trigger are only needed if you want to alter the row being
committed. Both before and after triggers can alter *other* data in the
database.

Maybe you need to split the triggers into tasks done before (updating
fields in NEW) and tasks after (updating other tables).

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jorge Godoy 2006-11-03 13:24:21 Re: Isolation / Visibility inside a trigger
Previous Message Merlin Moncure 2006-11-03 13:11:35 Re: Counting records in a PL/pgsql cursor