From cc1ea003336a31bca4133b4d5c06f119ccbbca0a Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Tue, 19 Jan 2016 13:51:49 +0900 Subject: [PATCH] Ensure event logging doesn't generate an error when cloning from a standby --- repmgr.c | 60 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/repmgr.c b/repmgr.c index 34c2fc1c..ad205a01 100644 --- a/repmgr.c +++ b/repmgr.c @@ -1212,6 +1212,7 @@ do_standby_unregister(void) static void do_standby_clone(void) { + PGconn *primary_conn = NULL; PGconn *upstream_conn; PGresult *res; @@ -1286,6 +1287,23 @@ do_standby_clone(void) log_info(_("Successfully connected to upstream node. Current installation size is %s\n"), cluster_size); + /* + * If the upstream node is a standby, try to connect to the primary too so we + * can write an event record + */ + if (is_standby(upstream_conn)) + { + if (strlen(options.cluster_name)) + { + primary_conn = get_master_connection(upstream_conn, options.cluster_name, + NULL, NULL); + } + } + else + { + primary_conn = upstream_conn; + } + /* * If --recovery-min-apply-delay was passed, check that * we're connected to PostgreSQL 9.4 or later @@ -1934,29 +1952,33 @@ stop_backup: log_hint(_("for example : /etc/init.d/postgresql start\n")); } - /* Log the event */ - initPQExpBuffer(&event_details); + /* Log the event - if we could connect to the primary */ - /* Add details about relevant runtime options used */ - appendPQExpBuffer(&event_details, - _("Cloned from host '%s', port %s"), - runtime_options.host, - runtime_options.masterport); + if (primary_conn != NULL) + { + initPQExpBuffer(&event_details); - appendPQExpBuffer(&event_details, - _("; backup method: %s"), - runtime_options.rsync_only ? "rsync" : "pg_basebackup"); + /* Add details about relevant runtime options used */ + appendPQExpBuffer(&event_details, + _("Cloned from host '%s', port %s"), + runtime_options.host, + runtime_options.masterport); - appendPQExpBuffer(&event_details, - _("; --force: %s"), - runtime_options.force ? "Y" : "N"); + appendPQExpBuffer(&event_details, + _("; backup method: %s"), + runtime_options.rsync_only ? "rsync" : "pg_basebackup"); - create_event_record(upstream_conn, - &options, - options.node, - "standby_clone", - true, - event_details.data); + appendPQExpBuffer(&event_details, + _("; --force: %s"), + runtime_options.force ? "Y" : "N"); + + create_event_record(primary_conn, + &options, + options.node, + "standby_clone", + true, + event_details.data); + } PQfinish(upstream_conn); exit(retval);