v23-0012-Avoid-running-pg_createsubscriber-on-standby-whi.patch

application/octet-stream

Filename: v23-0012-Avoid-running-pg_createsubscriber-on-standby-whi.patch
Type: application/octet-stream
Part: 10
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-0012
Subject: Avoid running pg_createsubscriber on standby which is primary to other node
File+
src/bin/pg_basebackup/pg_createsubscriber.c 20 0
From 304d5b9e9fd6f95d8fe88ad74aab96f6d80c4336 Mon Sep 17 00:00:00 2001
From: Shlok Kyal <shlok.kyal.oss@gmail.com>
Date: Tue, 20 Feb 2024 14:49:56 +0530
Subject: [PATCH v23 12/13] Avoid running pg_createsubscriber on standby which
 is primary to other node

pg_createsubscriber will throw error when run on a node which is a standby
but also primary to other node.
---
 src/bin/pg_basebackup/pg_createsubscriber.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c
index f5ccd479b6..26ce91f58b 100644
--- a/src/bin/pg_basebackup/pg_createsubscriber.c
+++ b/src/bin/pg_basebackup/pg_createsubscriber.c
@@ -834,6 +834,26 @@ check_subscriber(LogicalRepInfo *dbinfo)
 		return false;
 	}
 
+	/*
+	 * The target server must not be primary for other server. Because the
+	 * pg_createsubscriber would modify the system_identifier at the end of
+	 * run, but walreceiver of another standby would not accept the difference.
+	 */
+	res = PQexec(conn, 
+				 "SELECT count(1) from pg_catalog.pg_stat_activity where backend_type = 'walsender'");
+
+	if (PQresultStatus(res) != PGRES_TUPLES_OK)
+	{
+			pg_log_error("could not obtain walsender information");
+			return false;
+	}
+
+	if (atoi(PQgetvalue(res, 0, 0)) != 0)
+	{
+			pg_log_error("the target server is primary to other server");
+			return false;
+	}
+
 	/*
 	 * Subscriptions can only be created by roles that have the privileges of
 	 * pg_create_subscription role and CREATE privileges on the specified
-- 
2.41.0.windows.3