v9a-0004-Further-condense-and-reduce-memory-used-by-pgstat.patch

text/x-patch

Filename: v9a-0004-Further-condense-and-reduce-memory-used-by-pgstat.patch
Type: text/x-patch
Part: 4
Message: Re: pg_stat_io_histogram

Patch

Format: format-patch
Series: patch v9-0004
Subject: Further condense and reduce memory used by pgstat_io(_histogram) subsystem by eliminating tracking of useless backend types: autovacum launcher and standalone backend.
File+
src/backend/utils/activity/pgstat_backend.c 2 2
src/backend/utils/activity/pgstat_io.c 11 6
src/include/pgstat.h 1 1
src/test/recovery/t/029_stats_restart.pl 0 5
src/test/regress/expected/stats.out 1 13
From 9b1c66b66540108aa0333f6cd1544e4e62d0f62b Mon Sep 17 00:00:00 2001
From: Jakub Wartak <jakub.wartak@enterprisedb.com>
Date: Fri, 6 Mar 2026 14:00:38 +0100
Subject: [PATCH v9 4/4] Further condense and reduce memory used by
 pgstat_io(_histogram) subsystem by eliminating tracking of useless backend
 types: autovacum launcher and standalone backend.

---
 src/backend/utils/activity/pgstat_backend.c |  4 ++--
 src/backend/utils/activity/pgstat_io.c      | 17 +++++++++++------
 src/include/pgstat.h                        |  2 +-
 src/test/recovery/t/029_stats_restart.pl    |  5 -----
 src/test/regress/expected/stats.out         | 14 +-------------
 5 files changed, 15 insertions(+), 27 deletions(-)

diff --git a/src/backend/utils/activity/pgstat_backend.c b/src/backend/utils/activity/pgstat_backend.c
index 4cd3fb923c9..beaf882b108 100644
--- a/src/backend/utils/activity/pgstat_backend.c
+++ b/src/backend/utils/activity/pgstat_backend.c
@@ -380,12 +380,12 @@ pgstat_tracks_backend_bktype(BackendType bktype)
 		case B_CHECKPOINTER:
 		case B_IO_WORKER:
 		case B_STARTUP:
+		case B_AUTOVAC_WORKER:
+		case B_STANDALONE_BACKEND:
 			return false;
 
-		case B_AUTOVAC_WORKER:
 		case B_BACKEND:
 		case B_BG_WORKER:
-		case B_STANDALONE_BACKEND:
 		case B_SLOTSYNC_WORKER:
 		case B_WAL_RECEIVER:
 		case B_WAL_SENDER:
diff --git a/src/backend/utils/activity/pgstat_io.c b/src/backend/utils/activity/pgstat_io.c
index 2a473640daf..a87a61f3993 100644
--- a/src/backend/utils/activity/pgstat_io.c
+++ b/src/backend/utils/activity/pgstat_io.c
@@ -94,6 +94,8 @@ pgstat_count_io_op(IOObject io_object, IOContext io_context, IOOp io_op,
 	Assert((unsigned int) io_object < IOOBJECT_NUM_TYPES);
 	Assert((unsigned int) io_context < IOCONTEXT_NUM_TYPES);
 	Assert(pgstat_is_ioop_tracked_in_bytes(io_op) || bytes == 0);
+	if(unlikely(MyBackendType == B_STANDALONE_BACKEND || MyBackendType == B_AUTOVAC_LAUNCHER))
+		return;
 	Assert(pgstat_tracks_io_op(MyBackendType, io_object, io_context, io_op));
 
 	PendingIOStats.counts[io_object][io_context][io_op] += cnt;
@@ -495,6 +497,9 @@ pgstat_io_snapshot_cb(void)
 * - Syslogger because it is not connected to shared memory
 * - Archiver because most relevant archiving IO is delegated to a
 *   specialized command or module
+* - Autovacum launcher because it hardly performs any IO
+* - Standalone backend as it is only used in unusual maintenance
+*   scenarios
 *
 * Function returns true if BackendType participates in the cumulative stats
 * subsystem for IO and false if it does not.
@@ -516,9 +521,10 @@ pgstat_tracks_io_bktype(BackendType bktype)
 		case B_DEAD_END_BACKEND:
 		case B_ARCHIVER:
 		case B_LOGGER:
+		case B_AUTOVAC_LAUNCHER:
+		case B_STANDALONE_BACKEND:
 			return false;
 
-		case B_AUTOVAC_LAUNCHER:
 		case B_AUTOVAC_WORKER:
 		case B_BACKEND:
 		case B_BG_WORKER:
@@ -526,7 +532,6 @@ pgstat_tracks_io_bktype(BackendType bktype)
 		case B_CHECKPOINTER:
 		case B_IO_WORKER:
 		case B_SLOTSYNC_WORKER:
-		case B_STANDALONE_BACKEND:
 		case B_STARTUP:
 		case B_WAL_RECEIVER:
 		case B_WAL_SENDER:
@@ -552,20 +557,20 @@ pgstat_remap_condensed_bktype(BackendType bktype) {
 		-1, /* B_INVALID */
 		0,
 		-1, /* B_DEAD_END_BACKEND */
+		-1, /* B_AUTOVAC_LAUNCHER */
 		1,
 		2,
 		3,
 		4,
+		-1, /* B_STANDALONE_BACKEND */
+		-1, /* B_ARCHIVER */
 		5,
 		6,
-		-1, /* B_ARCHIVER */
 		7,
 		8,
-		8,
+		9,
 		10,
 		11,
-		12,
-		13,
 		-1  /* B_LOGGER */
 	};
 
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 17f18711453..0c3f71bc272 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -369,7 +369,7 @@ typedef struct PgStat_PendingIO
 extern PgStat_PendingIO PendingIOStats;
 
 /* This needs to stay in sync with pgstat_tracks_io_bktype() */
-#define PGSTAT_USED_BACKEND_NUM_TYPES BACKEND_NUM_TYPES - 4
+#define PGSTAT_USED_BACKEND_NUM_TYPES BACKEND_NUM_TYPES - 6
 typedef struct PgStat_IO
 {
 	TimestampTz stat_reset_timestamp;
diff --git a/src/test/recovery/t/029_stats_restart.pl b/src/test/recovery/t/029_stats_restart.pl
index 33939c8701a..681fb9ac16d 100644
--- a/src/test/recovery/t/029_stats_restart.pl
+++ b/src/test/recovery/t/029_stats_restart.pl
@@ -22,12 +22,7 @@ my $sect = "startup";
 
 # Check some WAL statistics after a fresh startup.  The startup process
 # should have done WAL reads, and initialization some WAL writes.
-my $standalone_io_stats = io_stats('init', 'wal', 'standalone backend');
 my $startup_io_stats = io_stats('normal', 'wal', 'startup');
-cmp_ok(
-	'0', '<',
-	$standalone_io_stats->{writes},
-	"$sect: increased standalone backend IO writes");
 cmp_ok(
 	'0', '<',
 	$startup_io_stats->{reads},
diff --git a/src/test/regress/expected/stats.out b/src/test/regress/expected/stats.out
index b99462bf946..6f99c10fdb3 100644
--- a/src/test/regress/expected/stats.out
+++ b/src/test/regress/expected/stats.out
@@ -16,11 +16,6 @@ SHOW track_counts;  -- must be on
 SELECT backend_type, object, context FROM pg_stat_io
   ORDER BY backend_type COLLATE "C", object COLLATE "C", context COLLATE "C";
 backend_type|object|context
-autovacuum launcher|relation|bulkread
-autovacuum launcher|relation|init
-autovacuum launcher|relation|normal
-autovacuum launcher|wal|init
-autovacuum launcher|wal|normal
 autovacuum worker|relation|bulkread
 autovacuum worker|relation|init
 autovacuum worker|relation|normal
@@ -67,13 +62,6 @@ slotsync worker|relation|vacuum
 slotsync worker|temp relation|normal
 slotsync worker|wal|init
 slotsync worker|wal|normal
-standalone backend|relation|bulkread
-standalone backend|relation|bulkwrite
-standalone backend|relation|init
-standalone backend|relation|normal
-standalone backend|relation|vacuum
-standalone backend|wal|init
-standalone backend|wal|normal
 startup|relation|bulkread
 startup|relation|bulkwrite
 startup|relation|init
@@ -95,7 +83,7 @@ walsummarizer|wal|init
 walsummarizer|wal|normal
 walwriter|wal|init
 walwriter|wal|normal
-(79 rows)
+(67 rows)
 \a
 -- ensure that both seqscan and indexscan plans are allowed
 SET enable_seqscan TO on;
-- 
2.43.0