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-30T15:28:58Z
Lists: pgsql-hackers

Attachments

On 28/03/2026 02:14, Tomas Vondra wrote:
> * 0002 - +1 to getting rid of HASH_SEGMENT, but I don't see the point of
> renaming DEF_SEGSIZE to HASH_SEGSIZE. Isn't that a bit unnecessary?

DEF_SEGSIZE stands for "default segsize", but after this commit it's not 
merely the default, it's the same hard-coded constant for every hash 
table. That's why it seems prudent to rename it.

> * 0003 - I'd probably rename CurrentDynaHashCxt to something that
> doesn't seem like a "global" variable, e.g. "dynahashCxt"

Renamed it to "hcxt", as that's what the corresponding field in HTAB is 
called.

> * 0004 - seems fine, +1 to get rid of unused pieces

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 
wait_event.c used constants 16 and 128.

The point is that it doesn't give you a very wide range of scalability, 
and I think it's better to not be flexible in that fashion. I would call 
it sloppiness rather than flexibility.

> * 0005 - seems fine
> 
> * 0006 - Doesn't this completely change the alignment? ShmemHashAlloc
> used to call ShmemAllocRaw, which is very careful to use CACHELINEALIGN.
> But now ShmemHashAlloc just does MAXALIGN, which ShmemAllocRaw claims is
> not enough on modern systems.

dynahash.c allocates multiple elements in each alloc() call, so even 
though ShmemAllocRaw() returns a cacheline-aligned block, the individual 
elements were not cacheline-aligned before this patch either. See 
element_alloc() and choose_nelem_alloc().

> * 0007 - this left one comment referencing HASH_DIRSIZE in dynahash.c

Fixed

- 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