From c2fe7cd8ab100d9c72bd7290754ca5970962b349 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Fri, 10 Jul 2026 10:54:46 +0900
Subject: [PATCH v2] Restrict pg_stat_io entries for data checksum processes

The data checksums launcher and workers were exposed in pg_stat_io
with the same broad set of object/context combinations as general
background workers. However, several of those entries can never
accumulate I/O statistics for these processes, such as bulkwrite,
relation init, temporary relation, and launcher vacuum entries.

Teach pgstat_tracks_io_object() and pgstat_tracks_io_op() about the
actual I/O performed by the data checksum processes. Keep the entries
needed for catalog scans, including bulkread catalog scans, worker
relation processing with a vacuum access strategy, and WAL writes and
initialization, while excluding WAL reads and other object/context
combinations that can never be used.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Discussion: https://postgr.es/m/CAHGQGwHz_-nt+YkHDMRZNBZrnoHro8cMOgSwuXEmSYT6vxgQ=w@mail.gmail.com
Backpatch-through: 19
---
 src/backend/utils/activity/pgstat_io.c | 33 ++++++++++++++++++++++++++
 src/test/regress/expected/stats.out    |  9 +------
 2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/src/backend/utils/activity/pgstat_io.c b/src/backend/utils/activity/pgstat_io.c
index 38bae7b15d2..4f7a39aaa0e 100644
--- a/src/backend/utils/activity/pgstat_io.c
+++ b/src/backend/utils/activity/pgstat_io.c
@@ -464,6 +464,37 @@ pgstat_tracks_io_object(BackendType bktype, IOObject io_object,
 		io_context == IOCONTEXT_BULKWRITE)
 		return false;
 
+	/*
+	 * The data checksums launcher scans catalogs and emits WAL records for
+	 * checksum state changes. Catalog scans can use a bulkread strategy.
+	 */
+	if (bktype == B_DATACHECKSUMSWORKER_LAUNCHER)
+	{
+		if (io_object == IOOBJECT_WAL ||
+			(io_object == IOOBJECT_RELATION &&
+			 (io_context == IOCONTEXT_BULKREAD ||
+			  io_context == IOCONTEXT_NORMAL)))
+			return true;
+
+		return false;
+	}
+
+	/*
+	 * The worker also scans catalogs, then processes relations using a vacuum
+	 * access strategy. Catalog scans can use a bulkread strategy.
+	 */
+	if (bktype == B_DATACHECKSUMSWORKER_WORKER)
+	{
+		if (io_object == IOOBJECT_WAL ||
+			(io_object == IOOBJECT_RELATION &&
+			 (io_context == IOCONTEXT_BULKREAD ||
+			  io_context == IOCONTEXT_NORMAL ||
+			  io_context == IOCONTEXT_VACUUM)))
+			return true;
+
+		return false;
+	}
+
 	return true;
 }
 
@@ -507,6 +538,8 @@ pgstat_tracks_io_op(BackendType bktype, IOObject io_object,
 	if (io_object == IOOBJECT_WAL && io_op == IOOP_READ &&
 		(bktype == B_WAL_RECEIVER || bktype == B_BG_WRITER ||
 		 bktype == B_AUTOVAC_LAUNCHER || bktype == B_AUTOVAC_WORKER ||
+		 bktype == B_DATACHECKSUMSWORKER_LAUNCHER ||
+		 bktype == B_DATACHECKSUMSWORKER_WORKER ||
 		 bktype == B_WAL_WRITER))
 		return false;
 
diff --git a/src/test/regress/expected/stats.out b/src/test/regress/expected/stats.out
index 03cbc1cdef5..e230356de13 100644
--- a/src/test/regress/expected/stats.out
+++ b/src/test/regress/expected/stats.out
@@ -52,19 +52,12 @@ client backend|temp relation|normal
 client backend|wal|init
 client backend|wal|normal
 datachecksums launcher|relation|bulkread
-datachecksums launcher|relation|bulkwrite
-datachecksums launcher|relation|init
 datachecksums launcher|relation|normal
-datachecksums launcher|relation|vacuum
-datachecksums launcher|temp relation|normal
 datachecksums launcher|wal|init
 datachecksums launcher|wal|normal
 datachecksums worker|relation|bulkread
-datachecksums worker|relation|bulkwrite
-datachecksums worker|relation|init
 datachecksums worker|relation|normal
 datachecksums worker|relation|vacuum
-datachecksums worker|temp relation|normal
 datachecksums worker|wal|init
 datachecksums worker|wal|normal
 io worker|relation|bulkread
@@ -111,7 +104,7 @@ walsummarizer|wal|init
 walsummarizer|wal|normal
 walwriter|wal|init
 walwriter|wal|normal
-(95 rows)
+(88 rows)
 \a
 -- List of registered statistics kinds.
 SELECT id, name, fixed_amount,
-- 
2.55.0

