From e286505af6547077a276555cf3f98d84008ef97c Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Mon, 19 Feb 2024 03:59:19 +0000
Subject: [PATCH v22 05/11] Fix some trivial issues

---
 src/bin/pg_basebackup/pg_createsubscriber.c | 44 ++++++++++-----------
 1 file changed, 20 insertions(+), 24 deletions(-)

diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c
index 1ad7de9190..968d0ae6bd 100644
--- a/src/bin/pg_basebackup/pg_createsubscriber.c
+++ b/src/bin/pg_basebackup/pg_createsubscriber.c
@@ -387,12 +387,11 @@ store_pub_sub_info(SimpleStringList dbnames, const char *pub_base_conninfo,
 				   const char *sub_base_conninfo)
 {
 	LogicalRepInfo *dbinfo;
-	SimpleStringListCell *cell;
 	int			i = 0;
 
 	dbinfo = (LogicalRepInfo *) pg_malloc(num_dbs * sizeof(LogicalRepInfo));
 
-	for (cell = dbnames.head; cell; cell = cell->next)
+	for (SimpleStringListCell *cell = dbnames.head; cell; cell = cell->next)
 	{
 		char	   *conninfo;
 
@@ -469,7 +468,6 @@ get_primary_sysid(const char *conninfo)
 	res = PQexec(conn, "SELECT system_identifier FROM pg_control_system()");
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
 	{
-		PQclear(res);
 		disconnect_database(conn);
 		pg_fatal("could not get system identifier: %s",
 				 PQresultErrorMessage(res));
@@ -516,7 +514,7 @@ get_standby_sysid(const char *datadir)
 	pg_log_info("system identifier is %llu on subscriber",
 				(unsigned long long) sysid);
 
-	pfree(cf);
+	pg_free(cf);
 
 	return sysid;
 }
@@ -534,7 +532,6 @@ modify_subscriber_sysid(const char *pg_resetwal_path, CreateSubscriberOptions *o
 	struct timeval tv;
 
 	char	   *cmd_str;
-	int			rc;
 
 	pg_log_info("modifying system identifier from subscriber");
 
@@ -567,14 +564,15 @@ modify_subscriber_sysid(const char *pg_resetwal_path, CreateSubscriberOptions *o
 
 	if (!dry_run)
 	{
-		rc = system(cmd_str);
+		int rc = system(cmd_str);
+
 		if (rc == 0)
 			pg_log_info("subscriber successfully changed the system identifier");
 		else
 			pg_fatal("subscriber failed to change system identifier: exit code: %d", rc);
 	}
 
-	pfree(cf);
+	pg_free(cf);
 }
 
 /*
@@ -584,11 +582,11 @@ modify_subscriber_sysid(const char *pg_resetwal_path, CreateSubscriberOptions *o
 static bool
 setup_publisher(LogicalRepInfo *dbinfo)
 {
-	PGconn	   *conn;
-	PGresult   *res;
 
 	for (int i = 0; i < num_dbs; i++)
 	{
+		PGconn	   *conn;
+		PGresult   *res;
 		char		pubname[NAMEDATALEN];
 		char		replslotname[NAMEDATALEN];
 
@@ -901,7 +899,7 @@ check_subscriber(LogicalRepInfo *dbinfo)
 		pg_log_error("permission denied for database %s", dbinfo[0].dbname);
 		return false;
 	}
-	if (strcmp(PQgetvalue(res, 0, 1), "t") != 0)
+	if (strcmp(PQgetvalue(res, 0, 2), "t") != 0)
 	{
 		pg_log_error("permission denied for function \"%s\"",
 					 "pg_catalog.pg_replication_origin_advance(text, pg_lsn)");
@@ -990,10 +988,10 @@ check_subscriber(LogicalRepInfo *dbinfo)
 static bool
 setup_subscriber(LogicalRepInfo *dbinfo, const char *consistent_lsn)
 {
-	PGconn	   *conn;
-
 	for (int i = 0; i < num_dbs; i++)
 	{
+		PGconn	   *conn;
+
 		/* Connect to subscriber. */
 		conn = connect_database(dbinfo[i].subconninfo);
 		if (conn == NULL)
@@ -1103,7 +1101,7 @@ drop_replication_slot(PGconn *conn, LogicalRepInfo *dbinfo,
 		res = PQexec(conn, str->data);
 		if (PQresultStatus(res) != PGRES_TUPLES_OK)
 			pg_log_error("could not drop replication slot \"%s\" on database \"%s\": %s",
-						 slot_name, dbinfo->dbname, PQerrorMessage(conn));
+						 slot_name, dbinfo->dbname, PQresultErrorMessage(res));
 
 		PQclear(res);
 	}
@@ -1294,7 +1292,6 @@ create_publication(PGconn *conn, LogicalRepInfo *dbinfo)
 	res = PQexec(conn, str->data);
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
 	{
-		PQclear(res);
 		PQfinish(conn);
 		pg_fatal("could not obtain publication information: %s",
 				 PQresultErrorMessage(res));
@@ -1348,7 +1345,7 @@ create_publication(PGconn *conn, LogicalRepInfo *dbinfo)
 		{
 			PQfinish(conn);
 			pg_fatal("could not create publication \"%s\" on database \"%s\": %s",
-					 dbinfo->pubname, dbinfo->dbname, PQerrorMessage(conn));
+					 dbinfo->pubname, dbinfo->dbname, PQresultErrorMessage(res));
 		}
 	}
 
@@ -1384,7 +1381,7 @@ drop_publication(PGconn *conn, LogicalRepInfo *dbinfo)
 		res = PQexec(conn, str->data);
 		if (PQresultStatus(res) != PGRES_COMMAND_OK)
 			pg_log_error("could not drop publication \"%s\" on database \"%s\": %s",
-						 dbinfo->pubname, dbinfo->dbname, PQerrorMessage(conn));
+						 dbinfo->pubname, dbinfo->dbname, PQresultErrorMessage(res));
 
 		PQclear(res);
 	}
@@ -1429,7 +1426,7 @@ create_subscription(PGconn *conn, LogicalRepInfo *dbinfo)
 		{
 			PQfinish(conn);
 			pg_fatal("could not create subscription \"%s\" on database \"%s\": %s",
-					 dbinfo->subname, dbinfo->dbname, PQerrorMessage(conn));
+					 dbinfo->subname, dbinfo->dbname, PQresultErrorMessage(res));
 		}
 	}
 
@@ -1465,7 +1462,7 @@ drop_subscription(PGconn *conn, LogicalRepInfo *dbinfo)
 		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, PQerrorMessage(conn));
+						 dbinfo->subname, dbinfo->dbname, PQresultErrorMessage(res));
 
 		PQclear(res);
 	}
@@ -1502,7 +1499,6 @@ set_replication_progress(PGconn *conn, LogicalRepInfo *dbinfo, const char *lsn)
 	res = PQexec(conn, str->data);
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
 	{
-		PQclear(res);
 		PQfinish(conn);
 		pg_fatal("could not obtain subscription OID: %s",
 				 PQresultErrorMessage(res));
@@ -1591,7 +1587,7 @@ enable_subscription(PGconn *conn, LogicalRepInfo *dbinfo)
 		{
 			PQfinish(conn);
 			pg_fatal("could not enable subscription \"%s\": %s",
-					 dbinfo->subname, PQerrorMessage(conn));
+					 dbinfo->subname, PQresultErrorMessage(res));
 		}
 
 		PQclear(res);
@@ -1745,11 +1741,11 @@ main(int argc, char **argv)
 					pg_fatal("invalid old port number");
 				break;
 			case 'U':
-				pfree(opt.subuser);
+				pg_free(opt.subuser);
 				opt.subuser = pg_strdup(optarg);
 				break;
 			case 's':
-				pfree(opt.socketdir);
+				pg_free(opt.socketdir);
 				opt.socketdir = pg_strdup(optarg);
 				break;
 			case 'd':
@@ -1854,7 +1850,7 @@ main(int argc, char **argv)
 	pg_ctl_path = get_exec_path(argv[0], "pg_ctl");
 	pg_resetwal_path = get_exec_path(argv[0], "pg_resetwal");
 
-	/* rudimentary check for a data directory. */
+	/* Rudimentary check for a data directory */
 	if (!check_data_directory(opt.subscriber_dir))
 		exit(1);
 
@@ -1877,7 +1873,7 @@ main(int argc, char **argv)
 	/* Create the output directory to store any data generated by this tool */
 	server_start_log = setup_server_logfile(opt.subscriber_dir);
 
-	/* subscriber PID file. */
+	/* Subscriber PID file */
 	snprintf(pidfile, MAXPGPATH, "%s/postmaster.pid", opt.subscriber_dir);
 
 	/*
-- 
2.43.0

