From aa089820ab661dbd29194934b63946d140dacd8d Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Fri, 10 Nov 2017 14:35:17 +0900 Subject: [PATCH] repmgrd: check shared library is loaded If this isn't the case, "repmgrd" will appear to run but not handle failover correctly. Address GitHub #337. --- dbutils.c | 22 +++++++++++++++++++++- dbutils.h | 1 + repmgr.c | 1 - repmgrd.c | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/dbutils.c b/dbutils.c index 9525f882..ac839076 100644 --- a/dbutils.c +++ b/dbutils.c @@ -1554,7 +1554,7 @@ repmgrd_set_local_node_id(PGconn *conn, int local_node_id) initPQExpBuffer(&query); appendPQExpBuffer(&query, - " SELECT repmgr.set_local_node_id(%i)", + "SELECT repmgr.set_local_node_id(%i)", local_node_id); res = PQexec(conn, query.data); @@ -1572,6 +1572,26 @@ repmgrd_set_local_node_id(PGconn *conn, int local_node_id) +int +repmgrd_get_local_node_id(PGconn *conn) +{ + PGresult *res = NULL; + int local_node_id = UNKNOWN_NODE_ID; + + res = PQexec(conn, "SELECT repmgr.get_local_node_id()"); + + if (!PQgetisnull(res, 0, 0)) + { + local_node_id = atoi(PQgetvalue(res, 0, 0)); + } + + PQclear(res); + + return local_node_id; +} + + + /* ================ */ /* result functions */ /* ================ */ diff --git a/dbutils.h b/dbutils.h index 8c78c05a..5fbfb16c 100644 --- a/dbutils.h +++ b/dbutils.h @@ -382,6 +382,7 @@ bool can_use_pg_rewind(PGconn *conn, const char *data_directory, PQExpBufferDat int get_ready_archive_files(PGconn *conn, const char *data_directory); bool identify_system(PGconn *repl_conn, t_system_identification *identification); bool repmgrd_set_local_node_id(PGconn *conn, int local_node_id); +int repmgrd_get_local_node_id(PGconn *conn); /* extension functions */ ExtensionStatus get_repmgr_extension_status(PGconn *conn); diff --git a/repmgr.c b/repmgr.c index 29bb183b..a12fc4d9 100644 --- a/repmgr.c +++ b/repmgr.c @@ -124,7 +124,6 @@ _PG_init(void) { elog(DEBUG1, "repmgr init"); - /* error here? */ if (!process_shared_preload_libraries_in_progress) return; diff --git a/repmgrd.c b/repmgrd.c index e850bb5c..8f88aeaa 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -332,6 +332,24 @@ main(int argc, char **argv) repmgrd_set_local_node_id(local_conn, config_file_options.node_id); + { + /* + * sanity-check that the shared library is loaded and shared memory + * can be written by attempting to retrieve the previously stored node_id + */ + int stored_local_node_id = UNKNOWN_NODE_ID; + + stored_local_node_id = repmgrd_get_local_node_id(local_conn); + + if (stored_local_node_id == UNKNOWN_NODE_ID) + { + log_error(_("unable to write to shared memory")); + log_hint(_("ensure \"shared_preload_libraries\" includes \"repmgr\"")); + PQfinish(local_conn); + terminate(ERR_BAD_CONFIG); + } + } + if (config_file_options.replication_type == REPLICATION_TYPE_BDR) { log_debug("node id is %i", local_node_info.node_id);