From e6f05633e258e214db47c82ea853c1dcf0307153 Mon Sep 17 00:00:00 2001
From: Alexander Korotkov <akorotkov@postgresql.org>
Date: Thu, 19 Sep 2024 14:48:44 +0300
Subject: [PATCH v2 1/2] Refactor WaitForLSNReplay() to return the result of
 waiting

Currently WaitForLSNReplay() immediatly throws an error if waiting for LSN
replay is not successful.  This commit teaches  WaitForLSNReplay() to return
the result of waiting, while making pg_wal_replay_wait() responsible for
throwing an appropriate error.

This is preparation for new procedure pg_wal_replay_wait_status(), which will
expose the waiting result to the user instead of throwing an error.
---
 src/backend/access/transam/xlogfuncs.c | 38 +++++++++++++++++++++++++-
 src/backend/commands/waitlsn.c         | 30 ++++++--------------
 src/include/commands/waitlsn.h         | 13 ++++++++-
 3 files changed, 58 insertions(+), 23 deletions(-)

diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c
index 3e3d2bb6189..74b493e437e 100644
--- a/src/backend/access/transam/xlogfuncs.c
+++ b/src/backend/access/transam/xlogfuncs.c
@@ -759,6 +759,7 @@ pg_wal_replay_wait(PG_FUNCTION_ARGS)
 {
 	XLogRecPtr	target_lsn = PG_GETARG_LSN(0);
 	int64		timeout = PG_GETARG_INT64(1);
+	WaitLSNResult result;
 
 	if (timeout < 0)
 		ereport(ERROR,
@@ -799,7 +800,42 @@ pg_wal_replay_wait(PG_FUNCTION_ARGS)
 	 */
 	Assert(MyProc->xmin == InvalidTransactionId);
 
-	(void) WaitForLSNReplay(target_lsn, timeout);
+	result = WaitForLSNReplay(target_lsn, timeout);
+
+	/*
+	 * Process the result of WaitForLSNReplay().  Throw appropriate error if
+	 * needed.
+	 */
+	switch (result)
+	{
+		case WaitLSNResultSuccess:
+			/* Nothing to do on success */
+			break;
+
+		case WaitLSNResultTimeout:
+			ereport(ERROR,
+					(errcode(ERRCODE_QUERY_CANCELED),
+					 errmsg("timed out while waiting for target LSN %X/%X to be replayed; current replay LSN %X/%X",
+							LSN_FORMAT_ARGS(target_lsn),
+							LSN_FORMAT_ARGS(GetXLogReplayRecPtr(NULL)))));
+			break;
+
+		case WaitLSNResultNotInRecovery:
+			ereport(ERROR,
+					(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+					 errmsg("recovery is not in progress"),
+					 errhint("Waiting for LSN can only be executed during recovery.")));
+			break;
+
+		case WaitLSNResultPromotedConcurrently:
+			ereport(ERROR,
+					(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+					 errmsg("recovery is not in progress"),
+					 errdetail("Recovery ended before replaying target LSN %X/%X; last replay LSN %X/%X.",
+							   LSN_FORMAT_ARGS(target_lsn),
+							   LSN_FORMAT_ARGS(GetXLogReplayRecPtr(NULL)))));
+			break;
+	}
 
 	PG_RETURN_VOID();
 }
diff --git a/src/backend/commands/waitlsn.c b/src/backend/commands/waitlsn.c
index 501938f4330..aab6aa18648 100644
--- a/src/backend/commands/waitlsn.c
+++ b/src/backend/commands/waitlsn.c
@@ -217,7 +217,7 @@ WaitLSNCleanup(void)
  * Wait using MyLatch till the given LSN is replayed, the postmaster dies or
  * timeout happens.
  */
-void
+WaitLSNResult
 WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
 {
 	XLogRecPtr	currentLSN;
@@ -240,17 +240,14 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
 		 * check the last replay LSN before reporting an error.
 		 */
 		if (targetLSN <= GetXLogReplayRecPtr(NULL))
-			return;
-		ereport(ERROR,
-				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-				 errmsg("recovery is not in progress"),
-				 errhint("Waiting for LSN can only be executed during recovery.")));
+			return WaitLSNResultSuccess;
+		return WaitLSNResultNotInRecovery;
 	}
 	else
 	{
 		/* If target LSN is already replayed, exit immediately */
 		if (targetLSN <= GetXLogReplayRecPtr(NULL))
-			return;
+			return WaitLSNResultSuccess;
 	}
 
 	if (timeout > 0)
@@ -280,13 +277,8 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
 			 */
 			currentLSN = GetXLogReplayRecPtr(NULL);
 			if (targetLSN <= currentLSN)
-				return;
-			ereport(ERROR,
-					(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					 errmsg("recovery is not in progress"),
-					 errdetail("Recovery ended before replaying target LSN %X/%X; last replay LSN %X/%X.",
-							   LSN_FORMAT_ARGS(targetLSN),
-							   LSN_FORMAT_ARGS(currentLSN))));
+				return WaitLSNResultSuccess;
+			return WaitLSNResultPromotedConcurrently;
 		}
 		else
 		{
@@ -328,11 +320,7 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
 	 * If we didn't reach the target LSN, we must be exited by timeout.
 	 */
 	if (targetLSN > currentLSN)
-	{
-		ereport(ERROR,
-				(errcode(ERRCODE_QUERY_CANCELED),
-				 errmsg("timed out while waiting for target LSN %X/%X to be replayed; current replay LSN %X/%X",
-						LSN_FORMAT_ARGS(targetLSN),
-						LSN_FORMAT_ARGS(currentLSN))));
-	}
+		return WaitLSNResultTimeout;
+
+	return WaitLSNResultSuccess;
 }
diff --git a/src/include/commands/waitlsn.h b/src/include/commands/waitlsn.h
index bb5ac858dcc..36a580674c0 100644
--- a/src/include/commands/waitlsn.h
+++ b/src/include/commands/waitlsn.h
@@ -70,12 +70,23 @@ typedef struct WaitLSNState
 	WaitLSNProcInfo procInfos[FLEXIBLE_ARRAY_MEMBER];
 } WaitLSNState;
 
+/*
+ * Results statuses for WaitForLSNReplay().
+ */
+typedef enum
+{
+	WaitLSNResultSuccess, /* Target LSN is reached */
+	WaitLSNResultTimeout, /* Timeout occured */
+	WaitLSNResultNotInRecovery, /* Recovery ended before we start waiting */
+	WaitLSNResultPromotedConcurrently, /* Standby got promoted during waiting and before target LSN is reached */
+} WaitLSNResult;
+
 extern PGDLLIMPORT WaitLSNState *waitLSNState;
 
 extern Size WaitLSNShmemSize(void);
 extern void WaitLSNShmemInit(void);
 extern void WaitLSNSetLatches(XLogRecPtr currentLSN);
 extern void WaitLSNCleanup(void);
-extern void WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout);
+extern WaitLSNResult WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout);
 
 #endif							/* WAIT_LSN_H */
-- 
2.39.5 (Apple Git-154)

