There's only one call of ShmemAlloc() left in the tree outside shmem.c,
in StatsShmemInit(), and that call doesn't look right:
The StatsShmemSize() function takes those allocations into account when
it calculates the size for the "Shared Memory Stats" shmem area, but
ShmemAlloc() doesn't use that reservation, it uses the general-purpose
unreserved shmem that then shows up as "<anonymous>" in
pg_shmem_allocations. The space reserved with ShmemRequestStruct() /
ShmemInitStruct() goes unused.
We should use the memory that we've reserved, per the attached patch.
One consequence of this fix though is that the allocations are now only
MAXALIGNed, while ShmemAlloc() uses CACHELINEALIGN(). Not sure which we
want.
I noticed this while working on the new shmem allocation functions, but
it's a pre-existing bug in stable branches too.
- Heikki
From 62d9a2fc8a56751fcd8a8ab780db7781374db5f0 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Mon, 6 Apr 2026 02:54:18 +0300
Subject: [PATCH 1/1] Use the allocated space properly for custom stats kind
---
src/backend/utils/activity/pgstat_shmem.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/activity/pgstat_shmem.c
index 955faf5ebc7..5e65a21fa83 100644
--- a/src/backend/utils/activity/pgstat_shmem.c
+++ b/src/backend/utils/activity/pgstat_shmem.c
@@ -189,6 +189,7 @@ StatsShmemInit(void *arg)
* efficiency win.
*/
ctl->raw_dsa_area = p;
+ p += pgstat_dsa_init_size();
dsa = dsa_create_in_place(ctl->raw_dsa_area,
pgstat_dsa_init_size(),
LWTRANCHE_PGSTATS_DSA, NULL);
@@ -242,7 +243,8 @@ StatsShmemInit(void *arg)
int idx = kind - PGSTAT_KIND_CUSTOM_MIN;
Assert(kind_info->shared_size != 0);
- ctl->custom_data[idx] = ShmemAlloc(kind_info->shared_size);
+ ctl->custom_data[idx] = p;
+ p += MAXALIGN(kind_info->shared_size);
ptr = ctl->custom_data[idx];
}
--
2.47.3