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 1vlCbb-008oZf-2e for pgsql-admin@arkaria.postgresql.org; Wed, 28 Jan 2026 20:57:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vlCba-003ocu-2Q for pgsql-admin@arkaria.postgresql.org; Wed, 28 Jan 2026 20:57:39 +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.96) (envelope-from ) id 1vlCba-003ocm-1K for pgsql-admin@lists.postgresql.org; Wed, 28 Jan 2026 20:57:38 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vlCbX-002nu8-38 for pgsql-admin@lists.postgresql.org; Wed, 28 Jan 2026 20:57:37 +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 60SKvYcq854469; Wed, 28 Jan 2026 15:57:34 -0500 From: Tom Lane To: Tim Herren cc: "pgsql-admin@lists.postgresql.org" Subject: Re: Maximum amount of pg_locks In-reply-to: References: Comments: In-reply-to Tim Herren message dated "Wed, 28 Jan 2026 20:12:01 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <854467.1769633854.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 28 Jan 2026 15:57:34 -0500 Message-ID: <854468.1769633854@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Tim Herren writes: > I'm trying to wrap my head around the way the calculation for the maximu= m amount of locks works in postgres 16.11 > I already came to the understanding that the maximum amount of locks are= not on a transaction basis, but rather influenced by the setting "max_loc= ks_per_transaction" and the "max_connections". > I'm saying influenced rather than calculated because on my server a simp= le multiplication of those two values, set at 512 and 180 respectively giv= es 92160. > Yet I regularly observe around 119k locks during my backup using "pg_dum= p -Fc". Well, the hash table size is indeed set by a calculation of that form, but it's max_locks_per_transaction times the number of potential locker processes --- not only regular sessions (max_connections), but also autovacuum processes and other background workers. And prepared transactions, too. lock.c has #define NLOCKENTS() \ mul_size(max_locks_per_xact, add_size(MaxBackends, max_prepared_xacts)) where postinit.c calculates MaxBackends as: /* Note that this does not include "auxiliary" processes */ MaxBackends =3D MaxConnections + autovacuum_worker_slots + max_worker_processes + max_wal_senders + NUM_SPECIAL_WORKER_PROCS; Then on top of that, there's a pretty considerable fudge factor: lock.c scales up its shared-memory size request by 10%, and there is (from memory) about 100K slop space added on top of the total of shared-memory size requests from all modules, and all of that space is potentially available for the lock table to overflow into. (Other shared data structures could claim it too, but I think the lock table is the only one that we don't have a hard upper bound for.) If you really want to know how many locks can be taken in your particular setup, I'd counsel experimenting by deliberately exhausting the lock space. (Maybe not during production hours.) The precise limit will vary across PG versions, and potentially depend on other factors like what extensions you have loaded. regards, tom lane