From f6de6da085a6d197916c1b2ca0674cc6257d8386 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Mon, 19 Feb 2024 04:42:17 +0000
Subject: [PATCH v23 10/13] Make the ERROR handling more consistent

---
 src/bin/pg_basebackup/pg_createsubscriber.c | 38 +++------------------
 1 file changed, 5 insertions(+), 33 deletions(-)

diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c
index e88b29ea3e..f5ccd479b6 100644
--- a/src/bin/pg_basebackup/pg_createsubscriber.c
+++ b/src/bin/pg_basebackup/pg_createsubscriber.c
@@ -453,18 +453,12 @@ get_primary_sysid(const char *conninfo)
 
 	res = PQexec(conn, "SELECT system_identifier FROM pg_control_system()");
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
-	{
-		disconnect_database(conn);
 		pg_fatal("could not get system identifier: %s",
 				 PQresultErrorMessage(res));
-	}
+
 	if (PQntuples(res) != 1)
-	{
-		PQclear(res);
-		disconnect_database(conn);
 		pg_fatal("could not get system identifier: got %d rows, expected %d row",
 				 PQntuples(res), 1);
-	}
 
 	sysid = strtou64(PQgetvalue(res, 0, 0), NULL, 10);
 
@@ -775,8 +769,6 @@ check_publisher(LogicalRepInfo *dbinfo)
 		{
 			pg_log_error("could not obtain replication slot information: got %d rows, expected %d row",
 						 PQntuples(res), 1);
-			pg_free(primary_slot_name); /* it is not being used. */
-			primary_slot_name = NULL;
 			return false;
 		}
 		else
@@ -1269,11 +1261,8 @@ create_publication(PGconn *conn, LogicalRepInfo *dbinfo)
 					  dbinfo->pubname);
 	res = PQexec(conn, str->data);
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
-	{
-		PQfinish(conn);
 		pg_fatal("could not obtain publication information: %s",
 				 PQresultErrorMessage(res));
-	}
 
 	if (atoi(PQgetvalue(res, 0, 0)) == 1)
 	{
@@ -1286,8 +1275,6 @@ create_publication(PGconn *conn, LogicalRepInfo *dbinfo)
 		 */
 		pg_log_error("publication \"%s\" already exists", dbinfo->pubname);
 		pg_log_error_hint("Consider renaming this publication.");
-		PQclear(res);
-		PQfinish(conn);
 		exit(1);
 	}
 
@@ -1305,12 +1292,10 @@ create_publication(PGconn *conn, LogicalRepInfo *dbinfo)
 	if (!dry_run)
 	{
 		res = PQexec(conn, str->data);
+
 		if (PQresultStatus(res) != PGRES_COMMAND_OK)
-		{
-			PQfinish(conn);
 			pg_fatal("could not create publication \"%s\" on database \"%s\": %s",
 					 dbinfo->pubname, dbinfo->dbname, PQresultErrorMessage(res));
-		}
 	}
 
 	/* for cleanup purposes */
@@ -1386,12 +1371,10 @@ create_subscription(PGconn *conn, LogicalRepInfo *dbinfo)
 	if (!dry_run)
 	{
 		res = PQexec(conn, str->data);
+
 		if (PQresultStatus(res) != PGRES_COMMAND_OK)
-		{
-			PQfinish(conn);
 			pg_fatal("could not create subscription \"%s\" on database \"%s\": %s",
 					 dbinfo->subname, dbinfo->dbname, PQresultErrorMessage(res));
-		}
 	}
 
 	if (!dry_run)
@@ -1428,19 +1411,12 @@ set_replication_progress(PGconn *conn, LogicalRepInfo *dbinfo, const char *lsn)
 
 	res = PQexec(conn, str->data);
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
-	{
-		PQfinish(conn);
 		pg_fatal("could not obtain subscription OID: %s",
 				 PQresultErrorMessage(res));
-	}
 
 	if (PQntuples(res) != 1 && !dry_run)
-	{
-		PQclear(res);
-		PQfinish(conn);
 		pg_fatal("could not obtain subscription OID: got %d rows, expected %d rows",
 				 PQntuples(res), 1);
-	}
 
 	if (dry_run)
 	{
@@ -1475,12 +1451,10 @@ set_replication_progress(PGconn *conn, LogicalRepInfo *dbinfo, const char *lsn)
 	if (!dry_run)
 	{
 		res = PQexec(conn, str->data);
+
 		if (PQresultStatus(res) != PGRES_TUPLES_OK)
-		{
-			PQfinish(conn);
 			pg_fatal("could not set replication progress for the subscription \"%s\": %s",
 					 dbinfo->subname, PQresultErrorMessage(res));
-		}
 
 		PQclear(res);
 	}
@@ -1513,12 +1487,10 @@ enable_subscription(PGconn *conn, LogicalRepInfo *dbinfo)
 	if (!dry_run)
 	{
 		res = PQexec(conn, str->data);
+
 		if (PQresultStatus(res) != PGRES_COMMAND_OK)
-		{
-			PQfinish(conn);
 			pg_fatal("could not enable subscription \"%s\": %s",
 					 dbinfo->subname, PQresultErrorMessage(res));
-		}
 
 		PQclear(res);
 	}
-- 
2.41.0.windows.3

