From fc233581899610e5b96b0f561dd74bd60eac17e3 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 v7 6/6] 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_io.c   | 17 +++++++++++------
 src/include/pgstat.h                     |  2 +-
 src/test/recovery/t/029_stats_restart.pl |  5 -----
 src/test/regress/expected/stats.out      | 14 +-------------
 4 files changed, 13 insertions(+), 25 deletions(-)

diff --git a/src/backend/utils/activity/pgstat_io.c b/src/backend/utils/activity/pgstat_io.c
index 1e9bff4da41..6c11430ad94 100644
--- a/src/backend/utils/activity/pgstat_io.c
+++ b/src/backend/utils/activity/pgstat_io.c
@@ -73,6 +73,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;
@@ -425,6 +427,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.
@@ -446,9 +451,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:
@@ -456,7 +462,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:
@@ -482,20 +487,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 22114d378bd..80476eda514 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -360,7 +360,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 cd00f35bf7a..7cefd37a99a 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

