Re: Do I need serializable for this query?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-general(at)postgresql(dot)org, William Garrison <postgres(at)mobydisk(dot)com>
Subject: Re: Do I need serializable for this query?
Date: 2007-04-11 08:49:50
Message-ID: 20070411084950.GA14213@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Apr 10, 2007 at 10:52:11PM +0200, Peter Eisentraut wrote:
> William Garrison wrote:
> > I have a table that keeps running totals. It is possible that this
> > would get called twice simultaneously for the same UserID. Do I need
> > to put this in a serializable transaction?
>
> Transaction isolation is only a question of interest if you have more
> than one statement in a transaction.

Not true; the isolation level is also relevant if you're doing
concurrent updates or deletes (although, as the documentation
mentions, PostgreSQL's serializable isolation level doesn't guarantee
true serializability).

create table test (id integer primary key, t text);
insert into test (id, t) values (1, 'a');

T1: begin isolation level serializable;
T2: begin isolation level serializable;
T1: update test set t = 'b' where id = 1;
T2: update test set t = 'c' where id = 1; -- blocks
T1: commit;
T2: ERROR: could not serialize access due to concurrent update

--
Michael Fuhr

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Listmail 2007-04-11 11:22:18 Re: The rule question before, request official documentation on the problem
Previous Message eugene.mindrov 2007-04-11 07:25:50 COPY FROM file with zero-delimited fields