From b54ca9f187b9dfb94b03e7a107f01df30e789925 Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Mon, 21 Mar 2022 05:37:54 +0000
Subject: [PATCH v6] Deduplicate checkpoint/restartpoint complete message

---
 src/backend/access/transam/xlog.c | 66 +++++++++++++------------------
 1 file changed, 28 insertions(+), 38 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 4ac3871c74..b5a6e0cd61 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -6094,6 +6094,7 @@ LogCheckpointEnd(bool restartpoint)
 				longest_msecs,
 				average_msecs;
 	uint64		average_sync_time;
+	StringInfoData logmsg;
 
 	CheckpointStats.ckpt_end_t = GetCurrentTimestamp();
 
@@ -6129,46 +6130,35 @@ LogCheckpointEnd(bool restartpoint)
 			CheckpointStats.ckpt_sync_rels;
 	average_msecs = (long) ((average_sync_time + 999) / 1000);
 
+	initStringInfo(&logmsg);
+
 	if (restartpoint)
-		ereport(LOG,
-				(errmsg("restartpoint complete: wrote %d buffers (%.1f%%); "
-						"%d WAL file(s) added, %d removed, %d recycled; "
-						"write=%ld.%03d s, sync=%ld.%03d s, total=%ld.%03d s; "
-						"sync files=%d, longest=%ld.%03d s, average=%ld.%03d s; "
-						"distance=%d kB, estimate=%d kB",
-						CheckpointStats.ckpt_bufs_written,
-						(double) CheckpointStats.ckpt_bufs_written * 100 / NBuffers,
-						CheckpointStats.ckpt_segs_added,
-						CheckpointStats.ckpt_segs_removed,
-						CheckpointStats.ckpt_segs_recycled,
-						write_msecs / 1000, (int) (write_msecs % 1000),
-						sync_msecs / 1000, (int) (sync_msecs % 1000),
-						total_msecs / 1000, (int) (total_msecs % 1000),
-						CheckpointStats.ckpt_sync_rels,
-						longest_msecs / 1000, (int) (longest_msecs % 1000),
-						average_msecs / 1000, (int) (average_msecs % 1000),
-						(int) (PrevCheckPointDistance / 1024.0),
-						(int) (CheckPointDistanceEstimate / 1024.0))));
+		appendStringInfo(&logmsg, _("restartpoint complete: "));
 	else
-		ereport(LOG,
-				(errmsg("checkpoint complete: wrote %d buffers (%.1f%%); "
-						"%d WAL file(s) added, %d removed, %d recycled; "
-						"write=%ld.%03d s, sync=%ld.%03d s, total=%ld.%03d s; "
-						"sync files=%d, longest=%ld.%03d s, average=%ld.%03d s; "
-						"distance=%d kB, estimate=%d kB",
-						CheckpointStats.ckpt_bufs_written,
-						(double) CheckpointStats.ckpt_bufs_written * 100 / NBuffers,
-						CheckpointStats.ckpt_segs_added,
-						CheckpointStats.ckpt_segs_removed,
-						CheckpointStats.ckpt_segs_recycled,
-						write_msecs / 1000, (int) (write_msecs % 1000),
-						sync_msecs / 1000, (int) (sync_msecs % 1000),
-						total_msecs / 1000, (int) (total_msecs % 1000),
-						CheckpointStats.ckpt_sync_rels,
-						longest_msecs / 1000, (int) (longest_msecs % 1000),
-						average_msecs / 1000, (int) (average_msecs % 1000),
-						(int) (PrevCheckPointDistance / 1024.0),
-						(int) (CheckPointDistanceEstimate / 1024.0))));
+		appendStringInfo(&logmsg, _("checkpoint complete: "));
+
+	appendStringInfo(&logmsg,
+					 _("wrote %d buffers (%.1f%%); "
+					  "%d WAL file(s) added, %d removed, %d recycled; "
+					  "write=%ld.%03d s, sync=%ld.%03d s, total=%ld.%03d s; "
+					  "sync files=%d, longest=%ld.%03d s, average=%ld.%03d s; "
+					  "distance=%d kB, estimate=%d kB"),
+					   CheckpointStats.ckpt_bufs_written,
+					   (double) CheckpointStats.ckpt_bufs_written * 100 / NBuffers,
+					   CheckpointStats.ckpt_segs_added,
+					   CheckpointStats.ckpt_segs_removed,
+					   CheckpointStats.ckpt_segs_recycled,
+					   write_msecs / 1000, (int) (write_msecs % 1000),
+					   sync_msecs / 1000, (int) (sync_msecs % 1000),
+					   total_msecs / 1000, (int) (total_msecs % 1000),
+					   CheckpointStats.ckpt_sync_rels,
+					   longest_msecs / 1000, (int) (longest_msecs % 1000),
+					   average_msecs / 1000, (int) (average_msecs % 1000),
+					   (int) (PrevCheckPointDistance / 1024.0),
+					   (int) (CheckPointDistanceEstimate / 1024.0));
+
+	ereport(LOG, errmsg_internal("%s", logmsg.data));
+	pfree(logmsg.data);
 }
 
 /*
-- 
2.25.1

