From b0fb4d797697fc9d96f88a61b7464613f150cbed Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Thu, 21 Dec 2017 21:23:25 +0900
Subject: [PATCH v18 2/6] Add monitoring aid for max_slot_wal_keep_size

Adds two columns "status" and "remain" in pg_replication_slot. Setting
max_slot_wal_keep_size, replication connections may lose sync by a
long delay. The "status" column shows whether the slot is
reconnectable or not, or about to lose reserving WAL segments. The
"remain" column shows the remaining bytes of WAL that can be advance
until the slot loses required WAL records.
---
 contrib/test_decoding/expected/ddl.out |   4 +-
 contrib/test_decoding/sql/ddl.sql      |   2 +
 src/backend/access/transam/xlog.c      | 298 +++++++++++++++++++++----
 src/backend/catalog/system_views.sql   |   4 +-
 src/backend/replication/slot.c         |  64 ------
 src/backend/replication/slotfuncs.c    |  39 +++-
 src/include/access/xlog.h              |  18 ++
 src/include/catalog/pg_proc.dat        |   6 +-
 src/include/replication/slot.h         |   1 -
 src/test/regress/expected/rules.out    |   6 +-
 10 files changed, 328 insertions(+), 114 deletions(-)

diff --git a/contrib/test_decoding/expected/ddl.out b/contrib/test_decoding/expected/ddl.out
index 2c999fd3eb..cf0318f697 100644
--- a/contrib/test_decoding/expected/ddl.out
+++ b/contrib/test_decoding/expected/ddl.out
@@ -723,8 +723,8 @@ SELECT pg_drop_replication_slot('regression_slot');
 (1 row)
 
 /* check that the slot is gone */
+\x
 SELECT * FROM pg_replication_slots;
- slot_name | plugin | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn 
------------+--------+-----------+--------+----------+-----------+--------+------------+------+--------------+-------------+---------------------
 (0 rows)
 
+\x
diff --git a/contrib/test_decoding/sql/ddl.sql b/contrib/test_decoding/sql/ddl.sql
index 856495c952..0f2b9992f7 100644
--- a/contrib/test_decoding/sql/ddl.sql
+++ b/contrib/test_decoding/sql/ddl.sql
@@ -387,4 +387,6 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'inc
 SELECT pg_drop_replication_slot('regression_slot');
 
 /* check that the slot is gone */
+\x
 SELECT * FROM pg_replication_slots;
+\x
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 7015300c77..8a83f87c8a 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -3900,6 +3900,55 @@ XLogGetLastRemovedSegno(void)
 	return lastRemovedSegNo;
 }
 
+/*
+ * Return the oldest WAL segment file.
+ *
+ * The returned value is XLogGetLastRemovedSegno() + 1 when the function
+ * returns a valid value.  Otherwise this function scans over WAL files and
+ * finds the oldest segment at the first time, which could be very slow.
+ */
+XLogSegNo
+FindOldestXLogFileSegNo(void)
+{
+	static XLogSegNo lastFoundOldestSeg = 0;
+	DIR		*xldir;
+	struct dirent *xlde;
+	XLogSegNo segno = XLogGetLastRemovedSegno();
+
+	if (segno > 0)
+		return segno + 1;
+
+	if (lastFoundOldestSeg > 0)
+		return lastFoundOldestSeg;
+
+	xldir = AllocateDir(XLOGDIR);
+	while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
+	{
+		TimeLineID tli;
+		XLogSegNo fsegno;
+
+		/* Ignore files that are not XLOG segments */
+		if (!IsXLogFileName(xlde->d_name) &&
+			!IsPartialXLogFileName(xlde->d_name))
+			continue;
+
+		XLogFromFileName(xlde->d_name, &tli, &fsegno, wal_segment_size);
+
+		/*
+		 * Get minimum segment ignoring timeline ID, the same way with
+		 * RemoveOldXlogFiles().
+		 */
+		if (segno == 0 || fsegno < segno)
+			segno = fsegno;
+	}
+
+	FreeDir(xldir);
+
+	lastFoundOldestSeg = segno;
+
+	return segno;
+}
+
 /*
  * Update the last removed segno pointer in shared memory, to reflect
  * that the given XLOG file has been removed.
@@ -9322,6 +9371,105 @@ CreateRestartPoint(int flags)
 	return true;
 }
 
+/*
+ * Detect availability of the record at given targetLSN.
+ *
+ * targetLSN is restart_lsn of a slot.
+ * walsender_pid is the slot's walsender PID.
+ * restBytes is pointer to uint64 variable, to store the remaining bytes until
+ * the slot goes into WAL_BEING_REMOVED state if max_slot_wal_keep_size >=
+ * 0. It is set only when WALAVAIL_NORMAL or WALAVAIL_PRESERVED is returned.
+ *
+ * Returns one of the following enum values.
+ *
+ * WALAVAIL_NORMAL_ means targetLSN is available because it is in the range of
+ * max_wal_size.  If max_slot_wal_keep_size is smaller than max_wal_size, this
+ * state is not returned.
+ *
+ * WALAVAIL_PRESERVED means it is still available by preserving extra segments
+ * beyond max_wal_size.
+ *
+ * WALAVAIL_BEING_REMOVED means it is being removed or already removed but the
+ * replication stream on the given slot is live yet. The state may transit to
+ * WALAVAIL_PRESERVED or WALAVAIL_NORMAL state if the walsender advances
+ * restart_lsn.
+ *
+ * WALAVAIL_REMOVED means it is definitly lost. The replication stream on the
+ * slot cannot continue.
+ *
+ * returns WALAVAIL_NULL if restart_lsn is invalid.
+ */
+WalAvailability
+GetWalAvailability(XLogRecPtr restart_lsn, pid_t walsender_pid)
+{
+	XLogRecPtr currpos;
+	XLogRecPtr slotPtr;
+	XLogSegNo currSeg;		/* segid of currpos */
+	XLogSegNo restartSeg;	/* segid of restart_lsn */
+	XLogSegNo oldestSeg;	/* actual oldest segid */
+	XLogSegNo oldestSegMaxWalSize;	/* oldest segid kept by max_wal_size */
+	XLogSegNo oldestSlotSeg;/* oldest segid kept by slot */
+	uint64	  keepSegs;
+
+	/* the case where the slot has never been activated */
+	if (XLogRecPtrIsInvalid(restart_lsn))
+		return WALAVAIL_INVALID_LSN;
+
+	currpos = GetXLogWriteRecPtr();
+
+	/* calculate oldest segment currently needed by slots */
+	XLByteToSeg(restart_lsn, restartSeg, wal_segment_size);
+	slotPtr = XLogGetReplicationSlotMinimumLSN();
+	oldestSlotSeg = GetOldestKeepSegment(currpos, slotPtr);
+
+	/* find the oldest segment file actually exists */
+	oldestSeg = FindOldestXLogFileSegNo();
+
+	/* calculate oldest segment by max_wal_size */
+	XLByteToSeg(currpos, currSeg, wal_segment_size);
+	keepSegs = ConvertToXSegs(max_wal_size_mb, wal_segment_size) + 1;
+
+	if (currSeg > keepSegs)
+		oldestSegMaxWalSize = currSeg - keepSegs;
+	else
+		oldestSegMaxWalSize = 1;
+
+	/*
+	 * If max_slot_wal_keep_size has changed after the last call, the segment
+	 * that would been kept by the current setting might have been lost by the
+	 * previous setting. No point in showing normal or keeping status values if
+	 * the restartSeg is known to be lost.
+	 */
+	if (restartSeg >= oldestSeg)
+	{
+		/*
+		 * show "normal" when restartSeg is within max_wal_size. If
+		 * max_slot_wal_keep_size is smaller than max_wal_size, there's no
+		 * point in showing the status.
+		 */
+		if ((max_slot_wal_keep_size_mb <= 0 ||
+			 max_slot_wal_keep_size_mb >= max_wal_size_mb) &&
+			oldestSegMaxWalSize <= restartSeg)
+			return WALAVAIL_NORMAL;
+
+		/* being retained by slots */
+		if (oldestSlotSeg <= restartSeg)
+			return WALAVAIL_PRESERVED;
+	}
+	
+	/*
+	 * The segment is alrady lost or being lost. If the oldest segment is just
+	 * after the restartSeg, running walsender may be reading the just removed
+	 * segment. The walsender may safely move to the oldest existing segment in
+	 * that case.
+	 */
+	if (oldestSeg == restartSeg + 1 && walsender_pid != 0)
+		return	WALAVAIL_BEING_REMOVED;
+
+	/* definitely lost. the walsender can no longer restart */
+	return WALAVAIL_REMOVED;
+}
+
 /*
  * Returns minimum segment number that the next checkpoint must leave
  * considering wal_keep_segments, replication slots and
@@ -9370,6 +9518,53 @@ GetOldestKeepSegment(XLogRecPtr currLSN, XLogRecPtr minSlotLSN)
 	return currSeg - keepSegs;
 }
 
+/*
+ * Calculate remaining bytes until WAL segment for targetLSN will be removed.
+ */
+int64
+DistanceToWalRemoval(XLogRecPtr currLSN, XLogRecPtr targetLSN)
+{
+	XLogSegNo	currSeg;
+	uint64		limitSegs = 0;
+	int64 		restbytes;
+	uint64		fragbytes;
+	XLogSegNo	targetSeg;
+
+	XLByteToSeg(currLSN, currSeg, wal_segment_size);
+
+	/* Calculate how far back WAL segments are preserved */
+	if (max_slot_wal_keep_size_mb >= 0)
+		limitSegs = ConvertToXSegs(max_slot_wal_keep_size_mb, wal_segment_size);
+
+	if (wal_keep_segments > 0 && limitSegs < wal_keep_segments)
+		limitSegs = wal_keep_segments;
+
+	XLByteToSeg(targetLSN, targetSeg, wal_segment_size);
+
+	/* avoid underflow */
+	if (targetSeg + limitSegs < currSeg)
+		return 0;
+
+	/*
+	 * This slot still has all required segments. Calculate how
+	 * many LSN bytes the slot has until it loses targetLSN.
+	 */
+	fragbytes = wal_segment_size - (currLSN % wal_segment_size);
+	XLogSegNoOffsetToRecPtr(targetSeg + limitSegs - currSeg,
+							fragbytes, wal_segment_size,
+							restbytes);
+
+	/*
+	 * not realistic, but make sure that it is not out of the
+	 * range of int64. No problem to do so since such large values
+	 * have no significant difference.
+	 */
+	if (restbytes > PG_INT64_MAX)
+		restbytes = PG_INT64_MAX;
+
+	return restbytes;
+}
+
 /*
  * Retreat *logSegNo to the last segment that we need to retain because of
  * either wal_keep_segments or replication slots.
@@ -9381,9 +9576,13 @@ GetOldestKeepSegment(XLogRecPtr currLSN, XLogRecPtr minSlotLSN)
 static void
 KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
 {
+	static XLogSegNo last_lost_segs = 0;
+	static int last_nslots = 0;
+	static char *last_slot_name = NULL;
 	XLogRecPtr	slotminptr = InvalidXLogRecPtr;
 	XLogSegNo	minSegNo;
-	XLogSegNo	slotSegNo;
+	XLogSegNo	minSlotSegNo;
+	int			nslots_affected = 0;
 
 	if (max_replication_slots > 0)
 		slotminptr = XLogGetReplicationSlotMinimumLSN();
@@ -9399,56 +9598,75 @@ KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
 	 */
 	if (!XLogRecPtrIsInvalid(slotminptr))
 	{
-		static XLogSegNo prev_lost_segs = 0;	/* avoid duplicate messages */
+		Assert (max_replication_slots > 0);
 
-		XLByteToSeg(slotminptr, slotSegNo, wal_segment_size);
+		XLByteToSeg(slotminptr, minSlotSegNo, wal_segment_size);
 
-		if (slotSegNo < minSegNo)
+		if (minSlotSegNo < minSegNo)
 		{
-			XLogSegNo lost_segs = minSegNo - slotSegNo;
-			if (prev_lost_segs != lost_segs)
+			/* Some slots has lost requred segments */
+			XLogSegNo	lost_segs = minSegNo - minSlotSegNo;
+			ReplicationSlot *earliest = NULL;
+			char	   *earliest_name = NULL;
+			int			i;
+
+			/* Find the most affected slot */
+			LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
+			for (i = 0 ; i < max_replication_slots ; i++)
 			{
-				/* We have lost a new segment, warn it.*/
-				XLogRecPtr minlsn;
-				static char *prev_slot_names = NULL;
-				char *slot_names;
-				int nslots;
+				ReplicationSlot *s =
+					&ReplicationSlotCtl->replication_slots[i];
+				XLogSegNo slotSegNo;
 
-				XLogSegNoOffsetToRecPtr(minSegNo, 0, wal_segment_size, minlsn);
-				slot_names =
-					ReplicationSlotsEnumerateBehinds(minlsn, ", ", &nslots);
+				XLByteToSeg(s->data.restart_lsn, slotSegNo, wal_segment_size);
 
-				/*
-				 * Some of the affected slots could have just been removed. We
-				 * don't need show anything here if no affected slots are
-				 * remaining.
-				 */
-				if (slot_names)
+				if (s->in_use && s->active_pid == 0 && slotSegNo < minSegNo)
 				{
-					if (prev_slot_names == NULL ||
-						strcmp(slot_names, prev_slot_names) != 0)
-					{
-						MemoryContext oldcxt;
+					nslots_affected++;
 
-						ereport(WARNING,
-								(errmsg ("some replication slots have lost required WAL segments"),
-								 errdetail_plural(
-									 "Slot %s lost %ld segment(s).",
-									 "Slots %s lost at most %ld segment(s).",
-									 nslots, slot_names, lost_segs)));
-
-						if (prev_slot_names)
-							pfree(prev_slot_names);
-						oldcxt = MemoryContextSwitchTo(TopMemoryContext);
-						prev_slot_names = pstrdup(slot_names);
-						MemoryContextSwitchTo(oldcxt);
-					}
+					if (earliest == NULL ||
+						s->data.restart_lsn < earliest->data.restart_lsn)
+						earliest = s;
 				}
 			}
-			prev_lost_segs = lost_segs;
+
+			if (earliest)
+			{
+				MemoryContext oldcxt = MemoryContextSwitchTo(TopMemoryContext);
+				earliest_name = pstrdup(NameStr(earliest->data.name));
+				MemoryContextSwitchTo(oldcxt);
+			}
+
+			LWLockRelease(ReplicationSlotControlLock);
+
+			/* Emit WARNING if something has changed */
+			if (earliest_name &&
+				(last_lost_segs != lost_segs || last_nslots != nslots_affected))
+			{
+				ereport(WARNING,
+						(errmsg_plural ("%d replication slot has lost required WAL segments by %lu segments",
+										"%d replication slots have lost required WAL segments by %lu segments",
+										nslots_affected, nslots_affected,
+										lost_segs),
+						 errdetail("Most affected slot is %s.",
+								   earliest_name)));
+
+				if (last_slot_name)
+					pfree(last_slot_name);
+				last_slot_name = earliest_name;
+				last_lost_segs = lost_segs;
+				last_nslots = nslots_affected;
+			}
 		}
-		else
-			prev_lost_segs = 0;
+	}
+
+	/* Reset the state if no affected slots remain. */
+	if (nslots_affected == 0 && last_slot_name)
+	{
+		pfree(last_slot_name);
+		last_slot_name = NULL;
+		last_lost_segs = 0;
+		last_nslots = 0;
 	}
 
 	/* don't delete WAL segments newer than the calculated segment */
diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql
index c9e75f4370..a3c7373d4f 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -860,7 +860,9 @@ CREATE VIEW pg_replication_slots AS
             L.xmin,
             L.catalog_xmin,
             L.restart_lsn,
-            L.confirmed_flush_lsn
+            L.confirmed_flush_lsn,
+            L.wal_status,
+            L.remain
     FROM pg_get_replication_slots() AS L
             LEFT JOIN pg_database D ON (L.datoid = D.oid);
 
diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c
index fcaede60d0..bba61fd324 100644
--- a/src/backend/replication/slot.c
+++ b/src/backend/replication/slot.c
@@ -1065,70 +1065,6 @@ ReplicationSlotReserveWal(void)
 	}
 }
 
-/*
- * Returns names of inactive replication slots that their restart_lsn are
- * behind specified LSN for the purpose of error message character array
- * stuffed with slot names delimited by the given separator. Returns NULL if no
- * slot matches. If pnslots is given, the number of the returned slots is
- * returned there. The returned array is palloc'ed in TopMemoryContext.
- */
-char *
-ReplicationSlotsEnumerateBehinds(XLogRecPtr target, char *separator, int *pnslots)
-{
-	static StringInfoData retstr;
-	static bool retstr_initialized = false;
-	bool insert_separator = false;
-	int i;
-	int nslots = 0;
-
-	Assert (separator);
-	if (max_replication_slots <= 0)
-		return NULL;
-
-	if (!retstr_initialized)
-	{
-		MemoryContext oldcxt = MemoryContextSwitchTo(TopMemoryContext);
-		initStringInfo(&retstr);
-		MemoryContextSwitchTo(oldcxt);
-		retstr_initialized = true;
-	}
-	else
-		resetStringInfo(&retstr);
-
-	/* construct name list */
-	LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
-	for (i = 0 ; i < max_replication_slots ; i++)
-	{
-		ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i];
-
-		/*
-		 * We are collecting slots that are definitely behind the given target
-		 * LSN. Active slots are exluded since they can catch up later.
-		 */
-		if (s->in_use && s->active_pid == 0 && s->data.restart_lsn < target)
-		{
-			if (insert_separator)
-				appendStringInfoString(&retstr, separator);
-
-			/*
-			 * Slot names consist only with lower-case letters. We don't
-			 * bother quoting.
-			 */
-			appendStringInfoString(&retstr, NameStr(s->data.name));
-			insert_separator = true;
-			nslots++;
-		}
-	}
-	LWLockRelease(ReplicationSlotControlLock);
-
-	/* return the number of slots in the list if requested */
-	if (pnslots)
-		*pnslots = nslots;
-
-	/* return NULL instead of an empty string */
-	return retstr.data[0] ? retstr.data : NULL;
-}
-
 /*
  * Flush all replication slots to disk.
  *
diff --git a/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c
index bb69683e2a..83533ea6c2 100644
--- a/src/backend/replication/slotfuncs.c
+++ b/src/backend/replication/slotfuncs.c
@@ -221,7 +221,7 @@ pg_drop_replication_slot(PG_FUNCTION_ARGS)
 Datum
 pg_get_replication_slots(PG_FUNCTION_ARGS)
 {
-#define PG_GET_REPLICATION_SLOTS_COLS 11
+#define PG_GET_REPLICATION_SLOTS_COLS 13
 	ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
 	TupleDesc	tupdesc;
 	Tuplestorestate *tupstore;
@@ -275,6 +275,7 @@ pg_get_replication_slots(PG_FUNCTION_ARGS)
 		Oid			database;
 		NameData	slot_name;
 		NameData	plugin;
+		WalAvailability walstate;
 		int			i;
 
 		if (!slot->in_use)
@@ -342,6 +343,42 @@ pg_get_replication_slots(PG_FUNCTION_ARGS)
 		else
 			nulls[i++] = true;
 
+		walstate = GetWalAvailability(restart_lsn, active_pid);
+
+		switch (walstate)
+		{
+			case WALAVAIL_INVALID_LSN:
+				nulls[i++] = true;
+				break;
+
+			case WALAVAIL_NORMAL:
+				values[i++] = CStringGetTextDatum("normal");
+				break;
+
+			case WALAVAIL_PRESERVED:
+				values[i++] = CStringGetTextDatum("keeping");
+				break;
+
+			case WALAVAIL_BEING_REMOVED:
+				values[i++] = CStringGetTextDatum("losing");
+				break;
+
+			case WALAVAIL_REMOVED:
+				values[i++] = CStringGetTextDatum("lost");
+				break;
+		}
+
+		if (max_slot_wal_keep_size_mb >=0 &&
+			(walstate == WALAVAIL_NORMAL ||
+			 walstate == WALAVAIL_PRESERVED))
+		{
+			XLogRecPtr currptr = GetXLogWriteRecPtr();
+			values[i++] =
+				Int64GetDatum(DistanceToWalRemoval(currptr, restart_lsn));
+		}
+		else
+			nulls[i++] = true;
+
 		tuplestore_putvalues(tupstore, tupdesc, values, nulls);
 	}
 	LWLockRelease(ReplicationSlotControlLock);
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index 5d117d5cfc..52ff676638 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -255,6 +255,20 @@ typedef struct CheckpointStatsData
 
 extern CheckpointStatsData CheckpointStats;
 
+/*
+ * WAL segment availability status
+ *
+ * This is used as the return value of GetWalAvailability.
+ */
+typedef enum WalAvailability
+{
+	WALAVAIL_INVALID_LSN,			/* parameter errror */
+	WALAVAIL_NORMAL,				/* WAL segment is within max_wal_size */
+	WALAVAIL_PRESERVED,				/* WAL segment is preserved by repslots */
+	WALAVAIL_BEING_REMOVED,			/* WAL segment is no longer preserved */
+	WALAVAIL_REMOVED				/* WAL segment has been removed */
+} WalAvailability;
+
 struct XLogRecData;
 
 extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata,
@@ -268,6 +282,7 @@ extern int	XLogFileOpen(XLogSegNo segno);
 
 extern void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli);
 extern XLogSegNo XLogGetLastRemovedSegno(void);
+extern XLogSegNo FindOldestXLogFileSegNo(void);
 extern void XLogSetAsyncXactLSN(XLogRecPtr record);
 extern void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn);
 
@@ -304,6 +319,9 @@ extern void ShutdownXLOG(int code, Datum arg);
 extern void InitXLOGAccess(void);
 extern void CreateCheckPoint(int flags);
 extern bool CreateRestartPoint(int flags);
+extern WalAvailability GetWalAvailability(XLogRecPtr restart_lsn,
+										  pid_t walsender_pid);
+extern int64 DistanceToWalRemoval(XLogRecPtr currLSN, XLogRecPtr targetLSN);
 extern void XLogPutNextOid(Oid nextOid);
 extern XLogRecPtr XLogRestorePoint(const char *rpName);
 extern void UpdateFullPageWrites(void);
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index fcf2a1214c..e70e62a657 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -9892,9 +9892,9 @@
   proname => 'pg_get_replication_slots', prorows => '10', proisstrict => 'f',
   proretset => 't', provolatile => 's', prorettype => 'record',
   proargtypes => '',
-  proallargtypes => '{name,name,text,oid,bool,bool,int4,xid,xid,pg_lsn,pg_lsn}',
-  proargmodes => '{o,o,o,o,o,o,o,o,o,o,o}',
-  proargnames => '{slot_name,plugin,slot_type,datoid,temporary,active,active_pid,xmin,catalog_xmin,restart_lsn,confirmed_flush_lsn}',
+  proallargtypes => '{name,name,text,oid,bool,bool,int4,xid,xid,pg_lsn,pg_lsn,text,int8}',
+  proargmodes => '{o,o,o,o,o,o,o,o,o,o,o,o,o}',
+  proargnames => '{slot_name,plugin,slot_type,datoid,temporary,active,active_pid,xmin,catalog_xmin,restart_lsn,confirmed_flush_lsn,wal_status,remain}',
   prosrc => 'pg_get_replication_slots' },
 { oid => '3786', descr => 'set up a logical replication slot',
   proname => 'pg_create_logical_replication_slot', provolatile => 'v',
diff --git a/src/include/replication/slot.h b/src/include/replication/slot.h
index 09b0ab7953..3e95b019b3 100644
--- a/src/include/replication/slot.h
+++ b/src/include/replication/slot.h
@@ -198,7 +198,6 @@ extern void ReplicationSlotsComputeRequiredLSN(void);
 extern XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void);
 extern bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive);
 extern void ReplicationSlotsDropDBSlots(Oid dboid);
-extern char *ReplicationSlotsEnumerateBehinds(XLogRecPtr target, char *separator, int *pnslots);
 
 extern void StartupReplicationSlots(void);
 extern void CheckPointReplicationSlots(void);
diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out
index 70e1e2f78d..4dec2b1c3d 100644
--- a/src/test/regress/expected/rules.out
+++ b/src/test/regress/expected/rules.out
@@ -1461,8 +1461,10 @@ pg_replication_slots| SELECT l.slot_name,
     l.xmin,
     l.catalog_xmin,
     l.restart_lsn,
-    l.confirmed_flush_lsn
-   FROM (pg_get_replication_slots() l(slot_name, plugin, slot_type, datoid, temporary, active, active_pid, xmin, catalog_xmin, restart_lsn, confirmed_flush_lsn)
+    l.confirmed_flush_lsn,
+    l.wal_status,
+    l.remain
+   FROM (pg_get_replication_slots() l(slot_name, plugin, slot_type, datoid, temporary, active, active_pid, xmin, catalog_xmin, restart_lsn, confirmed_flush_lsn, wal_status, remain)
      LEFT JOIN pg_database d ON ((l.datoid = d.oid)));
 pg_roles| SELECT pg_authid.rolname,
     pg_authid.rolsuper,
-- 
2.18.2

