Re: pg_try_advisory_lock is waiting?

From: Michael Lewis <mlewis(at)entrata(dot)com>
To: Mladen Gogala <gogala(dot)mladen(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: pg_try_advisory_lock is waiting?
Date: 2022-01-29 01:54:29
Message-ID: CAHOFxGr_+oLPido92xEOnCSDj6jD2XBT+ZEXHBAmdEfDOC+oSg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Jan 28, 2022 at 5:34 PM Mladen Gogala <gogala(dot)mladen(at)gmail(dot)com>
wrote:

> pg_try_advisory_lock returned TRUE even without "FOR UPDATE" clause in the
> subquery. Shouldn't it return false because it can't lock the row until the
> uncommitted update finishes?
>
> <https://dbwhisperer.wordpress.com>
>
> The rows being updated or not is unrelated to advisory locks. Your example
shows you locking on key 0 and then on key 1. That should be allowed. If
you tried key 0 twice, then that is when you would get back "false" from
the function call.

You could establish a pattern of using the table OID as the first lock key
and the primary key value (if a single column) as the second advisory lock
key with the two parameter version of the function. But it is totally up to
your code to honor that advisory lock, or not.

Again, why use advisory locks and not select for update? Perhaps just
because you don't want to deal with the failed transaction? What should
happen when some other process cannot get a lock on that row? Do you want
to wait and retry? Have you looked into the "skip locked" option? If you
use "returning id" with that, you'd be able to check if you got the lock or
not.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sourav Prasad Das 2022-01-29 17:34:45 Regarding PGperffarm_server Setup
Previous Message Mladen Gogala 2022-01-29 00:33:55 Re: pg_try_advisory_lock is waiting?