Shmem allocated wrong for custom cumulative stats

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>
Subject: Shmem allocated wrong for custom cumulative stats
Date: 2026-04-06 00:16:47
Message-ID: 04b04387-92f5-476c-90b0-4064e71c5f37@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
0001-Use-the-allocated-space-properly-for-custom-stats-ki.patch text/x-patch 1.1 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2026-04-06 00:20:34 Re: Changing the state of data checksums in a running cluster
Previous Message Michael Paquier 2026-04-06 00:05:15 Re: Adding locks statistics