Shmem allocated wrong for custom cumulative stats

Heikki Linnakangas <hlinnaka@iki.fi>

From: Heikki Linnakangas <hlinnaka@iki.fi>
To: "pgsql-hackers@postgresql.org" <pgsql-hackers@postgresql.org>
Cc: Michael Paquier <michael@paquier.xyz>
Date: 2026-04-06T00:16:47Z
Lists: pgsql-hackers

Attachments

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

Commits

  1. Fix shmem allocation of fixed-sized custom stats kind

  2. Fix shared memory size of template code for custom fixed-sized pgstats