Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v4cRn-002fO7-DA for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Oct 2025 09:51:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1v4cRk-00COr2-SR for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Oct 2025 09:51:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v4cBb-00CKN9-Gw for pgsql-bugs@lists.postgresql.org; Fri, 03 Oct 2025 09:34:48 +0000 Received: from mx3.htx.pl ([213.218.125.143]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1v4cBX-001BUL-1C for pgsql-bugs@lists.postgresql.org; Fri, 03 Oct 2025 09:34:46 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mx3.htx.pl (Postfix) with ESMTP id C3BC74804B4; Fri, 3 Oct 2025 11:34:39 +0200 (CEST) Received: from htxmx23.htx-net.pl ([127.0.0.1]) by localhost (htxmx23.htx-net.pl [127.0.0.1]) (amavis, port 10024) with ESMTP id ocw2xFfsBTi6; Fri, 3 Oct 2025 11:34:39 +0200 (CEST) Received: from HTXEX01.htx-net.pl (HTXEX01.htx-net.pl [192.168.19.71]) by htxmx23.htx-net.pl (Postfix) with ESMTP id A7C854804A6; Fri, 3 Oct 2025 11:34:39 +0200 (CEST) Received: from HTXEX01.htx-net.pl (192.168.19.80) by HTXEX01.htx-net.pl (192.168.19.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.58; Fri, 3 Oct 2025 11:34:39 +0200 Received: from HTXEX01.htx-net.pl ([fe80::f121:288a:84ea:709d]) by HTXEX01.htx-net.pl ([fe80::f121:288a:84ea:709d%4]) with mapi id 15.01.2507.058; Fri, 3 Oct 2025 11:34:39 +0200 From: Maciej Walczak To: Tom Lane CC: "pgsql-bugs@lists.postgresql.org" Subject: RE: BUG #19069: pg_advisory_xact_lock() in a WITH query doesn't work Thread-Topic: BUG #19069: pg_advisory_xact_lock() in a WITH query doesn't work Thread-Index: AQHcM9kTDIsEO72xzUudoFWSfWL3I7SwKhzA Date: Fri, 3 Oct 2025 09:34:39 +0000 Message-ID: References: <19069-f5c72e4e4f921a7c@postgresql.org> <8a06d047-45c8-4be5-9d39-f176bb8b62bc@HTXEX03.htx-net.pl> In-Reply-To: <8a06d047-45c8-4be5-9d39-f176bb8b62bc@HTXEX03.htx-net.pl> Accept-Language: pl-PL, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [192.168.19.92] x-endpointsecurity-0xde81-ev: v:7.9.27.572, d:out, a:y, w:t, t:8, sv:1759459765, ts:1759484079 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thank you. I missed those parts of the documentation. I should have studied it more ca= refully. The reason I tried to use WITH is that a separate SELECT pg_advisory_xact_lock(1); statement confuses my object mapper (Dapper) when I want to use a RETURNING= clause in the INSERT. I found a workaround: DO $$ BEGIN PERFORM pg_advisory_xact_lock(1); END $$; Regards Maciej Walczak -----Original Message----- From: Tom Lane =20 Sent: 02 October 2025 22:14 To: Maciej Walczak Cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19069: pg_advisory_xact_lock() in a WITH query doesn't wo= rk PG Bug reporting form writes: > This doesn't actually do any locking: > BEGIN; > WITH my_lock AS ( > SELECT pg_advisory_xact_lock(1) > ) > INSERT INTO my_table (...) VALUES (...); COMMIT; That looks as-expected to me. The docs say [1] [ This works because ] PostgreSQL's implementation evaluates only as many rows of a WITH query as are actually fetched by the parent query. which is to say, none at all in this case. There's also this in [2]: Data-modifying statements in WITH are executed exactly once, and always to completion, independently of whether the primary query reads all (or indeed any) of their output. Notice that this is different from the rule for SELECT in WITH: as stated in the previous section, execution of a SELECT is carried only as far as the primary query demands its output. You're apparently expecting the presence of a volatile function to cause th= e SELECT to be reclassified as data-modifying, but we don't do it that way. I'd recommend being less cute and just writing BEGIN; SELECT pg_advisory_xact_lock(1); INSERT INTO my_table (...) VALUES (...); COMMIT; regards, tom lane [1] https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-= CYCLE [2] https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-= MODIFYING