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 1vqI8c-008BAu-33 for pgsql-general@arkaria.postgresql.org; Wed, 11 Feb 2026 21:52:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vqI7b-0069Ua-3A for pgsql-general@arkaria.postgresql.org; Wed, 11 Feb 2026 21:51:44 +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 1vqI7b-0069US-1v for pgsql-general@lists.postgresql.org; Wed, 11 Feb 2026 21:51:44 +0000 Received: from mout02.posteo.de ([185.67.36.66]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vqI7a-00000000DXT-0zjX for pgsql-general@postgresql.org; Wed, 11 Feb 2026 21:51:43 +0000 Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 9C01E240101 for ; Wed, 11 Feb 2026 22:51:40 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=posteo.de; s=2017; t=1770846700; bh=UEqD7jaXzwiUCnUpWNXrtghkK9VsBpLpp3V8lUjNHBI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:From:Content-Type: Content-Transfer-Encoding:From; b=c7R0Jq+dMQT6ka6bxUlEboe614PkTVBj6mNaBA1wKQdu2W+4bqjJnQrBaScIyG4bm wwjCfKCTDUb7H5nObav4Klhuetl/UngOtE7HI+xC+JNmamZGNVlBZ91JHZbKIgu3aF LkEAcS7/d+tVwUhiLLTX1wu9e1YhxqHjhFf8TcT6Z8Uiy7PGPpSlvMv03FlyieJygD HqCzkdLn5ta4ybpI9QbRfZ6lN25NgxHT0213aFWUWPuKHSSm5sbS1hL81f6H1xabGX CAFfLZuFkEujvOh7tYeVlqacHqWB0uB/KApjPq42e3hWHQCacey+Eh9iEYKMmQU1D7 EZyXsJX/Wz/Jg== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4fBBwD1Rfjz9rxB; Wed, 11 Feb 2026 22:51:39 +0100 (CET) Message-ID: Date: Wed, 11 Feb 2026 21:51:40 +0000 MIME-Version: 1.0 Subject: Re: Guarantee order of batched pg_advisory_xact_lock To: Tom Lane Cc: pgsql-general@postgresql.org References: <93fb937f-9f4d-4bd7-b7e6-51d5f98859d9@posteo.de> <14506.1770844674@sss.pgh.pa.us> Content-Language: en-US From: Nico Heller In-Reply-To: <14506.1770844674@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk That's an interesting idea and more likely, yes - I didn't think of that. So it would probably be better to ORDER BY the hashtextended result instead of :keysToLock, right? Hash collisions could therefore not create the [a,b,c] [b,a,c] locking pattern which obviously deadlocks. I will check for hash collisions tomorrow, I know all possible keys. On 2/11/26 22:17, Tom Lane wrote: > 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