Improve accounting for memory used by shared hash tables pg_shmem_allocations tracks the memory allocated by ShmemInitStruct(), but for shared hash tables that covered only the header and hash directory. The remaining parts (segments and buckets) were allocated later using ShmemAlloc(), which does not update the shmem accounting. Thus, these allocations were not shown in pg_shmem_allocations.
This commit improves the situation by allocating all the hash table parts at once, using a single ShmemInitStruct() call. This way the ShmemIndex entries (and thus pg_shmem_allocations) better reflect the proper size of the hash table. This affects allocations for private (non-shared) hash tables too, as the hash_create() code is shared. For non-shared tables this however makes no practical difference. This changes the alignment a bit. ShmemAlloc() aligns the chunks using CACHELINEALIGN(), which means some parts (header, directory, segments) were aligned this way. Allocating all parts as a single chunk removes this (implicit) alignment. We've considered adding explicit alignment, but we've decided not to - it seems to be merely a coincidence due to using the ShmemAlloc() API, not due to necessity. Author: Rahila Syed <rahilasye...@gmail.com> Reviewed-by: Andres Freund <and...@anarazel.de> Reviewed-by: Nazir Bilal Yavuz <byavu...@gmail.com> Reviewed-by: Tomas Vondra <to...@vondra.me> Discussion: https://postgr.es/m/cah2l28vhzrankszhqz7dexurxkncxfirnuw68zd7+hvaqas...@mail.gmail.com Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/f5930f9a98ea65d659d41600a138e608988ad122 Modified Files -------------- src/backend/storage/ipc/shmem.c | 4 +- src/backend/utils/hash/dynahash.c | 281 +++++++++++++++++++++++++++++--------- src/include/utils/hsearch.h | 3 +- 3 files changed, 222 insertions(+), 66 deletions(-)