From b974cba7919678a8c11d2f1f89ee0f2d82f0b4e9 Mon Sep 17 00:00:00 2001
From: Euler Taveira <euler@eulerto.com>
Date: Sat, 16 Mar 2024 11:58:11 -0300
Subject: [PATCH v30 2/3] Stop the target server earlier

Since the recovery process requires that it reaches a consistent state
before considering the recovery stop point, stop the server before
creating the replication slots since the last replication slot is its
recovery stop point.
---
 src/bin/pg_basebackup/pg_createsubscriber.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c
index 6cc1c34121..34ec7c8505 100644
--- a/src/bin/pg_basebackup/pg_createsubscriber.c
+++ b/src/bin/pg_basebackup/pg_createsubscriber.c
@@ -2074,6 +2074,16 @@ main(int argc, char **argv)
 	 */
 	check_publisher(dbinfo);
 
+	/*
+	 * Stop the target server. The recovery process requires that the server
+	 * reaches a consistent state before targeting the recovery stop point.
+	 * Make sure a consistent state is reached (stop the target server
+	 * guarantees it) *before* creating the replication slots in
+	 * setup_publisher().
+	 */
+	pg_log_info("stopping the subscriber");
+	stop_standby_server(subscriber_dir);
+
 	/*
 	 * Create the required objects for each database on publisher. This step
 	 * is here mainly because if we stop the standby we cannot verify if the
@@ -2086,11 +2096,10 @@ main(int argc, char **argv)
 	setup_recovery(dbinfo, subscriber_dir, consistent_lsn);
 
 	/*
-	 * Restart subscriber so the recovery parameters will take effect. Wait
+	 * Start subscriber so the recovery parameters will take effect. Wait
 	 * until accepting connections.
 	 */
-	pg_log_info("stopping and starting the subscriber");
-	stop_standby_server(subscriber_dir);
+	pg_log_info("starting the subscriber");
 	start_standby_server(&opt, true);
 
 	/* Waiting the subscriber to be promoted */
-- 
2.34.1

