From: Michael Lewis <mlewis@entrata.com>
To: Mladen Gogala <gogala.mladen@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: pg_try_advisory_lock is waiting?
Date: Fri, 28 Jan 2022 18:54:29 -0700
Message-ID: <CAHOFxGr_+oLPido92xEOnCSDj6jD2XBT+ZEXHBAmdEfDOC+oSg@mail.gmail.com> (raw)
In-Reply-To: <1b2e1d4e-298e-86ef-5875-0c89719d48f1@gmail.com>
References: <54e6a2bc-3546-88c7-0382-c74f23e4b668@gmail.com>
<2858894.1643414894@sss.pgh.pa.us>
<1b2e1d4e-298e-86ef-5875-0c89719d48f1@gmail.com>
On Fri, Jan 28, 2022 at 5:34 PM Mladen Gogala <gogala.mladen@gmail.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.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: mlewis@entrata.com, gogala.mladen@gmail.com, tgl@sss.pgh.pa.us
Subject: Re: pg_try_advisory_lock is waiting?
In-Reply-To: <CAHOFxGr_+oLPido92xEOnCSDj6jD2XBT+ZEXHBAmdEfDOC+oSg@mail.gmail.com>
References: <54e6a2bc-3546-88c7-0382-c74f23e4b668@gmail.com> <2858894.1643414894@sss.pgh.pa.us> <1b2e1d4e-298e-86ef-5875-0c89719d48f1@gmail.com> <CAHOFxGr_+oLPido92xEOnCSDj6jD2XBT+ZEXHBAmdEfDOC+oSg@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox