Skip to content

Commit f5930f9

Browse files
committed
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 <rahilasyed90@gmail.com> Reviewed-by: Andres Freund <andres@anarazel.de> Reviewed-by: Nazir Bilal Yavuz <byavuz81@gmail.com> Reviewed-by: Tomas Vondra <tomas@vondra.me> Discussion: https://postgr.es/m/CAH2L28vHzRankszhqz7deXURxKncxfirnuW68zD7+hVAqaS5GQ@mail.gmail.com
1 parent bd17896 commit f5930f9

File tree

3 files changed

+222
-66
lines changed

3 files changed

+222
-66
lines changed

src/backend/storage/ipc/shmem.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "storage/shmem.h"
7474
#include "storage/spin.h"
7575
#include "utils/builtins.h"
76+
#include "utils/dynahash.h"
7677

7778
static void *ShmemAllocRaw(Size size, Size *allocated_size);
7879

@@ -346,7 +347,8 @@ ShmemInitHash(const char *name, /* table string name for shmem index */
346347

347348
/* look it up in the shmem index */
348349
location = ShmemInitStruct(name,
349-
hash_get_shared_size(infoP, hash_flags),
350+
hash_get_size(infoP, hash_flags,
351+
init_size, true),
350352
&found);
351353

352354
/*

0 commit comments

Comments
 (0)