Thread: Deadlock on the same select for update

Deadlock on the same select for update

From
Roman
Date:
Hi,
I have problem with deadlocks and don't know why it happens. Below is
the log (postgres 9.0, debian):

[11882]DETAIL:  Process 11882 waits for ShareLock on transaction
44324308; blocked by process 11884.
        Process 11884 waits for ShareLock on transaction 44324307;
blocked by process 11882.

        Process 11882: SELECT * FROM teddy WHERE id IN

(112747007,112747008,112747011,112747013,112747015,112747016,112747020,112747021,112747022,112747024,112747025,112747028,112747030,112747032,112747034,112747035,112747038,112747043,112747044,112747045,112747050,112747052,112747053)
FOR UPDATE

        Process 11884: SELECT * FROM teddy WHERE id IN

(112747007,112747008,112747011,112747013,112747015,112747016,112747020,112747021,112747022,112747024,112747025,112747028,112747030,112747032,112747034,112747035,112747038,112747043,112747044,112747045,112747050,112747052,112747053)
FOR UPDATE


Regards,
Roman

Re: Deadlock on the same select for update

From
Bill Moran
Date:
In response to Roman <roman.janowczyk@gmail.com>:

> Hi,
> I have problem with deadlocks and don't know why it happens. Below is
> the log (postgres 9.0, debian):
>
> [11882]DETAIL:  Process 11882 waits for ShareLock on transaction
> 44324308; blocked by process 11884.
>         Process 11884 waits for ShareLock on transaction 44324307;
> blocked by process 11882.
>
>         Process 11882: SELECT * FROM teddy WHERE id IN
>
(112747007,112747008,112747011,112747013,112747015,112747016,112747020,112747021,112747022,112747024,112747025,112747028,112747030,112747032,112747034,112747035,112747038,112747043,112747044,112747045,112747050,112747052,112747053)
> FOR UPDATE
>
>         Process 11884: SELECT * FROM teddy WHERE id IN
>
(112747007,112747008,112747011,112747013,112747015,112747016,112747020,112747021,112747022,112747024,112747025,112747028,112747030,112747032,112747034,112747035,112747038,112747043,112747044,112747045,112747050,112747052,112747053)
> FOR UPDATE

My experience is that you have no guarantee what order SELECT FOR UPDATE
will lock those rows in, thus the chance for deadlock is there.

--
Bill Moran
http://www.potentialtech.com
http://people.collaborativefusion.com/~wmoran/

Re: Deadlock on the same select for update

From
Merlin Moncure
Date:
On Mon, Feb 21, 2011 at 7:16 AM, Roman <roman.janowczyk@gmail.com> wrote:
> Hi,
> I have problem with deadlocks and don't know why it happens. Below is
> the log (postgres 9.0, debian):
>
> [11882]DETAIL:  Process 11882 waits for ShareLock on transaction
> 44324308; blocked by process 11884.
>        Process 11884 waits for ShareLock on transaction 44324307;
> blocked by process 11882.
>
>        Process 11882: SELECT * FROM teddy WHERE id IN
>
(112747007,112747008,112747011,112747013,112747015,112747016,112747020,112747021,112747022,112747024,112747025,112747028,112747030,112747032,112747034,112747035,112747038,112747043,112747044,112747045,112747050,112747052,112747053)
> FOR UPDATE
>
>        Process 11884: SELECT * FROM teddy WHERE id IN
>
(112747007,112747008,112747011,112747013,112747015,112747016,112747020,112747021,112747022,112747024,112747025,112747028,112747030,112747032,112747034,112747035,112747038,112747043,112747044,112747045,112747050,112747052,112747053)

try this:
SELECT * FROM teddy WHERE id IN
(select
unnest(array[112747007,112747008,112747011,112747013,112747015,112747016,112747020,112747021,112747022,112747024,112747025,112747028,112747030,112747032,112747034,112747035,112747038,112747043,112747044,112747045,112747050,112747052,112747053]));

and you should force a nestloop (always?)
(tried this on a lark: curious to get other opinions on a good way to do it...)

merlin