Re: pg advisory locks

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Arun Gokule <arun(dot)gokule(at)gmail(dot)com>
Cc: pgsql-general General <pgsql-general(at)postgresql(dot)org>
Subject: Re: pg advisory locks
Date: 2014-10-13 08:05:40
Message-ID: 8A4E4371-7B0C-4627-8EBE-D71241915F79@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On 12 Oct 2014, at 18:41, Arun Gokule <arun(dot)gokule(at)gmail(dot)com> wrote:

> Hi,
>
> I am executing pg_advisory_locks using the following set of statements:
>
> SELECT pg_advisory_lock(317,2);
> UPDATE posts SET dislikers = array_remove(dislikers, 7) WHERE id = 317;
> update posts set num_dislikes = icount(dislikers), updated_at = now() where id = 317;
> WITH update_likers AS (SELECT pg_advisory_unlock(317,2)) select num_likes, num_dislikes, (7 IN (select(unnest(likers)))) as liked, (7 IN (select(unnest(dislikers)))) as disliked from posts where id = 317 LIMIT 1;
>
> These are issued from a multithreaded app. One in 1000 queries, I get a deadlock after the execution of the above set of statements. i.e. SELECT try_pg_advisory_lock(317,2) returns false. Is there something obvious that I am doing wrong?

Your last query doesn’t call the unlock statement, that WITH section is going to be ignored as the query isn’t using it.

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Vick Khera 2014-10-13 11:54:37 Re: 9.3 migration issue
Previous Message Stephen Davies 2014-10-13 04:11:34 9.3 migration issue