From b900819822609b60388b47277fb4f738561968cd Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Tue, 6 Sep 2022 13:18:42 +0900
Subject: [PATCH v6 4/4] Change error messages to show the difference to the
 limit

Change the error message to show the difference to the limit instead
of the absolute value of the limit LSN.

"exceeds the limit by xxx MB"
---
 src/backend/replication/slot.c            | 18 ++++++++++++------
 src/test/recovery/t/019_replslot_limit.pl |  2 +-
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c
index a30d4e93cd..f06b34ab1f 100644
--- a/src/backend/replication/slot.c
+++ b/src/backend/replication/slot.c
@@ -51,6 +51,7 @@
 #include "storage/proc.h"
 #include "storage/procarray.h"
 #include "utils/builtins.h"
+#include "utils/dbsize.h"
 
 /*
  * Replication slot on-disk data structure.
@@ -1292,11 +1293,14 @@ InvalidatePossiblyObsoleteSlot(ReplicationSlot *s, XLogRecPtr oldestLSN,
 			 */
 			if (last_signaled_pid != active_pid)
 			{
+				char buf[64];
+
+				byte_size_pretty(buf, sizeof(buf),
+								 oldestLSN - restart_lsn);
 				ereport(LOG,
-						(errmsg("terminating process %d to release replication slot \"%s\" because its restart_lsn %X/%X exceeds the limit %X/%X",
+						(errmsg("terminating process %d to release replication slot \"%s\" because its restart_lsn %X/%X exceeds the limit by %s",
 								active_pid, NameStr(slotname),
-								LSN_FORMAT_ARGS(restart_lsn),
-								LSN_FORMAT_ARGS(oldestLSN)),
+								LSN_FORMAT_ARGS(restart_lsn), buf),
 						 errhint("You might need to increase max_slot_wal_keep_size.")));
 
 				(void) kill(active_pid, SIGTERM);
@@ -1317,6 +1321,8 @@ InvalidatePossiblyObsoleteSlot(ReplicationSlot *s, XLogRecPtr oldestLSN,
 		}
 		else
 		{
+			char buf[64];
+
 			/*
 			 * We hold the slot now and have already invalidated it; flush it
 			 * to ensure that state persists.
@@ -1333,11 +1339,11 @@ InvalidatePossiblyObsoleteSlot(ReplicationSlot *s, XLogRecPtr oldestLSN,
 			ReplicationSlotSave();
 			ReplicationSlotRelease();
 
+			byte_size_pretty(buf, sizeof(buf), oldestLSN - restart_lsn);
 			ereport(LOG,
-					(errmsg("invalidating slot \"%s\" because its restart_lsn %X/%X exceeds the limit %X/%X",
+					(errmsg("invalidating slot \"%s\" because its restart_lsn %X/%X exceeds the limit by %s",
 							NameStr(slotname),
-							LSN_FORMAT_ARGS(restart_lsn),
-							LSN_FORMAT_ARGS(oldestLSN)),
+							LSN_FORMAT_ARGS(restart_lsn), buf),
 					 errhint("You might need to increase max_slot_wal_keep_size.")));
 
 			/* done with this slot for now */
diff --git a/src/test/recovery/t/019_replslot_limit.pl b/src/test/recovery/t/019_replslot_limit.pl
index f6c34c6887..7c975f74c2 100644
--- a/src/test/recovery/t/019_replslot_limit.pl
+++ b/src/test/recovery/t/019_replslot_limit.pl
@@ -186,7 +186,7 @@ for (my $i = 0; $i < 10000; $i++)
 {
 	if (find_in_log(
 			$node_primary,
-			"invalidating slot \"rep1\" because its restart_lsn [0-9A-F/]+ exceeds the limit [0-9A-F/]+",
+			"invalidating slot \"rep1\" because its restart_lsn [0-9A-F/]+ exceeds the limit by ",
 			$logstart))
 	{
 		$invalidated = 1;
-- 
2.31.1

