From 872510ed67b91cb7482dea3bce831549d3519e80 Mon Sep 17 00:00:00 2001 From: Euler Taveira Date: Mon, 25 Mar 2024 23:25:30 -0300 Subject: [PATCH v1 2/2] Improve the code that checks if the primary slot is available The target server is started a few instructions before checking the primary server and the replication slot might not be active. Instead of failing the first time, try a few times. --- src/bin/pg_basebackup/pg_createsubscriber.c | 35 +++++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c index cca93d8c25..f17e9bde9c 100644 --- a/src/bin/pg_basebackup/pg_createsubscriber.c +++ b/src/bin/pg_basebackup/pg_createsubscriber.c @@ -887,6 +887,8 @@ check_publisher(const struct LogicalRepInfo *dbinfo) { PQExpBuffer str = createPQExpBuffer(); char *psn_esc = PQescapeLiteral(conn, primary_slot_name, strlen(primary_slot_name)); + int ntuples; + int count = 0; appendPQExpBuffer(str, "SELECT 1 FROM pg_catalog.pg_replication_slots " @@ -897,25 +899,38 @@ check_publisher(const struct LogicalRepInfo *dbinfo) pg_log_debug("command is: %s", str->data); - res = PQexec(conn, str->data); - if (PQresultStatus(res) != PGRES_TUPLES_OK) + /* + * The replication slot might take some time to be active, try a few + * times if necessary. + */ + do { - pg_log_error("could not obtain replication slot information: %s", - PQresultErrorMessage(res)); - disconnect_database(conn, true); - } + res = PQexec(conn, str->data); + if (PQresultStatus(res) != PGRES_TUPLES_OK) + { + pg_log_error("could not obtain replication slot information: %s", + PQresultErrorMessage(res)); + disconnect_database(conn, true); + } - if (PQntuples(res) != 1) + ntuples = PQntuples(res); + PQclear(res); + + if (ntuples == 1) /* replication slot is already active */ + break; + else + count++; + } while (count > NUM_ATTEMPTS); + + if (ntuples != 1) { pg_log_error("could not obtain replication slot information: got %d rows, expected %d row", - PQntuples(res), 1); + ntuples, 1); disconnect_database(conn, true); } else pg_log_info("primary has replication slot \"%s\"", primary_slot_name); - - PQclear(res); } disconnect_database(conn, false); -- 2.30.2