v2-0002-Improve-the-code-that-checks-if-the-primary-slot-.patch
application/octet-stream
Filename: v2-0002-Improve-the-code-that-checks-if-the-primary-slot-.patch
Type: application/octet-stream
Part: 2
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 v2-0002
Subject: Improve the code that checks if the primary slot is available
| File | + | − |
|---|---|---|
| src/bin/pg_basebackup/pg_createsubscriber.c | 25 | 10 |
From 66cdc07bb1e475993a6784158e9912b015dfb472 Mon Sep 17 00:00:00 2001
From: Euler Taveira <euler.taveira@enterprisedb.com>
Date: Mon, 25 Mar 2024 23:25:30 -0300
Subject: [PATCH v2 2/5] 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 b4424861b2..e0a6e10578 100644
--- a/src/bin/pg_basebackup/pg_createsubscriber.c
+++ b/src/bin/pg_basebackup/pg_createsubscriber.c
@@ -891,6 +891,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 "
@@ -901,25 +903,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.34.1