Re: [PERFORM] Incr/Decr Integer

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-general(at)postgresql(dot)org
Cc: William Scott Jordan <wsjordan(at)brownpapertickets(dot)com>
Subject: Re: [PERFORM] Incr/Decr Integer
Date: 2009-07-16 18:30:56
Message-ID: 200907162030.56434.andres@anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-performance

On Thursday 16 July 2009 19:56:47 William Scott Jordan wrote:
> Hey all!
>
> Is there a better way to increase or decrease the value of an integer
> than doing something like:
>
> ---
> UPDATE the_table SET the_int = the_int + 1 WHERE the_id = 123 ;
> ---
>
> We seem to be getting a lot of deadlocks using this method under heavy
> load. Just wondering if we should be doing something different.
Is this the only statement in your transaction? Or are you issuing multiple
such update statements in one transactions?
I am quite sure its not the increment of that value causing the problem.

If you issue multiple such statements you have to be carefull. Example:

Session 1:
BEGIN;
UPDATE the_table SET the_int = the_int + 1 WHERE the_id = 1;

Session 2:
BEGIN
UPDATE the_table SET the_int = the_int + 1 WHERE the_id = 2;

Fine so far.

Session 1:
UPDATE the_table SET the_int = the_int + 1 WHERE the_id = 2 ;
Waits for lock.

Session 2:
UPDATE the_table SET the_int = the_int + 1 WHERE the_id = 1;
Deadlock.

Andres

PS: Moved to pgsql-general, seems more appropriate

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2009-07-16 18:46:15 Re: Incr/Decr Integer
Previous Message William Scott Jordan 2009-07-16 17:56:47 Incr/Decr Integer

Browse pgsql-performance by date

  From Date Subject
Next Message Justin Pitts 2009-07-16 18:35:50 Re: cluster index on a table
Previous Message Scott Carey 2009-07-16 18:21:46 Re: cluster index on a table