pgsql: Improve accounting for memory used by shared hash tables - Mailing list pgsql-committers

From Tomas Vondra
Subject pgsql: Improve accounting for memory used by shared hash tables
Date
Msg-id E1tzzpV-002JTf-1H@gemulon.postgresql.org
Whole thread Raw
Responses Re: pgsql: Improve accounting for memory used by shared hash tables
List pgsql-committers
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

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(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Need to do CommandCounterIncrement after StoreAttrMissingVal.
Next
From: Heikki Linnakangas
Date:
Subject: pgsql: docs: Fix column count attribute in table