From 71dda21b85ca4118adcafe74b5a80fea86d83608 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Wed, 28 Nov 2018 15:04:32 +0900 Subject: [PATCH] "witness register": don't try and read nodes table if it doesn't exist Previously, "repmgr witness register --dry-run" would attempt to check for records in the nodes table, but that might not exist yet. Skip that check if the repmgr extension is not yet installed. Implements GitHub #513. --- repmgr-action-witness.c | 49 ++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/repmgr-action-witness.c b/repmgr-action-witness.c index a7714c20..16597e55 100644 --- a/repmgr-action-witness.c +++ b/repmgr-action-witness.c @@ -37,6 +37,7 @@ do_witness_register(void) PGconn *witness_conn = NULL; PGconn *primary_conn = NULL; RecoveryType recovery_type = RECTYPE_UNKNOWN; + ExtensionStatus extension_status = REPMGR_UNKNOWN; NodeInfoList nodes = T_NODE_INFO_LIST_INITIALIZER; t_node_info node_record = T_NODE_INFO_INITIALIZER; RecordStatus record_status = RECORD_NOT_FOUND; @@ -214,33 +215,45 @@ do_witness_register(void) } } + extension_status = get_repmgr_extension_status(witness_conn, NULL); + /* - * if repmgr.nodes contains entries, delete if -F/--force provided, - * otherwise exit with error + * Check if the witness database already contains node records; + * only do this if the extension is actually installed. */ - if (get_all_node_records(witness_conn, &nodes) == false) + if (extension_status == REPMGR_INSTALLED + || extension_status == REPMGR_OLD_VERSION_INSTALLED) { - /* get_all_node_records() will display the error */ - PQfinish(witness_conn); - PQfinish(primary_conn); - exit(ERR_BAD_CONFIG); - } + /* + * if repmgr.nodes contains entries, exit with error unless + * -F/--force provided (which will cause the existing records + * to be overwritten) + */ - log_verbose(LOG_DEBUG, "%i node records found", nodes.node_count); - - if (nodes.node_count > 0) - { - if (!runtime_options.force) + if (get_all_node_records(witness_conn, &nodes) == false) { - log_error(_("witness node is already initialised and contains node records")); - log_hint(_("use option -F/--force to reinitialise the node")); - PQfinish(primary_conn); + /* get_all_node_records() will display the error */ PQfinish(witness_conn); + PQfinish(primary_conn); exit(ERR_BAD_CONFIG); } - } - clear_node_info_list(&nodes); + log_verbose(LOG_DEBUG, "%i node records found", nodes.node_count); + + if (nodes.node_count > 0) + { + if (!runtime_options.force) + { + log_error(_("witness node is already initialised and contains node records")); + log_hint(_("use option -F/--force to reinitialise the node")); + PQfinish(primary_conn); + PQfinish(witness_conn); + exit(ERR_BAD_CONFIG); + } + } + + clear_node_info_list(&nodes); + } if (runtime_options.dry_run == true) {