Re: Shared hash table allocations

Heikki Linnakangas <hlinnaka@iki.fi>

From: Heikki Linnakangas <hlinnaka@iki.fi>
To: Tomas Vondra <tomas@vondra.me>, "pgsql-hackers@postgresql.org" <pgsql-hackers@postgresql.org>, Robert Haas <robertmhaas@gmail.com>, Rahila Syed <rahilasyed90@gmail.com>
Date: 2026-03-30T22:02:52Z
Lists: pgsql-hackers
On 30/03/2026 18:28, Heikki Linnakangas wrote:
> To be clear, the init_size/max_size are not completely unused at the 
> moment: the lock manager sets max_size to 2 * init_size, and ... 

Huh, I only now realized the implications of the above. The formula in 
the lock manager is actually:

#define NLOCKENTS() \
	mul_size(max_locks_per_xact, add_size(MaxBackends, max_prepared_xacts))

...
	max_table_size = NLOCKENTS();
	init_table_size = max_table_size / 2;

So the initial size is only half of the maximum you get from 
max_locks_per_xacts * max_connections. That means that if something 
consumes the "wiggle room" shared memory, you might get *fewer* locks 
than you might expect based on the GUCs.

I've somehow always thought that it's the other way round, that you 
might get *more* locks than max_locks_per_xacts * max_connections if 
you're lucky, but that  max_connections * max_locks_per_xacts was 
guaranteed.

This will change with these patches.

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.

- Heikki




Commits

  1. Remove HASH_DIRSIZE, always use the default algorithm to select it

  2. Allocate all parts of shmem hash table from a single contiguous area

  3. Prevent shared memory hash tables from growing beyond initial size

  4. Merge init and max size options on shmem hash tables

  5. Change default of max_locks_per_transactions to 128

  6. Make the lock hash tables fixed-sized

  7. Remove 10% safety margin from lock manager hash table estimates

  8. Remove bogus "safety margin" from predicate.c shmem estimates

  9. Make ShmemIndex visible in the pg_shmem_allocations view

  10. Change the signature of dynahash's alloc function

  11. Remove HASH_SEGMENT option

  12. Use ShmemInitStruct to allocate shmem for semaphores