Thread: could not serialize access due to concurrent update

could not serialize access due to concurrent update

From
Brian Hurt
Date:
Actually, I have something of a weird problem.  I'm trying to test some
code of mine to handle cases where a retry is needed when working with
serializable transactions.  My problem is that I can *not* figure out
how to trigger this error:


> UPDATE, DELETE, SELECT FOR UPDATE, and SELECT FOR SHARE commands
> behave the same as SELECT in terms of searching for target rows: they
> will only find target rows that were committed as of the transaction
> start time. However, such a target row may have already been updated
> (or deleted or locked) by another concurrent transaction by the time
> it is found. In this case, the serializable transaction will wait for
> the first updating transaction to commit or roll back (if it is still
> in progress). If the first updater rolls back, then its effects are
> negated and the serializable transaction can proceed with updating the
> originally found row. But if the first updater commits (and actually
> updated or deleted the row, not just locked it) then the serializable
> transaction will be rolled back with the message
>
>ERROR:  could not serialize access due to concurrent update
>
> because a serializable transaction cannot modify or lock rows changed
> by other transactions after the serializable transaction began.
>

from:
http://www.postgresql.org/docs/8.1/interactive/transaction-iso.html#XACT-SERIALIZABLE

I know I need at least two different transactions on the same database
stepping on each other somehow, but the worst case I can come up with is
a deadlock case (which I want to retry, but that I know works).  But I
want to trigger the above error to make sure it works.  Most of the
examples I've come up with don't even generate an error, one transaction
simply blocks until the other completes.

Could somebody please send me a simple example of how I can trigger this
error?  I'm on postgres 8.1.4.

For the record, Postgresql is amazingly, sometimes even annoyingly, robust.

Brian


Re: could not serialize access due to concurrent update

From
Oliver Elphick
Date:
On Fri, 2007-08-10 at 16:23 -0400, Brian Hurt wrote:
> Actually, I have something of a weird problem.  I'm trying to test some
> code of mine to handle cases where a retry is needed when working with
> serializable transactions.  My problem is that I can *not* figure out
> how to trigger this error:

...
> >ERROR:  could not serialize access due to concurrent update
> >

Session1: BEGIN;
Session2: BEGIN;
Session1: SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
Session2: SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
Session1: SELECT * FROM tunnels WHERE un = 1 FOR UPDATE;
 un | password
----+----------
  1 | bugsy
(1 row)

Session1: UPDATE tunnels SET password = 'malone' WHERE un = 1;
Session2: SELECT * FROM tunnels WHERE un = 1 FOR UPDATE;
...waits on Session1's lock...
Session1: COMMIT;
Session2: ERROR:  could not serialise access due to concurrent update


--
Oliver Elphick                                          olly@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
GPG: 1024D/A54310EA  92C8 39E7 280E 3631 3F0E  1EC0 5664 7A2F A543 10EA
                 ========================================
   Do you want to know God?   http://www.lfix.co.uk/knowing_god.html


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.