]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix shmem allocation of fixed-sized custom stats kind
authorMichael Paquier <michael@paquier.xyz>
Tue, 7 Apr 2026 02:59:54 +0000 (11:59 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 7 Apr 2026 02:59:54 +0000 (11:59 +0900)
StatsShmemSize(), that computes the shmem size needed for pgstats,
includes the amount of shared memory wanted by all the custom stats
kinds registered.  However, the shared memory allocation was done by
ShmemAlloc() in StatsShmemInit(), meaning that the space reserved was
not used, wasting some memory.

These extra allocations would show up under "<anonymous>" in
pg_shmem_allocations, as the allocations done by ShmemAlloc() are not
tracked by ShmemIndexEnt.

Issue introduced by 7949d9594582.

Author: Heikki Linnakangas <hlinnaka@iki.fi>
Discussion: https://postgr.es/m/04b04387-92f5-476c-90b0-4064e71c5f37@iki.fi
Backpatch-through: 18

src/backend/utils/activity/pgstat_shmem.c

index 08ec264baa3a730f624138865944187fb51579cf..2b7f783ef7c453c4e09c3bc47e60f5e68e6b35e1 100644 (file)
@@ -142,8 +142,7 @@ StatsShmemSize(void)
                        continue;
 
                Assert(kind_info->shared_size != 0);
-
-               sz += MAXALIGN(kind_info->shared_size);
+               sz = add_size(sz, MAXALIGN(kind_info->shared_size));
        }
 
        return sz;
@@ -227,7 +226,8 @@ StatsShmemInit(void)
                                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];
                        }