Hi, The 0001* patch improved the accounting for the shared memory allocated for a hash table during hash_create. pg_shmem_allocations tracks the memory allocated by ShmemInitStruct, which, for shared hash tables, only covers memory allocated for the hash directory and control structure via ShmemInitHash. The hash segments and buckets are allocated using ShmemAllocNoError, which does not attribute the allocation to the hash table and also does not add it to ShmemIndex.
Therefore, these allocations are not tracked in pg_shmem_allocations. To improve this, include the allocation of segments and buckets in the initial allocation of the shared memory for the hash table, in ShmemInitHash. This will result in pg_shmem_allocations representing the total size of the initial hash table, including all the buckets and elements, instead of just the directory size. Like ShmemAllocNoError, the shared memory allocated by ShmemAlloc is not tracked by pg_shmem_allocations. The 0002* patch replaces most of the calls to ShmemAlloc with ShmemInitStruct to associate a name with the allocations and ensure that they get tracked by pg_shmem_allocations. I observed an improvement in total memory allocation by consolidating initial shared memory allocations for the hash table. For ex. the allocated size for the LOCK hash hash_create decreased from 801664 bytes to 799616 bytes. Please find the attached patches, which I will add to the March Commitfest. Thank you, Rahila Syed
0001-Account-for-initial-shared-memory-allocated-during-h.patch
Description: Binary data
0002-Replace-ShmemAlloc-calls-by-ShmemInitStruct.patch
Description: Binary data