From ca9d6604283b53cf1cabcc64a1a858df1e14e36e Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Mon, 30 Aug 2021 05:26:54 +0000
Subject: [PATCH v1] replace IDENTIFY_SYSTEM code in receivelog.c with
 RunIdentifySystem()

Couple of improvements to receivelog.c and pg_receivewal.c:
1) ReceiveXlogStream in receivelog.c has a duplicate code to
execute IDENTIFY_SYSTEM replication command on the server which
can be replaced with RunIdentifySystem().
2) bool returning ReceiveXlogStream() in pg_receivewal.c is being
used without type-casting its return return value which might
generate a warning with some compilers. This kind of type-casting
is more common in other places in the postgres code base.
---
 src/bin/pg_basebackup/pg_receivewal.c |  2 +-
 src/bin/pg_basebackup/receivelog.c    | 33 ++++++++++++---------------
 2 files changed, 16 insertions(+), 19 deletions(-)

diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c
index 4474273daf..445e44581e 100644
--- a/src/bin/pg_basebackup/pg_receivewal.c
+++ b/src/bin/pg_basebackup/pg_receivewal.c
@@ -436,7 +436,7 @@ StreamLog(void)
 	stream.partial_suffix = ".partial";
 	stream.replication_slot = replication_slot;
 
-	ReceiveXlogStream(conn, &stream);
+	(void) ReceiveXlogStream(conn, &stream);
 
 	if (!stream.walmethod->finish())
 	{
diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c
index ec53b6837e..fb317d9335 100644
--- a/src/bin/pg_basebackup/receivelog.c
+++ b/src/bin/pg_basebackup/receivelog.c
@@ -482,36 +482,33 @@ ReceiveXlogStream(PGconn *conn, StreamCtl *stream)
 
 	if (stream->sysidentifier != NULL)
 	{
-		/* Validate system identifier hasn't changed */
-		res = PQexec(conn, "IDENTIFY_SYSTEM");
-		if (PQresultStatus(res) != PGRES_TUPLES_OK)
-		{
-			pg_log_error("could not send replication command \"%s\": %s",
-						 "IDENTIFY_SYSTEM", PQerrorMessage(conn));
-			PQclear(res);
-			return false;
-		}
-		if (PQntuples(res) != 1 || PQnfields(res) < 3)
+		char	   *sysidentifier;
+		TimeLineID	servertli;
+
+		/*
+		 * Get the server system identifier and timeline, validate with what we
+		 * have.
+		 */
+		if (!RunIdentifySystem(conn, &sysidentifier, &servertli, NULL, NULL))
 		{
-			pg_log_error("could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields",
-						 PQntuples(res), PQnfields(res), 1, 3);
-			PQclear(res);
+			pg_free(sysidentifier);
 			return false;
 		}
-		if (strcmp(stream->sysidentifier, PQgetvalue(res, 0, 0)) != 0)
+
+		if (strcmp(stream->sysidentifier, sysidentifier) != 0)
 		{
 			pg_log_error("system identifier does not match between base backup and streaming connection");
-			PQclear(res);
+			pg_free(sysidentifier);
 			return false;
 		}
-		if (stream->timeline > atoi(PQgetvalue(res, 0, 1)))
+		pg_free(sysidentifier);
+
+		if (stream->timeline > servertli)
 		{
 			pg_log_error("starting timeline %u is not present in the server",
 						 stream->timeline);
-			PQclear(res);
 			return false;
 		}
-		PQclear(res);
 	}
 
 	/*
-- 
2.25.1

