From fdfa74d754cb0f91f047699e77480738387f2a42 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Fri, 16 Feb 2024 07:34:41 +0000
Subject: [PATCH v23 06/13] Fix cleanup functions

---
 src/bin/pg_basebackup/pg_createsubscriber.c | 60 +++------------------
 1 file changed, 8 insertions(+), 52 deletions(-)

diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c
index 968d0ae6bd..252d541472 100644
--- a/src/bin/pg_basebackup/pg_createsubscriber.c
+++ b/src/bin/pg_basebackup/pg_createsubscriber.c
@@ -54,7 +54,6 @@ typedef struct LogicalRepInfo
 
 	bool		made_replslot;	/* replication slot was created */
 	bool		made_publication;	/* publication was created */
-	bool		made_subscription;	/* subscription was created */
 } LogicalRepInfo;
 
 static void cleanup_objects_atexit(void);
@@ -95,7 +94,6 @@ static void wait_for_end_recovery(const char *conninfo, const char *pg_ctl_path,
 static void create_publication(PGconn *conn, LogicalRepInfo *dbinfo);
 static void drop_publication(PGconn *conn, LogicalRepInfo *dbinfo);
 static void create_subscription(PGconn *conn, LogicalRepInfo *dbinfo);
-static void drop_subscription(PGconn *conn, LogicalRepInfo *dbinfo);
 static void set_replication_progress(PGconn *conn, LogicalRepInfo *dbinfo,
 									 const char *lsn);
 static void enable_subscription(PGconn *conn, LogicalRepInfo *dbinfo);
@@ -141,22 +139,11 @@ cleanup_objects_atexit(void)
 
 	for (i = 0; i < num_dbs; i++)
 	{
-		if (dbinfo[i].made_subscription || recovery_ended)
+		if (recovery_ended)
 		{
-			conn = connect_database(dbinfo[i].subconninfo);
-			if (conn != NULL)
-			{
-				if (dbinfo[i].made_subscription)
-					drop_subscription(conn, &dbinfo[i]);
-
-				/*
-				 * Publications are created on publisher before promotion so
-				 * it might exist on subscriber after recovery ends.
-				 */
-				if (recovery_ended)
-					drop_publication(conn, &dbinfo[i]);
-				disconnect_database(conn);
-			}
+			pg_log_warning("pg_createsubscriber failed after the end of recovery");
+			pg_log_warning("Target server could not be usable as physical standby anymore.");
+			pg_log_warning_hint("You must re-create the physical standby again.");
 		}
 
 		if (dbinfo[i].made_publication || dbinfo[i].made_replslot)
@@ -404,7 +391,6 @@ store_pub_sub_info(SimpleStringList dbnames, const char *pub_base_conninfo,
 		/* Fill subscriber attributes */
 		conninfo = concat_conninfo_dbname(sub_base_conninfo, cell->val);
 		dbinfo[i].subconninfo = conninfo;
-		dbinfo[i].made_subscription = false;
 		/* Other fields will be filled later */
 
 		i++;
@@ -1430,46 +1416,12 @@ create_subscription(PGconn *conn, LogicalRepInfo *dbinfo)
 		}
 	}
 
-	/* for cleanup purposes */
-	dbinfo->made_subscription = true;
-
 	if (!dry_run)
 		PQclear(res);
 
 	destroyPQExpBuffer(str);
 }
 
-/*
- * Remove subscription if it couldn't finish all steps.
- */
-static void
-drop_subscription(PGconn *conn, LogicalRepInfo *dbinfo)
-{
-	PQExpBuffer str = createPQExpBuffer();
-	PGresult   *res;
-
-	Assert(conn != NULL);
-
-	pg_log_info("dropping subscription \"%s\" on database \"%s\"",
-				dbinfo->subname, dbinfo->dbname);
-
-	appendPQExpBuffer(str, "DROP SUBSCRIPTION %s", dbinfo->subname);
-
-	pg_log_debug("command is: %s", str->data);
-
-	if (!dry_run)
-	{
-		res = PQexec(conn, str->data);
-		if (PQresultStatus(res) != PGRES_COMMAND_OK)
-			pg_log_error("could not drop subscription \"%s\" on database \"%s\": %s",
-						 dbinfo->subname, dbinfo->dbname, PQresultErrorMessage(res));
-
-		PQclear(res);
-	}
-
-	destroyPQExpBuffer(str);
-}
-
 /*
  * Sets the replication progress to the consistent LSN.
  *
@@ -1986,6 +1938,10 @@ main(int argc, char **argv)
 	/* Waiting the subscriber to be promoted */
 	wait_for_end_recovery(dbinfo[0].subconninfo, pg_ctl_path, &opt);
 
+	pg_log_info("target server reached the consistent state");
+	pg_log_info_hint("If pg_createsubscriber fails after this point, "
+					 "you must re-create the new physical standby before continuing.");
+
 	/*
 	 * Create the subscription for each database on subscriber. It does not
 	 * enable it immediately because it needs to adjust the logical
-- 
2.41.0.windows.3

