From 87037f4187f59ea2780ee51f764c4d37a0d6c0a3 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@enterprisedb.com>
Date: Mon, 16 Apr 2018 20:51:08 +1200
Subject: [PATCH] Don't count zero-filled buffers as 'read' in EXPLAIN.

If you extend a relation, it should count as a block written, not read (we
write a zero-filled block).  If you ask for a zero-filled buffer, it shouldn't
be counted as read or written.

Thomas Munro
---
 src/backend/storage/buffer/bufmgr.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 01eabe57063..6ea88712004 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -733,7 +733,9 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 		bufHdr = LocalBufferAlloc(smgr, forkNum, blockNum, &found);
 		if (found)
 			pgBufferUsage.local_blks_hit++;
-		else
+		else if (isExtend)
+			pgBufferUsage.local_blks_written++;
+		else if (mode == RBM_NORMAL || mode == RBM_NORMAL_NO_LOG)
 			pgBufferUsage.local_blks_read++;
 	}
 	else
@@ -746,7 +748,9 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
 							 strategy, &found);
 		if (found)
 			pgBufferUsage.shared_blks_hit++;
-		else
+		else if (isExtend)
+			pgBufferUsage.shared_blks_written++;
+		else if (mode == RBM_NORMAL || mode == RBM_NORMAL_NO_LOG)
 			pgBufferUsage.shared_blks_read++;
 	}
 
-- 
2.17.0

