From 401b8a524136004bdf682a38ebffe37ebe298e2d Mon Sep 17 00:00:00 2001 From: Shlok Kyal Date: Tue, 20 Feb 2024 14:49:56 +0530 Subject: [PATCH v24 12/18] 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.43.0