From c6de2b764d5010cff35a53e9c7b000d0d20a59dc Mon Sep 17 00:00:00 2001
From: Zhao Junwang <zhjwpku@gmail.com>
Date: Thu, 9 Nov 2023 15:03:38 +0800
Subject: [PATCH v3] PITR shutdown should not report error by pg_ctl

After a PITR recovery, the dbstate in pg_control is
DB_SHUTDOWNED_IN_RECOVERY, check this and do not report error
in pg_ctl.

Signed-off-by: Zhao Junwang <zhjwpku@gmail.com>
---
 src/bin/pg_ctl/pg_ctl.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index 3b145bd838..349f7e5abe 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -45,6 +45,7 @@ typedef enum
 {
 	POSTMASTER_READY,
 	POSTMASTER_STILL_STARTING,
+	POSTMASTER_RECOVERY_SHUTDOWN,
 	POSTMASTER_FAILED,
 } WaitPMResult;
 
@@ -662,11 +663,21 @@ wait_for_postmaster_start(pid_t pm_pid, bool do_checkpoint)
 			int			exitstatus;
 
 			if (waitpid(pm_pid, &exitstatus, WNOHANG) == pm_pid)
-				return POSTMASTER_FAILED;
+			{
+				if (get_control_dbstate() == DB_SHUTDOWNED_IN_RECOVERY)
+					return POSTMASTER_RECOVERY_SHUTDOWN;
+				else
+					return POSTMASTER_FAILED;
+			}
 		}
 #else
 		if (WaitForSingleObject(postmasterProcess, 0) == WAIT_OBJECT_0)
-			return POSTMASTER_FAILED;
+		{
+			if (get_control_dbstate() == DB_SHUTDOWNED_IN_RECOVERY)
+				return POSTMASTER_RECOVERY_SHUTDOWN;
+			else
+				return POSTMASTER_FAILED;
+		}
 #endif
 
 		/* Startup still in process; wait, printing a dot once per second */
@@ -991,6 +1002,10 @@ do_start(void)
 							 progname);
 				exit(1);
 				break;
+			case POSTMASTER_RECOVERY_SHUTDOWN:
+				print_msg(_("PITR shutdown\n"));
+				print_msg(_("update configuration for startup again if needed\n"));
+				break;
 			case POSTMASTER_FAILED:
 				print_msg(_(" stopped waiting\n"));
 				write_stderr(_("%s: could not start server\n"
-- 
2.41.0

