Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nDw1P-0004wn-Py for pgsql-general@arkaria.postgresql.org; Sat, 29 Jan 2022 22:16:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nDw1O-0005b2-Cx for pgsql-general@arkaria.postgresql.org; Sat, 29 Jan 2022 22:16:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nDw1O-0005at-1W for pgsql-general@lists.postgresql.org; Sat, 29 Jan 2022 22:16:38 +0000 Received: from mail-qt1-x832.google.com ([2607:f8b0:4864:20::832]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nDw1L-0002r0-SC for pgsql-general@lists.postgresql.org; Sat, 29 Jan 2022 22:16:37 +0000 Received: by mail-qt1-x832.google.com with SMTP id h25so7368575qtm.1 for ; Sat, 29 Jan 2022 14:16:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to; bh=P9la1ZaeC7+PewdacJFvek7nxKwxlc+VvsCO1SU67FQ=; b=MUgSWN8ipcrt4WXEFBhr4pnsTg/qtd7Tr8cI2Xcq8vcrJX/1s8tPqZo6jStl6A1eDj XpkbYd3BGbPgYd6XdSp6/1SSZQzqU597ORarG9eNUwE/Y1FJn1vZFm5BZ8HUZQj6V1Fr IGrbNnnvj4kV79SXM6GwqNRtknC12VJ+VJHUIf7M5IMl0JlMzt0WbpDjsH97QM5Hm8Qf q4owvoXC60labEn79ydmwyXAcGYGYPhd/Wea7m88L2lSnmWcojBk3mYYi4qogheA05lO ZaUtDGbKlpjqcWLejAIlxNEJfLrS/cer8iv1KxNPq2tGvXVHXcRR+Ng9qEt8Ir11lzVr lZbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to; bh=P9la1ZaeC7+PewdacJFvek7nxKwxlc+VvsCO1SU67FQ=; b=NI3mjdtE73ig61qQccphhgVBEtcFpN+mUmrm+nHl3PZe8kF5ehioUU4K8rO6VCJUC6 5hl3erhaULZz7izoNnS4vR8chGV6YFUD1qzD36Y2kILCufYZfe+F0PCjOAyHZmigI/5q h6OVzaY+srUSwn0cFfxzfgGxFaD2HwwuaGOrMyssiQJLuiKjfN7CtsB0yfjyqXPvVo80 9QkeevYZvlgWkgVMseyteG1Za2gBUu+FAObizwm7+OVPAB/3hX91zQ98XV/hk4qLctBj CXepAfvlMnZMCR88CgdDs+9mdntGWXKi3n+oa9EG3Si+nbbpfeQeL3htSpBs42fa2KO1 jzDg== X-Gm-Message-State: AOAM530kCnGue0uq8UeeAEvFKMSds30QSwtpVVsyki0vT+CwPzZ8ODBN CapMorI66B0O0rOiIOYVEZf/qCmYPjz9xA== X-Google-Smtp-Source: ABdhPJxC7LVZsuSdo84KT9evfosUnkv2WM9spbjTk9RXhOTCezaNEhC29AM7H3wHUYniKHhvPzKOOg== X-Received: by 2002:a05:622a:1056:: with SMTP id f22mr10137375qte.99.1643494593833; Sat, 29 Jan 2022 14:16:33 -0800 (PST) Received: from [192.168.2.100] (pool-72-79-19-93.nwrknj.east.verizon.net. [72.79.19.93]) by smtp.gmail.com with ESMTPSA id u11sm2073560qkp.122.2022.01.29.14.16.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 29 Jan 2022 14:16:33 -0800 (PST) Content-Type: multipart/alternative; boundary="------------QidWP2jQaUx0CmjDcvbTzc3R" Message-ID: <07a78d2a-35ed-9ecc-4e4c-990b4d26ff50@gmail.com> Date: Sat, 29 Jan 2022 17:16:32 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: pg_try_advisory_lock is waiting? Content-Language: en-US To: Michael Lewis Cc: Tom Lane , pgsql-general@lists.postgresql.org References: <54e6a2bc-3546-88c7-0382-c74f23e4b668@gmail.com> <2858894.1643414894@sss.pgh.pa.us> <1b2e1d4e-298e-86ef-5875-0c89719d48f1@gmail.com> From: Mladen Gogala In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------QidWP2jQaUx0CmjDcvbTzc3R Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 1/28/22 20:54, Michael Lewis wrote: > On Fri, Jan 28, 2022 at 5:34 PM Mladen Gogala > 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 --------------QidWP2jQaUx0CmjDcvbTzc3R Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
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
--------------QidWP2jQaUx0CmjDcvbTzc3R--