From e1ad034809a4628b995ae458d0d56744ca3b5ff3 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Fri, 3 Apr 2026 20:27:16 +0300 Subject: [PATCH] Make the lock hash tables fixed-sized This prevents the LOCK table from "stealing" space that was originally calculated for the PROLOCK table, and vice versa. That was weirdly indeterministic so that if you e.g. took a lot of locks consuming all the available shared memory for the LOCK table, subsequent transactions that needed the more space for the PROCLOCK table would fail, but if you restarted the system then the space would be available for PROCLOCK again. Better to be strict and predictable, even though that means that in many cases you can acquire far fewer locks than before. This also prevents the lock hash tables from using up the general-purpose 100 kB reserve we set aside for "stuff that's too small to bother estimating" in CalculateShmemSize(). We are pretty good at accounting for everything nowadays, so we could probably make that reservation smaller, but I'll leave that for another commit. Reviewed-by: Ashutosh Bapat Reviewed-by: Matthias van de Meent Discussion: https://www.postgresql.org/message-id/e07be2ba-856b-4ff5-8313-8b58b6b4e4d0@iki.fi --- src/backend/storage/lmgr/lock.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c index 2159de9015a..aa0f7bf4a1c 100644 --- a/src/backend/storage/lmgr/lock.c +++ b/src/backend/storage/lmgr/lock.c @@ -445,16 +445,14 @@ void LockManagerShmemInit(void) { HASHCTL info; - int64 init_table_size, - max_table_size; + int64 max_table_size; bool found; /* - * Compute init/max size to request for lock hashtables. Note these - * calculations must agree with LockManagerShmemSize! + * Compute sizes for lock hashtables. Note that these calculations must + * agree with LockManagerShmemSize! */ max_table_size = NLOCKENTS(); - init_table_size = max_table_size / 2; /* * Allocate hash table for LOCK structs. This stores per-locked-object @@ -465,14 +463,14 @@ LockManagerShmemInit(void) info.num_partitions = NUM_LOCK_PARTITIONS; LockMethodLockHash = ShmemInitHash("LOCK hash", - init_table_size, + max_table_size, max_table_size, &info, - HASH_ELEM | HASH_BLOBS | HASH_PARTITION); + HASH_ELEM | HASH_BLOBS | + HASH_PARTITION | HASH_FIXED_SIZE); /* Assume an average of 2 holders per lock */ max_table_size *= 2; - init_table_size *= 2; /* * Allocate hash table for PROCLOCK structs. This stores @@ -484,10 +482,11 @@ LockManagerShmemInit(void) info.num_partitions = NUM_LOCK_PARTITIONS; LockMethodProcLockHash = ShmemInitHash("PROCLOCK hash", - init_table_size, + max_table_size, max_table_size, &info, - HASH_ELEM | HASH_FUNCTION | HASH_PARTITION); + HASH_ELEM | HASH_FUNCTION | + HASH_FIXED_SIZE | HASH_PARTITION); /* * Allocate fast-path structures.