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 1nDaJi-0008Mb-3R for pgsql-general@arkaria.postgresql.org; Fri, 28 Jan 2022 23:06:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nDaJ9-00013a-So for pgsql-general@arkaria.postgresql.org; Fri, 28 Jan 2022 23:05:31 +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 1nDaJ9-00013Q-Fu for pgsql-general@lists.postgresql.org; Fri, 28 Jan 2022 23:05:31 +0000 Received: from mail-qk1-x72d.google.com ([2607:f8b0:4864:20::72d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nDaJ1-0007yB-PI for pgsql-general@lists.postgresql.org; Fri, 28 Jan 2022 23:05:30 +0000 Received: by mail-qk1-x72d.google.com with SMTP id j24so4676985qkk.10 for ; Fri, 28 Jan 2022 15:05:23 -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:content-language:to:from :subject; bh=GOiMAjORyWdW+rxyx7XhxXvjP622vLAcBTBexvpzqBw=; b=QSyvA+3xyn5wSXFTOgTnFxq5qWeh9GQvaXYKaChKgLhv7BsUqQa/nfNtL+y4lo50yv mbe6F/0KvgHG7759E3r2newfCWmeFzzvmdMD42+JForJmomXsyvZBA7Pa98TZvR/68PO V4ft2+E6L+UVRunE1xMtiBgsUfOScuezmxHbZmYAejEYAcCPvYg8Me0t2Ig3DoCxYHt0 zAUnBClArpP4ETvYxtfTFMzeFJki9AChrB99FArkeTnT5CVR6rsMyN4sgvveyD4DNYMZ IiWKmeQ1HqatlSE5Mj/lwle0sSEclRZmGUF6HeZSLRstA338fVGxBI22uiOAcYeqVuYy 2e3g== 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 :content-language:to:from:subject; bh=GOiMAjORyWdW+rxyx7XhxXvjP622vLAcBTBexvpzqBw=; b=eE+FyhXkEW6lrDnVHRdibI1AZ/Mxng/AsaK97sj5zBdbqIJsaH9Xopy2dymU7khJFA yKC8ZW5TyoAzINxVVYcRQF+VPTr/pfEgRYM9qVz1LDyYqEyF3AaH+3D08Q4zmA6KFOp8 FCvxzM3nW6ZRXWRSHZCd374sxpjWRc/dbzIriPsSLXFC/pe1OJpTvyehNDLVoRMq3lp5 kEhqQPY6TUf758UmJFTTlQIFkZqQszQytIPRwThLQrB3wYrsOycRgA+hv7I071AuXAR+ EbggXX/klDpUhQhyzwl1gHxm1FPCXdkoLYieNsPVDP/7LdULuywTxpnPRkxatXIko2l9 I6SQ== X-Gm-Message-State: AOAM532Iq87Zpw23XtiYB/qJ2ElGUhKj8EkMOzVV6I7xmuhDW7QfbQmQ Ba4jYIvsorrnmMn+/ZdgKnlwqbjIybt0LQ== X-Google-Smtp-Source: ABdhPJx9AksvVaoheXwogzuVNATRq+uv1MWkJUZH1fMRkCKiEUwn32IdGC+V/ZLXee7ruH4ZGXLUaA== X-Received: by 2002:a05:620a:4720:: with SMTP id bs32mr2140652qkb.486.1643411119997; Fri, 28 Jan 2022 15:05:19 -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 g20sm4008834qko.27.2022.01.28.15.05.18 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Jan 2022 15:05:19 -0800 (PST) Content-Type: multipart/alternative; boundary="------------FKnESySqnejRQr0hexsAZNW5" Message-ID: <54e6a2bc-3546-88c7-0382-c74f23e4b668@gmail.com> Date: Fri, 28 Jan 2022 18:05:18 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Content-Language: en-US To: pgsql-general@lists.postgresql.org From: Mladen Gogala Subject: pg_try_advisory_lock is waiting? 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. --------------FKnESySqnejRQr0hexsAZNW5 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit I am trying to replace SELECT FROM WHERE FOR UPDATE with pg_try_advisory_lock. The documentation says the following: https://www.postgresql.org/docs/13/functions-admin.html |pg_try_advisory_lock| ( /|key|/ |bigint| ) → |boolean| |pg_try_advisory_lock| ( /|key1|/ |integer|, /|key2|/ |integer| ) → |boolean| Obtains an exclusive session-level advisory lock if available. This will either obtain the lock immediately and return |true|, or return |false|_*without waiting*_ if the lock cannot be acquired immediately. I tried the following: _*1st Session:*_ mgogala=# begin transaction; BEGIN mgogala=*# update emp set sal=sal*1 where empno=7934; UPDATE 1 mgogala=*# _*2nd Session:*_ mgogala=# begin transaction; BEGIN mgogala=*# select pg_try_advisory_lock(0) from (select ename from emp where empno=7934 for update) as tbl; To my infinite surprise, "pg_advisory_lock" is waiting. I am aware of SELECT FOR UPDATE NOWAIT, but that produces an error and kills the transaction block. I would like to use something that would not kill the transaction block. I am obviously doing something wrong because the select in parenthesis will not return, so the query cannot be executed.  On the other hand, without the "FOR UPDATE" clause, I am getting TRUE, which is wrong: mgogala=# begin transaction; BEGIN mgogala=*# select pg_try_advisory_xact_lock(0) from (select ename from emp where empno=7934) as tbl;  pg_try_advisory_xact_lock ---------------------------  t (1 row) mgogala=*# rollback; ROLLBACK mgogala=# select pg_try_advisory_xact_lock(1) from (select ename from emp where empno=7934) as tbl;  pg_try_advisory_xact_lock ---------------------------  t (1 row) The row is still locked by the UPDATE statement, so the try_advisory_lock should return "f", not "t". The database is 13.5 on  Oracle Linux 8, x86_64. Transactions are written in Java so an exception will terminate the transaction block. SQL statements are generated by the home grown ORM. The application is ported from Oracle which will not hang the transaction block on the 1st error. Is there a way to get PostgreSQL to use something like NOWAIT without aborting the transaction block? -- Mladen Gogala Database Consultant Tel: (347) 321-1217 https://dbwhisperer.wordpress.com --------------FKnESySqnejRQr0hexsAZNW5 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

I am trying to replace SELECT <colum list> FROM <table> WHERE <condition> FOR UPDATE with pg_try_advisory_lock. The documentation says the following:

https://www.postgresql.org/docs/13/functions-admin.html

pg_try_advisory_lock ( key bigint ) → boolean

pg_try_advisory_lock ( key1 integer, key2 integer ) → boolean

Obtains an exclusive session-level advisory lock if available. This will either obtain the lock immediately and return true, or return false without waiting if the lock cannot be acquired immediately.

I tried the following:

1st Session:

mgogala=# begin transaction;
BEGIN
mgogala=*# update emp set sal=sal*1 where empno=7934;
UPDATE 1
mgogala=*#

2nd Session:

mgogala=# begin transaction;
BEGIN
mgogala=*# select pg_try_advisory_lock(0) from (select ename from emp where empno=7934 for update) as tbl;


To my infinite surprise, "pg_advisory_lock" is waiting. I am aware of SELECT FOR UPDATE NOWAIT, but that produces an error and kills the transaction block. I would like to use something that would not kill the transaction block. I am obviously doing something wrong because the select in parenthesis will not return, so the query cannot be executed.  On the other hand, without the "FOR UPDATE" clause, I am getting TRUE, which is wrong:

mgogala=# begin transaction;
BEGIN
mgogala=*# select pg_try_advisory_xact_lock(0) from (select ename from emp where empno=7934) as tbl;
 pg_try_advisory_xact_lock
---------------------------
 t
(1 row)

mgogala=*# rollback;
ROLLBACK
mgogala=# select pg_try_advisory_xact_lock(1) from (select ename from emp where empno=7934) as tbl;
 pg_try_advisory_xact_lock
---------------------------
 t
(1 row)

The row is still locked by the UPDATE statement, so the try_advisory_lock should return "f", not "t". The database is 13.5 on  Oracle Linux 8, x86_64. Transactions are written in Java so an exception will terminate the transaction block. SQL statements are generated by the home grown ORM. The application is ported from Oracle which will not hang the transaction block on the 1st error. Is there a way to get PostgreSQL to use something like NOWAIT without aborting the transaction block?

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