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 1w8FED-000LLC-1o for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Apr 2026 10:24:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w8FEC-004slS-1M for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Apr 2026 10:24: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 1w8FEC-004slK-0G for pgsql-hackers@lists.postgresql.org; Thu, 02 Apr 2026 10:24:44 +0000 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w8FE9-00000000B86-4BwB for pgsql-hackers@postgresql.org; Thu, 02 Apr 2026 10:24:44 +0000 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-38a723b2b29so7059391fa.2 for ; Thu, 02 Apr 2026 03:24:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1775125481; cv=none; d=google.com; s=arc-20240605; b=VUAYJ13EaczDoJdlohcRnfhLSc3h8jfSrHuqOfHkeKyd8Thl3qatTPimsQVuVEdM76 BacJXm2hFrTf+F/X+jvBZR4hdDUMqDpzo+oJAJyhrITWFpLJXDndpBvGpdMG8gt5v2bK 5sRYyJ/SflVu9+AdQVR6ySRDtXnxZ97jCre4FDw0IU9flcmCHJCKL4MhGtjZxE9Yg7mk 3quyST0WxRhvXL8eG/cfTIGQF5Wk454nieEAPd3EbwFZBLpI7Y+4dPqjdg8TJQ8/TGpL sdCPH7MNJvhU2tupAUWoJVt98quoUXqfwTXhLCTP9EP0iT0w8olLw9NBxN/VSAhph2/v e7xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=laFUkIdRwXJS5X22AILAhJIhG9lDFizp/Ro82/4IQN8=; fh=3LgpuXTMIUl3HuAcWQ9CBIDuuj5Nf9rTjV6Ks4rYGY4=; b=O3WAFNGEVyexeVaaveYe3CondyihN36RNAGnJiI9S9hppJwuLWhp+pYqXg9zRNe3iY wPwYV0Bbev8IS82G8TXlW71jPt1A18Y9pQEfFyNbXEVBCmxQdF25d2HHuPnbChXi/OSa N8AeWHvXejmgJ3NaO5r+mPA4nImIC4KeZCzC4i5hpRZzAz4mhPWHtn97fOTFfQNwnjCm isNBvaPHzklqeO/K8EcRaFJVO0LPwwLj3stecPQabEWjfjFwJWCvoQi3/hPZaJPT1z1t SDgdAo+8va9HsKjS67QXQvpinNinnQ9iBxMaKlKvZNIvDb9tpjp+4r8pU0+gZxAZkNSg feKw==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775125481; x=1775730281; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=laFUkIdRwXJS5X22AILAhJIhG9lDFizp/Ro82/4IQN8=; b=Y/4g86victSfUxtUkINoJtztY5oL927LeYgt8uniwm7danKsfz68G/q+my7v/tBRmV LkVZbFglDcCeConug8J+7cZ3KDfU8dI3R3AxWiGshaZqzcPByeFeF4gRBX8KLPCdf78+ tampLUeAnHgqi6h7HZgBi+YjG3ybekE5zKQtQD3nAZol2KS8+w9Yu4fNMoM8TXiwIzP7 x2RSG2Q8o1aDW/0VJGp6EVa4PnRHZv7DKQNQN4J4nJk9Xbm+MP0cfVtEMJhIwTXdsace JRzzdDkiAPexJdTiZMgjeMDmfuB9UeRatkd1XKaXuV+UHHxxfeb+FTlnEkQWmy3Ua9Ow XSCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775125481; x=1775730281; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=laFUkIdRwXJS5X22AILAhJIhG9lDFizp/Ro82/4IQN8=; b=m68RkmwNJaX2dWxTt7aIIkiUpLgcKW0O63XTwdNMRlHDebn3eLjqsMIia3Y8xALdtW 7hKNW/kPz0ZrymftM3JzJ2c11xKVbGR7p3sno0LhFmisiHZJihDTGyL84OyLek0amdoI UZGppccMlGhpN5NY4jpddsK46h7rZdyDf9gxqVocBbXV+PowHC1ZThDF9ihLuIHER8Eh S+IZtO3rD8lHM7qJzjugeaV4Vf97mSdk0otPHlsOn9s7iTCGyEsCh4AopPRWLy7RwaN8 UU2U1xZ/fmIO5q1oSurMrqnpn4SQ1n57GiEW7Kz5eaLdd1GRD54hvsIOukUfB8dyU6su 0Fzg== X-Forwarded-Encrypted: i=1; AJvYcCV7sjtPl2Tr8sc7gySWHUsDSdpoUDyxevmVhVirTjx08pnAN7ZK83TlMhUsgGjimOdE7LcjFP2Mgg49csMc@postgresql.org X-Gm-Message-State: AOJu0YwTo1v54cZ4+rsIDf2Ok23KNPFLJ3kz6g+LPQE5+u5RQZyOQB/t Vj3i5lrJNKCLnqX9vWY6GuoAcu6wvjiPPyyI+uiPd/J8AJ2xDlg83+ofKDv6mGQ21TK9ckkVF2o /8W58B+1m0kSw7271lhagd/1PKvpd9UQ= X-Gm-Gg: ATEYQzzw2Uk3UuKRUeYjPq0PrwfwXVzWIvM+W/gPuOjdgRVWDVNV0v/MpHF6tDFGjn5 pVtQxW0HJQHRaouhqvxZ+kbmfGAPyPdVOpRLwrvpnNwZYM0ZWVOcyRtPju5BID/X+4CwCbIYJ6x CE+WSfMnR3/Vxf0a0HxhbtxXNO9fUbejcnFqxu3JFWVEzgeyiIxAQEBfFqZpgtPGYBPtxcdKml8 PQQtJoKCJN4aPuELJzyJhhjuKP0/hnDOkNevHU1xL98nxNwcAVFWDxZKZNwV/S6kKlaMostYz9W UhHYVMqWt9Oa6reO0wy6u7Kx3rYcTZiLfmd6I5ltI9VRFZi8pgSFhNnm7NCbRdecHP+Vf3E1JGD L6oiK X-Received: by 2002:a05:651c:902:b0:38a:49d1:3d with SMTP id 38308e7fff4ca-38cc302bdbdmr22852651fa.17.1775125481058; Thu, 02 Apr 2026 03:24:41 -0700 (PDT) MIME-Version: 1.0 References: <01ab1d41-3eda-4705-8bbd-af898f5007f1@iki.fi> <2981bb36-6bbe-4bdc-9a94-29b1114c79bd@vondra.me> <3026ec05-f664-4ebe-8bf6-0a1218b234ec@iki.fi> <19945803-6bcc-40fe-a14a-7dc5c462ed80@iki.fi> In-Reply-To: From: Matthias van de Meent Date: Thu, 2 Apr 2026 12:24:29 +0200 X-Gm-Features: AQROBzA1UkAuhwoXyB0eLKT9kQ49btcy1-lLrKz6siU4K_ybX221HJIYCDaXL1w Message-ID: Subject: Re: Shared hash table allocations To: Heikki Linnakangas Cc: Tomas Vondra , "pgsql-hackers@postgresql.org" , Robert Haas , Rahila Syed Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 31 Mar 2026 at 23:25, Heikki Linnakangas wrote: > > On 31/03/2026 01:02, Heikki Linnakangas wrote: > > I wonder if we should change the defaults somehow. In usual > > configurations, people are currently getting much more lock space than > > you'd expect based on max_connections and max_locks_per_transaction, and > > after these patches, they'll get much fewer locks. It might be prudent > > bump up the default max_locks_per_transaction setting so that you'd get > > roughly the same amount of locks in the default configuration. > > master: With the default configuration on master, the attached test > procedure can take 14927 locks before hitting "out of shared memory" > error. At that point, all the "wiggle room" is assigned for the LOCK > hash table. A different scenario could make the PROCLOCK hash table > consume all the wiggle room instead, but I believe running out of LOCK > space is more common, and I don't think it changes the big picture > anyway if you hit the ceiling with PROCLOCK instead. > > 0001: [...] LGTM > 0002: As the next step, I also removed the 10% safety margin from > lock.c. That reduced memory usage by another 320 kB, and the number of > locks went down from 14159 to 12815. LGTM > 0003: In patch 0003 I removed that flexibility by marking them both with > HASH_FIXED_SIZE, and making init_size equal to max_size. That also stops > the hash tables from using any of the other remaining wiggle room, > making them truly fixed-size. I think this patch finally gave me a good reason why PROCLOCK would've needed to be allocated with double the sizes of LOCK: LOCK is (was) initialized with only 50% of its max capacity. If PROCLOCK was initialized with the same parameters and all spare shmem is then allocated to other processes, then backends wouldn't be able to safely use max_locks_per_transaction. To guarantee no OOMs when all backends use max_locks_per_transaction, PROCLOCK's size must be doubled to make sure PROCLOCK has sufficient space. (The same isn't usually an issue for LOCK, because it's very likely backends will operate on the same tables, and thus will be able to share most of the LOCK structs.) Now that LOCK is fully allocated, I think the size doubling can be removed, or possibly parameterized for those that need it. > 0004: To buy back that lock manager space in common out-of-the box > situations, I propose to bump up the default for > max_locks_per_transactions from 64 to 128. [...] > The number of locks you can > take after that is 17535, which more than on master (14927). Note that this is for one backend; with current sizing you could lock the same 17535 locks in at least one more backend. Patch LGTM. > Any thoughts, objections? Overall, I'm +1 on this change. I do have some general comments though, at least in part based on discussions in the hackers discord last year[0]: 1.) We'll need to clearly advertise the changed, more strict behaviour of the heavy-weight locking system in the release notes. 2.) (Related) We probably should make it easier for DBAs to monitor lock counts now that we enforce the limit more strictly. This could take the form of (optional) logging that alerts when a session exceeds some threshold number of locks in a transaction (e.g. 100% and 200% of max_locks_per_transaction), or as a metric in pg_stat_{activity,databases} as total locks taken/max number of locks taken in a transaction. 3.) (Related) We should probably parameterize the LOCK-to-PROCLOCK ratio. LOCK is large, and especially on systems with high values of max_connections (where the additional LOCKs will go unused) the overhead of carrying all those additional LOCKs would go up to 50% of the added memory usage (LOCK at 152+24=176B, PROCLOCK at 2*(64+24B)=176B). It'd be nice if we could avoid allocating that memory. Kind regards, Matthias van de Meent Databricks (https://www.databricks.com) [0] starting at https://discord.com/channels/1258108670710124574/1266090488415654032/1442879718285119518