v23-0007-Fix-server_is_in_recovery.patch

application/octet-stream

Filename: v23-0007-Fix-server_is_in_recovery.patch
Type: application/octet-stream
Part: 6
Message: Re: speed up a logical replica setup

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v23-0007
Subject: Fix server_is_in_recovery
File+
src/bin/pg_basebackup/pg_createsubscriber.c 8 17
From 266c4db08b2b0192290c9484801e98508a207c8b Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Mon, 19 Feb 2024 04:12:32 +0000
Subject: [PATCH v23 07/13] Fix server_is_in_recovery

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

diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c
index 252d541472..ea4eb7e621 100644
--- a/src/bin/pg_basebackup/pg_createsubscriber.c
+++ b/src/bin/pg_basebackup/pg_createsubscriber.c
@@ -73,7 +73,7 @@ static uint64 get_primary_sysid(const char *conninfo);
 static uint64 get_standby_sysid(const char *datadir);
 static void modify_subscriber_sysid(const char *pg_resetwal_path,
 									CreateSubscriberOptions *opt);
-static int	server_is_in_recovery(PGconn *conn);
+static bool	server_is_in_recovery(PGconn *conn);
 static bool check_publisher(LogicalRepInfo *dbinfo);
 static bool setup_publisher(LogicalRepInfo *dbinfo);
 static bool check_subscriber(LogicalRepInfo *dbinfo);
@@ -651,7 +651,7 @@ setup_publisher(LogicalRepInfo *dbinfo)
  * If the answer is yes, it returns 1, otherwise, returns 0. If an error occurs
  * while executing the query, it returns -1.
  */
-static int
+static bool
 server_is_in_recovery(PGconn *conn)
 {
 	PGresult   *res;
@@ -660,22 +660,13 @@ server_is_in_recovery(PGconn *conn)
 	res = PQexec(conn, "SELECT pg_catalog.pg_is_in_recovery()");
 
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
-	{
-		PQclear(res);
-		pg_log_error("could not obtain recovery progress");
-		return -1;
-	}
+		pg_fatal("could not obtain recovery progress");
 
 	ret = strcmp("t", PQgetvalue(res, 0, 0));
 
 	PQclear(res);
 
-	if (ret == 0)
-		return 1;
-	else if (ret > 0)
-		return 0;
-	else
-		return -1;				/* should not happen */
+	return ret == 0;
 }
 
 /*
@@ -704,7 +695,7 @@ check_publisher(LogicalRepInfo *dbinfo)
 	 * If the primary server is in recovery (i.e. cascading replication),
 	 * objects (publication) cannot be created because it is read only.
 	 */
-	if (server_is_in_recovery(conn) == 1)
+	if (server_is_in_recovery(conn))
 		pg_fatal("primary server cannot be in recovery");
 
 	/*------------------------------------------------------------------------
@@ -845,7 +836,7 @@ check_subscriber(LogicalRepInfo *dbinfo)
 		exit(1);
 
 	/* The target server must be a standby */
-	if (server_is_in_recovery(conn) == 0)
+	if (!server_is_in_recovery(conn))
 	{
 		pg_log_error("The target server is not a standby");
 		return false;
@@ -1223,7 +1214,7 @@ wait_for_end_recovery(const char *conninfo, const char *pg_ctl_path,
 
 	for (;;)
 	{
-		int			in_recovery;
+		bool			in_recovery;
 
 		in_recovery = server_is_in_recovery(conn);
 
@@ -1231,7 +1222,7 @@ wait_for_end_recovery(const char *conninfo, const char *pg_ctl_path,
 		 * Does the recovery process finish? In dry run mode, there is no
 		 * recovery mode. Bail out as the recovery process has ended.
 		 */
-		if (in_recovery == 0 || dry_run)
+		if (!in_recovery || dry_run)
 		{
 			status = POSTMASTER_READY;
 			recovery_ended = true;
-- 
2.41.0.windows.3