Re: Shared hash table allocations
Tomas Vondra <tomas@vondra.me>
On 3/27/26 23:37, Heikki Linnakangas wrote: > I'm starting a new thread for this topic that we discussed on the > "Better shared data structure management and resizable shared data > structures" thread: > > On 25/03/2026 20:37, Robert Haas wrote: >> On Sat, Mar 21, 2026 at 8:14 PM Heikki Linnakangas <hlinnaka@iki.fi> >> wrote: >>> I wonder if we should set aside a small amount of memory, like 10-20 kB, >>> in the fixed shmem segment for small structs like that. Currently, such >>> allocations will usually succeed because we leave some wiggle room, but >>> the can also be consumed by other things like locks. But we could >>> reserve a small amount of memory specifically for small allocations in >>> extensions like this. >> >> Yeah, I don't really understand why we let the lock table use up that >> space. I mean, I think it would be useful to have a way to let the >> lock table expand without a server restart, and I also suspect that we >> could come up with a less-silly data structure than the PROCLOCK hash, >> but also if the only thing keeping you from running out of lock space >> is the wiggle room, maybe you just need to bump up >> max_locks_per_transaction. Like, you could easily burn through the >> wiggle room, get an error anyway, and then later find that you also >> now can't load certain extensions without a server restart. > > Attached patch set tightens up all shared memory hash tables so that > they no longer use the "wiggle room". They are now truly fixed size. You > specify the number of elements in ShmemInitHash(), and that's it. > > This also addresses the accounting issue we currently have with hash > tables, that the size reported in pg_shmem_allocations only shows the > size of fixed header and the directory, not the actual hash buckets. > They were previously all lumped together in the "<anonymous>" section. > These patches fix that. > > There was an earlier attempt at that at last year [1], but it got > reverted. I hope my approach is less invasive: instead of changing > dynahash.c to use a single allocation directly, I'm providing it an > "alloc" callback that carves out the different parts it needs from the > single contiguous shmem area, which in turn is allocated with > ShmemInitStruct(). > Thanks! That earlier attempt was committed+reverted by me, and I certainly find this new approach much easier to understand (but I thought I understand the old patch too ...). A couple comments, based on a quick review: * 0001 - seems fine * 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? * 0003 - I'd probably rename CurrentDynaHashCxt to something that doesn't seem like a "global" variable, e.g. "dynahashCxt" * 0004 - seems fine, +1 to get rid of unused pieces * 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. * 0007 - this left one comment referencing HASH_DIRSIZE in dynahash.c * 0008 - makes sense regards -- Tomas Vondra
Commits
-
Remove HASH_DIRSIZE, always use the default algorithm to select it
- 4953a25b7f3b 19 (unreleased) landed
-
Allocate all parts of shmem hash table from a single contiguous area
- 9fe9ecd516b0 19 (unreleased) landed
-
Prevent shared memory hash tables from growing beyond initial size
- 999e9ebb51c9 19 (unreleased) landed
-
Merge init and max size options on shmem hash tables
- 9ebe1c4f2c7c 19 (unreleased) landed
-
Change default of max_locks_per_transactions to 128
- 79534f90657c 19 (unreleased) landed
-
Make the lock hash tables fixed-sized
- e1ad034809a4 19 (unreleased) landed
-
Remove 10% safety margin from lock manager hash table estimates
- 3e854d2ff1a6 19 (unreleased) landed
-
Remove bogus "safety margin" from predicate.c shmem estimates
- feb03dfecdc9 19 (unreleased) landed
-
Make ShmemIndex visible in the pg_shmem_allocations view
- 1bdbb211bbf3 19 (unreleased) landed
-
Change the signature of dynahash's alloc function
- 7b424e3108e4 19 (unreleased) landed
-
Remove HASH_SEGMENT option
- dde69621c324 19 (unreleased) landed
-
Use ShmemInitStruct to allocate shmem for semaphores
- 40c41dc77306 19 (unreleased) landed