pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mladen Gogala <gogala.mladen@gmail.com>
To: Michael Lewis <mlewis@entrata.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: pg_try_advisory_lock is waiting?
Date: Sat, 29 Jan 2022 17:16:32 -0500
Message-ID: <07a78d2a-35ed-9ecc-4e4c-990b4d26ff50@gmail.com> (raw)
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>

On 1/28/22 20:54, Michael Lewis wrote:
> 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?
>
> 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.

I managed to resolve the problem:

savepoint pt1;

select from table where <...> for update nowait;

If it fails, do "rollback to pt1" and continue. Transaction block will 
survive. OK, now the life is good again.

Regards

-- 
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com

view thread (7+ messages)  latest in thread

Message-ID: <07a78d2a-35ed-9ecc-4e4c-990b4d26ff50@gmail.com>
Permalink:  ../07a78d2a-35ed-9ecc-4e4c-990b4d26ff50@gmail.com/
Also on:    postgresql.org/message-id/07a78d2a-35ed-9ecc-4e4c-990b4d26ff50@gmail.com

 · 

reply

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: gogala.mladen@gmail.com, mlewis@entrata.com, tgl@sss.pgh.pa.us
  Subject: Re: pg_try_advisory_lock is waiting?
  In-Reply-To: <07a78d2a-35ed-9ecc-4e4c-990b4d26ff50@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> <07a78d2a-35ed-9ecc-4e4c-990b4d26ff50@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