Re: concurrent updates problem - Mailing list pgsql-general

From Tom Lane
Subject Re: concurrent updates problem
Date
Msg-id 19668.985018321@sss.pgh.pa.us
Whole thread Raw
In response to Re: concurrent updates problem  ("Richard Huxton" <dev@archonet.com>)
Responses RE: concurrent updates problem
List pgsql-general
>> I have on a web application
>> update threads set views = views + 1 where forum_id = 1 and thread_id = 1

It should work to do

begin;
select * from threads where forum_id = 1 and thread_id = 1 FOR UPDATE;
update threads set views = views + 1 where forum_id = 1 and thread_id = 1;
end;

Note the FOR UPDATE to lock the row and the transaction wrapping to
define the scope of the lock.  Without this I'd expect you to lose
some counter increments as a result of two processes doing the UPDATE
at about the same time (both will read the old value of "views" and
increment it by one).

            regards, tom lane

pgsql-general by date:

Previous
From: Richard Huxton
Date:
Subject: Re: concurrent updates problem
Next
From: Tom Lane
Date:
Subject: Re: concurrent updates problem