From 6dfad1816c9aaf213b3239490802227beff0d4cc Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Thu, 22 Feb 2024 10:00:02 +0000 Subject: [PATCH v24 14/18] address comments from Vignesh #1 --- doc/src/sgml/ref/pg_createsubscriber.sgml | 7 +++++++ src/bin/pg_basebackup/pg_createsubscriber.c | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/ref/pg_createsubscriber.sgml b/doc/src/sgml/ref/pg_createsubscriber.sgml index 579e50a0a0..4579495089 100644 --- a/doc/src/sgml/ref/pg_createsubscriber.sgml +++ b/doc/src/sgml/ref/pg_createsubscriber.sgml @@ -135,6 +135,13 @@ PostgreSQL documentation would be removed from a primary instance. + + Executing DDL commands while running pg_createsubscriber + is not recommended. Because if the physical standby has already been + converted to the subscriber, it would not be replicated, so an error + would occur. + + The pg_createsubscriber focuses on large-scale systems that contain more data than 1GB. For smaller systems, initial data diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c index 4a28dfb81c..a51943106a 100644 --- a/src/bin/pg_basebackup/pg_createsubscriber.c +++ b/src/bin/pg_basebackup/pg_createsubscriber.c @@ -1226,9 +1226,13 @@ wait_for_end_recovery(const char *conninfo, const char *pg_ctl_path, if (conn == NULL) exit(1); +#define NUM_ACCEPTABLE_DISCONNECTION 15 + for (;;) { bool in_recovery; + PGresult *res; + int count = 0; in_recovery = server_is_in_recovery(conn); @@ -1243,6 +1247,16 @@ wait_for_end_recovery(const char *conninfo, const char *pg_ctl_path, break; } + res = PQexec(conn, + "SELECT count(1) FROM pg_catalog.pg_stat_wal_receiver;"); + + if (atoi(PQgetvalue(res, 0, 0)) == 0 && + count++ > NUM_ACCEPTABLE_DISCONNECTION) + { + stop_standby_server(pg_ctl_path, opt->subscriber_dir); + pg_fatal("standby disconnected from the primary"); + } + /* Bail out after recovery_timeout seconds if this option is set */ if (opt->recovery_timeout > 0 && timer >= opt->recovery_timeout) { @@ -1251,6 +1265,7 @@ wait_for_end_recovery(const char *conninfo, const char *pg_ctl_path, } /* Keep waiting */ + PQclear(res); pg_usleep(WAIT_INTERVAL * USEC_PER_SEC); timer += WAIT_INTERVAL; @@ -1342,7 +1357,7 @@ drop_publication(PGconn *conn, LogicalRepInfo *dbinfo) pg_log_info("dropping publication \"%s\" on database \"%s\"", dbinfo->pubname, dbinfo->dbname); - appendPQExpBuffer(str, "DROP PUBLICATION %s", dbinfo->pubname); + appendPQExpBuffer(str, "DROP PUBLICATION IF EXISTS %s", dbinfo->pubname); pg_log_debug("command is: %s", str->data); -- 2.43.0