From 8bd5fc925ea82a492c0374d3f6e7a635b6b8b40f Mon Sep 17 00:00:00 2001
From: shihao zhong <zhong950419@gmail.com>
Date: Mon, 23 Nov 2026 22:21:54 -0500
Subject: [PATCH 2/4] Add pgstat_count_smgr_io_op_time()

Relation block reads, writes and extends are counted through it.  It
takes the IOObject from the SMgrRelation.  No behavior change.
---
 src/backend/storage/buffer/bufmgr.c    | 42 ++++++++++++--------------
 src/backend/storage/buffer/localbuf.c  |  8 ++---
 src/backend/utils/activity/pgstat_io.c | 35 +++++++++++++++++++--
 src/include/pgstat.h                   |  5 +++
 4 files changed, 61 insertions(+), 29 deletions(-)

diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 4dbc25c6c50..71d1724e3a6 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -658,9 +658,9 @@ static pg_always_inline void TrackBufferHit(IOObject io_object,
 											ForkNumber forknum, BlockNumber blocknum);
 static Buffer GetVictimBuffer(BufferAccessStrategy strategy, IOContext io_context);
 static void FlushUnlockedBuffer(BufferDesc *buf, SMgrRelation reln,
-								IOObject io_object, IOContext io_context);
+								IOContext io_context);
 static void FlushBuffer(BufferDesc *buf, SMgrRelation reln,
-						IOObject io_object, IOContext io_context);
+						IOContext io_context);
 static void FindAndDropRelationBuffers(RelFileLocator rlocator,
 									   ForkNumber forkNum,
 									   BlockNumber nForkBlock,
@@ -1837,8 +1837,8 @@ WaitReadBuffers(ReadBuffersOperation *operation)
 				 * itself was already counted earlier in AsyncReadBuffers() --
 				 * either by us or by another backend if this is a foreign IO.
 				 */
-				pgstat_count_io_op_time(io_object, io_context, IOOP_READ,
-										io_start, 0, 0);
+				pgstat_count_smgr_io_op_time(operation->smgr, io_context,
+											 IOOP_READ, io_start, 0, 0);
 			}
 			else
 			{
@@ -2157,8 +2157,8 @@ AsyncReadBuffers(ReadBuffersOperation *operation, int *nblocks_progress)
 	smgrstartreadv(ioh, operation->smgr, forknum,
 				   blocknum,
 				   io_pages, io_buffers_len);
-	pgstat_count_io_op_time(io_object, io_context, IOOP_READ,
-							io_start, 1, io_buffers_len * BLCKSZ);
+	pgstat_count_smgr_io_op_time(operation->smgr, io_context, IOOP_READ,
+								 io_start, 1, io_buffers_len * BLCKSZ);
 
 	if (persistence == RELPERSISTENCE_TEMP)
 		pgBufferUsage.local_blks_read += io_buffers_len;
@@ -2635,7 +2635,7 @@ again:
 		}
 
 		/* OK, do the I/O */
-		FlushBuffer(buf_hdr, NULL, IOOBJECT_RELATION, io_context);
+		FlushBuffer(buf_hdr, NULL, io_context);
 		LockBuffer(buf, BUFFER_LOCK_UNLOCK);
 
 		ScheduleBufferTagForWriteback(&BackendWritebackContext, io_context,
@@ -3045,8 +3045,8 @@ ExtendBufferedRelShared(BufferManagerRelation bmr,
 	if (!(flags & EB_SKIP_EXTENSION_LOCK))
 		UnlockRelationForExtension(bmr.rel, ExclusiveLock);
 
-	pgstat_count_io_op_time(IOOBJECT_RELATION, io_context, IOOP_EXTEND,
-							io_start, 1, extend_by * BLCKSZ);
+	pgstat_count_smgr_io_op_time(BMR_GET_SMGR(bmr), io_context, IOOP_EXTEND,
+								 io_start, 1, extend_by * BLCKSZ);
 
 	/* Set BM_VALID, terminate IO, and wake up any waiters */
 	for (uint32 i = 0; i < extend_by; i++)
@@ -4200,7 +4200,7 @@ SyncOneBuffer(int buf_id, bool skip_recently_used, WritebackContext *wb_context)
 	 */
 	PinBuffer_Locked(bufHdr);
 
-	FlushUnlockedBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
+	FlushUnlockedBuffer(bufHdr, NULL, IOCONTEXT_NORMAL);
 
 	tag = bufHdr->tag;
 
@@ -4531,8 +4531,7 @@ BufferGetTag(Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum,
  * as the second parameter.  If not, pass NULL.
  */
 static void
-FlushBuffer(BufferDesc *buf, SMgrRelation reln, IOObject io_object,
-			IOContext io_context)
+FlushBuffer(BufferDesc *buf, SMgrRelation reln, IOContext io_context)
 {
 	XLogRecPtr	recptr;
 	ErrorContextCallback errcallback;
@@ -4629,8 +4628,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln, IOObject io_object,
 	 * When a strategy is not in use, the write can only be a "regular" write
 	 * of a dirty shared buffer (IOCONTEXT_NORMAL IOOP_WRITE).
 	 */
-	pgstat_count_io_op_time(io_object, io_context,
-							IOOP_WRITE, io_start, 1, BLCKSZ);
+	pgstat_count_smgr_io_op_time(reln, io_context,
+								 IOOP_WRITE, io_start, 1, BLCKSZ);
 
 	pgBufferUsage.shared_blks_written++;
 
@@ -4654,13 +4653,12 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln, IOObject io_object,
  * before/after calling FlushBuffer().
  */
 static void
-FlushUnlockedBuffer(BufferDesc *buf, SMgrRelation reln,
-					IOObject io_object, IOContext io_context)
+FlushUnlockedBuffer(BufferDesc *buf, SMgrRelation reln, IOContext io_context)
 {
 	Buffer		buffer = BufferDescriptorGetBuffer(buf);
 
 	BufferLockAcquire(buffer, buf, BUFFER_LOCK_SHARE_EXCLUSIVE);
-	FlushBuffer(buf, reln, io_object, io_context);
+	FlushBuffer(buf, reln, io_context);
 	BufferLockUnlock(buffer, buf);
 }
 
@@ -5260,7 +5258,7 @@ FlushRelationBuffers(Relation rel)
 			(buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
 		{
 			PinBuffer_Locked(bufHdr);
-			FlushUnlockedBuffer(bufHdr, srel, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
+			FlushUnlockedBuffer(bufHdr, srel, IOCONTEXT_NORMAL);
 			UnpinBuffer(bufHdr);
 		}
 		else
@@ -5355,7 +5353,7 @@ FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels)
 			(buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
 		{
 			PinBuffer_Locked(bufHdr);
-			FlushUnlockedBuffer(bufHdr, srelent->srel, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
+			FlushUnlockedBuffer(bufHdr, srelent->srel, IOCONTEXT_NORMAL);
 			UnpinBuffer(bufHdr);
 		}
 		else
@@ -5602,7 +5600,7 @@ FlushDatabaseBuffers(Oid dbid)
 			(buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
 		{
 			PinBuffer_Locked(bufHdr);
-			FlushUnlockedBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
+			FlushUnlockedBuffer(bufHdr, NULL, IOCONTEXT_NORMAL);
 			UnpinBuffer(bufHdr);
 		}
 		else
@@ -5628,7 +5626,7 @@ FlushOneBuffer(Buffer buffer)
 
 	Assert(BufferIsLockedByMe(buffer));
 
-	FlushBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
+	FlushBuffer(bufHdr, NULL, IOCONTEXT_NORMAL);
 }
 
 /*
@@ -7994,7 +7992,7 @@ EvictUnpinnedBufferInternal(BufferDesc *desc, bool *buffer_flushed)
 	/* If it was dirty, try to clean it once. */
 	if (buf_state & BM_DIRTY)
 	{
-		FlushUnlockedBuffer(desc, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
+		FlushUnlockedBuffer(desc, NULL, IOCONTEXT_NORMAL);
 		*buffer_flushed = true;
 	}
 
diff --git a/src/backend/storage/buffer/localbuf.c b/src/backend/storage/buffer/localbuf.c
index dd5351552d1..3897696680f 100644
--- a/src/backend/storage/buffer/localbuf.c
+++ b/src/backend/storage/buffer/localbuf.c
@@ -212,8 +212,8 @@ FlushLocalBuffer(BufferDesc *bufHdr, SMgrRelation reln)
 			  false);
 
 	/* Temporary table I/O does not use Buffer Access Strategies */
-	pgstat_count_io_op_time(IOOBJECT_TEMP_RELATION, IOCONTEXT_NORMAL,
-							IOOP_WRITE, io_start, 1, BLCKSZ);
+	pgstat_count_smgr_io_op_time(reln, IOCONTEXT_NORMAL,
+								 IOOP_WRITE, io_start, 1, BLCKSZ);
 
 	/* Mark not-dirty */
 	TerminateLocalBufferIO(bufHdr, true, 0, false);
@@ -469,8 +469,8 @@ ExtendBufferedRelLocal(BufferManagerRelation bmr,
 	/* actually extend relation */
 	smgrzeroextend(BMR_GET_SMGR(bmr), fork, first_block, extend_by, false);
 
-	pgstat_count_io_op_time(IOOBJECT_TEMP_RELATION, IOCONTEXT_NORMAL, IOOP_EXTEND,
-							io_start, 1, extend_by * BLCKSZ);
+	pgstat_count_smgr_io_op_time(BMR_GET_SMGR(bmr), IOCONTEXT_NORMAL, IOOP_EXTEND,
+								 io_start, 1, extend_by * BLCKSZ);
 
 	for (uint32 i = 0; i < extend_by; i++)
 	{
diff --git a/src/backend/utils/activity/pgstat_io.c b/src/backend/utils/activity/pgstat_io.c
index cec575966ae..699c1187b5a 100644
--- a/src/backend/utils/activity/pgstat_io.c
+++ b/src/backend/utils/activity/pgstat_io.c
@@ -18,6 +18,7 @@
 
 #include "executor/instrument.h"
 #include "storage/bufmgr.h"
+#include "storage/smgr.h"
 #include "utils/pgstat_internal.h"
 
 static PgStat_PendingIO PendingIOStats;
@@ -109,10 +110,14 @@ pgstat_prepare_io_time(bool track_io_guc)
  * pgBufferUsage is used for EXPLAIN.  pgBufferUsage has write and read stats
  * for shared, local and temporary blocks.  pg_stat_io does not track the
  * activity of temporary blocks, so these are ignored here.
+ *
+ * This is the timing part of pgstat_count_io_op_time() and
+ * pgstat_count_smgr_io_op_time(), inlined so that neither pays for a second
+ * call.
  */
-void
-pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op,
-						instr_time start_time, uint32 cnt, uint64 bytes)
+static pg_always_inline void
+pgstat_count_io_time_internal(IOObject io_object, IOContext io_context,
+							  IOOp io_op, instr_time start_time)
 {
 	if (!INSTR_TIME_IS_ZERO(start_time))
 	{
@@ -148,6 +153,30 @@ pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op,
 		pgstat_count_backend_io_op_time(io_object, io_context, io_op,
 										io_time);
 	}
+}
+
+void
+pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op,
+						instr_time start_time, uint32 cnt, uint64 bytes)
+{
+	pgstat_count_io_time_internal(io_object, io_context, io_op, start_time);
+
+	pgstat_count_io_op(io_object, io_context, io_op, cnt, bytes);
+}
+
+/*
+ * Like pgstat_count_io_op_time(), for block I/O on a relation.
+ */
+void
+pgstat_count_smgr_io_op_time(SMgrRelation reln, IOContext io_context,
+							 IOOp io_op, instr_time start_time,
+							 uint32 cnt, uint64 bytes)
+{
+	IOObject	io_object;
+
+	io_object = SmgrIsTemp(reln) ? IOOBJECT_TEMP_RELATION : IOOBJECT_RELATION;
+
+	pgstat_count_io_time_internal(io_object, io_context, io_op, start_time);
 
 	pgstat_count_io_op(io_object, io_context, io_op, cnt, bytes);
 }
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index f29b3ddba7a..81873094cd0 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -26,6 +26,7 @@ typedef struct FullTransactionId FullTransactionId;
 
 /* avoid including utils/relcache.h */
 typedef struct RelationData *Relation;
+typedef struct SMgrRelationData *SMgrRelation;
 
 
 /* ----------
@@ -710,6 +711,10 @@ extern instr_time pgstat_prepare_io_time(bool track_io_guc);
 extern void pgstat_count_io_op_time(IOObject io_object, IOContext io_context,
 									IOOp io_op, instr_time start_time,
 									uint32 cnt, uint64 bytes);
+extern void pgstat_count_smgr_io_op_time(SMgrRelation reln,
+										 IOContext io_context, IOOp io_op,
+										 instr_time start_time,
+										 uint32 cnt, uint64 bytes);
 
 extern PgStat_IO *pgstat_fetch_stat_io(void);
 extern const char *pgstat_get_io_context_name(IOContext io_context);
-- 
2.37.1 (Apple Git-137.1)

