v6-0002-Convert-PgStat_IO-to-pointer-to-avoid-huge-static.patch
text/x-patch
Filename: v6-0002-Convert-PgStat_IO-to-pointer-to-avoid-huge-static.patch
Type: text/x-patch
Part: 1
Message:
Re: pg_stat_io_histogram
Patch
Format: format-patch
Series: patch v6-0002
Subject: Convert PgStat_IO to pointer to avoid huge static memory allocation if not used
| File | + | − |
|---|---|---|
| src/backend/utils/activity/pgstat.c | 8 | 1 |
| src/backend/utils/activity/pgstat_io.c | 12 | 3 |
| src/include/utils/pgstat_internal.h | 1 | 1 |
From 50517c03c8f118e14bb31d06e4cdf7627866a668 Mon Sep 17 00:00:00 2001
From: Jakub Wartak <jakub.wartak@enterprisedb.com>
Date: Mon, 2 Mar 2026 08:20:09 +0100
Subject: [PATCH v6 2/2] Convert PgStat_IO to pointer to avoid huge static
memory allocation if not used
---
src/backend/utils/activity/pgstat.c | 9 ++++++++-
src/backend/utils/activity/pgstat_io.c | 15 ++++++++++++---
src/include/utils/pgstat_internal.h | 2 +-
3 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activity/pgstat.c
index 11bb71cad5a..62a950987de 100644
--- a/src/backend/utils/activity/pgstat.c
+++ b/src/backend/utils/activity/pgstat.c
@@ -1634,10 +1634,17 @@ pgstat_write_statsfile(void)
pgstat_build_snapshot_fixed(kind);
if (pgstat_is_kind_builtin(kind))
- ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
+ {
+ if(kind == PGSTAT_KIND_IO)
+ ptr = (char *) pgStatLocal.snapshot.io;
+ else
+ ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
+ }
else
ptr = pgStatLocal.snapshot.custom_data[kind - PGSTAT_KIND_CUSTOM_MIN];
+ Assert(ptr != NULL);
+
fputc(PGSTAT_FILE_ENTRY_FIXED, fpout);
pgstat_write_chunk_s(fpout, &kind);
pgstat_write_chunk(fpout, ptr, info->shared_data_len);
diff --git a/src/backend/utils/activity/pgstat_io.c b/src/backend/utils/activity/pgstat_io.c
index 148a2a9c7d5..b5c75e67781 100644
--- a/src/backend/utils/activity/pgstat_io.c
+++ b/src/backend/utils/activity/pgstat_io.c
@@ -19,6 +19,7 @@
#include "executor/instrument.h"
#include "port/pg_bitutils.h"
#include "storage/bufmgr.h"
+#include "utils/memutils.h"
#include "utils/pgstat_internal.h"
static PgStat_PendingIO PendingIOStats;
@@ -197,7 +198,7 @@ pgstat_fetch_stat_io(void)
{
pgstat_snapshot_fixed(PGSTAT_KIND_IO);
- return &pgStatLocal.snapshot.io;
+ return pgStatLocal.snapshot.io;
}
/*
@@ -344,6 +345,9 @@ pgstat_io_init_shmem_cb(void *stats)
for (int i = 0; i < BACKEND_NUM_TYPES; i++)
LWLockInitialize(&stat_shmem->locks[i], LWTRANCHE_PGSTATS_DATA);
+
+ /* this might end up being lazily allocated in pgstat_io_snapshot_cb() */
+ pgStatLocal.snapshot.io = NULL;
}
void
@@ -371,11 +375,16 @@ pgstat_io_reset_all_cb(TimestampTz ts)
void
pgstat_io_snapshot_cb(void)
{
+ /* allocate memory only when necessary on first use */
+ if (unlikely(pgStatLocal.snapshot.io == NULL))
+ pgStatLocal.snapshot.io = MemoryContextAllocZero(TopMemoryContext,
+ sizeof(PgStat_IO));
+
for (int i = 0; i < BACKEND_NUM_TYPES; i++)
{
LWLock *bktype_lock = &pgStatLocal.shmem->io.locks[i];
PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
- PgStat_BktypeIO *bktype_snap = &pgStatLocal.snapshot.io.stats[i];
+ PgStat_BktypeIO *bktype_snap = &pgStatLocal.snapshot.io->stats[i];
LWLockAcquire(bktype_lock, LW_SHARED);
@@ -384,7 +393,7 @@ pgstat_io_snapshot_cb(void)
* the reset timestamp as well.
*/
if (i == 0)
- pgStatLocal.snapshot.io.stat_reset_timestamp =
+ pgStatLocal.snapshot.io->stat_reset_timestamp =
pgStatLocal.shmem->io.stats.stat_reset_timestamp;
/* using struct assignment due to better type safety */
diff --git a/src/include/utils/pgstat_internal.h b/src/include/utils/pgstat_internal.h
index 9b8fbae00ed..407657e060c 100644
--- a/src/include/utils/pgstat_internal.h
+++ b/src/include/utils/pgstat_internal.h
@@ -600,7 +600,7 @@ typedef struct PgStat_Snapshot
PgStat_CheckpointerStats checkpointer;
- PgStat_IO io;
+ PgStat_IO *io;
PgStat_SLRUStats slru[SLRU_NUM_ELEMENTS];
--
2.43.0