Re: How can this INSERT fail?

From: "Greg Sabino Mullane" <greg(at)turnstep(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: How can this INSERT fail?
Date: 2012-04-25 17:26:17
Message-ID: dc95e526e636a3b5dd2777a9f98b978e@biglumber.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

> I can only assume it is possible for multiple transactions to overlap and
> one of them to miss the row so it doesn't appear in the EXCEPT SELECT but
> the row appears before the transaction commits and so an error occurs. My
> first thought was SELECT ... FOR UPDATE, but I can't do that within an
> EXCEPT.
>
> Can someone confirm whether I could avoid these errors by some form of SET
> TRANSACTION ISOLATION LEVEL, or let me know if I am just Doing It Wrong?

Yes, there is a race condition. You can solve it by setting the isolation
to repeatable read. As long as your application doesn't *need* the default
read committed level, it should be logically safe and not affect performance.
(But make sure you use repeatable read and not serializable, as they are
distinct in recent versions of Postgres and the latter does carry a
potential performance penalty).

- --
Greg Sabino Mullane greg(at)turnstep(dot)com
PGP Key: 0x14964AC8 201204251322
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
-----BEGIN PGP SIGNATURE-----

iEYEAREDAAYFAk+YM6IACgkQvJuQZxSWSshlSQCg8qVLbaYcEEJ9vOU43f2Irawv
2nwAnAxidDPeAohXOOTPa7mK0ORz2wc9
=xIfz
-----END PGP SIGNATURE-----

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Andreas Gaab 2012-04-26 09:22:44 Running mean filtering using Window Functions?
Previous Message Peter Harris 2012-04-25 13:01:54 Re: How can this INSERT fail?