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.96) (envelope-from ) id 1vqHbC-007z8R-37 for pgsql-general@arkaria.postgresql.org; Wed, 11 Feb 2026 21:18:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vqHbC-0061Nw-0N for pgsql-general@arkaria.postgresql.org; Wed, 11 Feb 2026 21:18:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vqHbB-0061No-2X for pgsql-general@lists.postgresql.org; Wed, 11 Feb 2026 21:18:14 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vqHbA-00000000DJV-2MBU for pgsql-general@postgresql.org; Wed, 11 Feb 2026 21:18:14 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 61BLHsAl014507; Wed, 11 Feb 2026 16:17:55 -0500 From: Tom Lane To: Nico Heller cc: pgsql-general@postgresql.org Subject: Re: Guarantee order of batched pg_advisory_xact_lock In-reply-to: <93fb937f-9f4d-4bd7-b7e6-51d5f98859d9@posteo.de> References: <93fb937f-9f4d-4bd7-b7e6-51d5f98859d9@posteo.de> Comments: In-reply-to Nico Heller message dated "Wed, 11 Feb 2026 18:44:07 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14505.1770844674.1@sss.pgh.pa.us> Date: Wed, 11 Feb 2026 16:17:54 -0500 Message-ID: <14506.1770844674@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Nico Heller writes: > We use the following bulk query as we sometimes need acquire multiple > locks at the same time and want to avoid round-trips to the database: > |WITH keys(key) AS (SELECT unnest(:keysToLock)) SELECT > pg_advisory_xact_lock(hashtextextended(key, 0)) FROM keys| > :keysToLock is a text[] parameter which is pre-sorted in our > application. This pre-sorting is done to prevent dead locks when two > concurrent transactions try acquire the same advisory locks (e.g. > [a,b,c] [b,a,c] can easily deadlock). > We thought this would be enough, but we occasionally still run into > deadlocks. Have you eliminated the possibility that you're getting hash collisions? With or without that CTE, I can't see a reason for PG to change the order in which the unnest() results are processed, so I think you are barking up the wrong tree about where the problem is. regards, tom lane