Re: Working around spurious unique constraint errors due to SERIALIZABLE bug

From: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Craig Ringer *EXTERN*" <craig(at)postnewspapers(dot)com(dot)au>, "Florian Weimer" <fweimer(at)bfk(dot)de>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Working around spurious unique constraint errors due to SERIALIZABLE bug
Date: 2009-07-20 08:15:55
Message-ID: D960CB61B694CF459DCFB4B0128514C203937E49@exadv11.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Craig Ringer wrote:
> > The drawback is that some of the side effects of the INSERT occur
> > before the constraint check fails, so it seems to me that I still need
> > to perform the select.
>
> If you really can't afford the INSERT side effects and can't redesign
> your code to be tolerant of them, you can always lock the table before
> an INSERT.

I wonder what could be the side effects of an INSERT that causes an error
that is caught in a plpgsql exception block.

What happens behind the scenes when an exception is caught is that the
transaction is rolled back to a savepoint that was set at the beginning
of the block.

So all changes to the database that were caused as side effects of the INSERT,
for example triggers, will be rolled back as well.

The only side effects that would remain could be caused by things that
go outside the limits of the database, e.g. modify files on the database
server or perform trigger based replication to another database.
Everything that is not under MVCC control, for short.

Is that the problem here?

On another line:
The original question asked was "how can I tell an error that is caused
by incomplete isolation from another error?"

If you have a code segment like
SELECT COUNT(id) INTO i2 FROM a WHERE id = i;
IF i2 = 0 THEN
INSERT INTO a (id) VALUES (i);
END IF;

Then you can be certain that any "unique_violation" thrown here must
be a serialization problem (if the only unique contraint is on "id").

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Florian Weimer 2009-07-20 08:59:49 Re: Working around spurious unique constraint errors due to SERIALIZABLE bug
Previous Message Albe Laurenz 2009-07-20 07:34:02 Re: Best practices for moving UTF8 databases