From b094d7b4078ca4f0c357f455198336f27977192f Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Thu, 1 Jul 2021 13:04:23 +0900 Subject: [PATCH] standby clone: set "slot_name" in node record if required If executing "repmgr standby clone --replication-conf-only" on a node which was set up without replication slots, but the repmgr configuration was since changed to "use_replication_slots=1", repmgr will attempt to create the replication slot. This will however fail if "slot_name" is not set in the node's record, so have repmgr set the slot_name in this case. It might be preferable to preemptively create the slot name for each node when configuring the cluster, however this would be a behavioural change which would be better off in a major release (for example, it's conceivable a user runs sanity checks on the node records and expects to find the slot names empty if replication slots are not in use). --- repmgr-action-standby.c | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/repmgr-action-standby.c b/repmgr-action-standby.c index 5ba51484..846806a7 100644 --- a/repmgr-action-standby.c +++ b/repmgr-action-standby.c @@ -1300,6 +1300,64 @@ _do_create_replication_conf(void) PQExpBufferData msg; t_replication_slot slot_info = T_REPLICATION_SLOT_INITIALIZER; + /* + * Check the node record has slot_name set; if not we'll need to + * update it. + */ + if (local_node_record.slot_name[0] == '\0') + { + PGconn *primary_conn = NULL; + + create_slot_name(local_node_record.slot_name, local_node_record.node_id); + + /* Check we can connect to the primary so we can update the record */ + + if (get_recovery_type(upstream_conn) == RECTYPE_PRIMARY) + { + primary_conn = upstream_conn; + } + else + { + primary_conn = establish_primary_db_connection(upstream_conn, false); + + if (primary_conn == NULL) + { + log_error(_("unable to connect to primary to update slot name for node \"%s\" (ID: %i)"), + local_node_record.node_name, + local_node_record.node_id); + PQfinish(upstream_conn); + termPQExpBuffer(&msg); + exit(ERR_BAD_CONFIG); + } + } + + if (runtime_options.dry_run == true) + { + log_info(_("would set \"slot_name\" for node \"%s\" (ID: %i) to \"%s\""), + local_node_record.node_name, + local_node_record.node_id, + local_node_record.slot_name); + } + else + { + bool success = update_node_record_slot_name(primary_conn, + local_node_record.node_id, + local_node_record.slot_name); + + if (primary_conn != upstream_conn) + PQfinish(primary_conn); + + if (success == false) + { + log_error(_("unable to update slot name for node \"%s\" (ID: %i)"), + local_node_record.node_name, + local_node_record.node_id); + PQfinish(upstream_conn); + exit(ERR_BAD_CONFIG); + } + } + } + record_status = get_slot_record(upstream_conn, local_node_record.slot_name, &slot_info); /* check if replication slot exists*/