From 687872e9793aa8dd2cae67cc873ada2e616bc256 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Tue, 6 Jan 2015 13:47:31 +0900 Subject: [PATCH] get_data_directory() -> get_pg_setting() More code consolidation --- dbutils.c | 40 ++++++++++++++++++++++++++++++---------- dbutils.h | 2 +- repmgr.c | 36 +++++++++++------------------------- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/dbutils.c b/dbutils.c index 9591755e..814b7157 100644 --- a/dbutils.c +++ b/dbutils.c @@ -278,7 +278,7 @@ get_cluster_size(PGconn *conn, char *size) " FROM pg_database "); res = PQexec(conn, sqlquery); - if (PQresultStatus(res) != PGRES_TUPLES_OK) + if (res == NULL || PQresultStatus(res) != PGRES_TUPLES_OK) { log_err(_("Get cluster size PQexec failed: %s"), PQerrorMessage(conn)); @@ -295,32 +295,52 @@ get_cluster_size(PGconn *conn, char *size) bool -get_data_directory(PGconn *conn, char *data_directory) +get_pg_setting(PGconn *conn, const char *setting, char *output) { char sqlquery[QUERY_STR_LEN]; PGresult *res; + int i; + bool success = true; sqlquery_snprintf(sqlquery, - "SELECT setting " - " FROM pg_settings WHERE name = 'data_directory'"); + "SELECT name, setting " + " FROM pg_settings WHERE name = '%s'", + setting); - log_debug(_("get_data_directory(): %s\n"), sqlquery); + log_debug(_("get_pg_setting(): %s\n"), sqlquery); res = PQexec(conn, sqlquery); - if (res == NULL || PQresultStatus(res) != PGRES_TUPLES_OK || PQntuples(res) != 1) + if (res == NULL || PQresultStatus(res) != PGRES_TUPLES_OK) { - log_err(_("get_data_directory() - PQexec failed: %s"), + log_err(_("get_pg_setting() - PQexec failed: %s"), PQerrorMessage(conn)); PQclear(res); return false; } - strncpy(data_directory, PQgetvalue(res, 0, 0), MAXLEN); - log_debug(_("get_data_directory(): returned value is '%s'\n"), data_directory); + for (i = 0; i < PQntuples(res); i++) + { + if (strcmp(PQgetvalue(res, i, 0), setting) == 0) + { + strncpy(output, PQgetvalue(res, i, 1), MAXLEN); + success = true; + break; + } + else + { + log_err(_("unknown parameter: %s"), PQgetvalue(res, i, 0)); + } + } + + if(success == true) + { + log_debug(_("get_pg_setting(): returned value is '%s'\n"), output); + } + PQclear(res); - return true; + return success; } diff --git a/dbutils.h b/dbutils.h index a68db641..b87aed95 100644 --- a/dbutils.h +++ b/dbutils.h @@ -32,7 +32,7 @@ int is_witness(PGconn *conn, char *schema, char *cluster, int node_id); bool is_pgup(PGconn *conn, int timeout); int get_server_version(PGconn *conn, char *server_version); bool get_cluster_size(PGconn *conn, char *size); -bool get_data_directory(PGconn *conn, char *data_directory); +bool get_pg_setting(PGconn *conn, const char *setting, char *output); int guc_set(PGconn *conn, const char *parameter, const char *op, const char *value); diff --git a/repmgr.c b/repmgr.c index 47db076e..f3aae632 100644 --- a/repmgr.c +++ b/repmgr.c @@ -1185,7 +1185,7 @@ do_standby_promote(void) promote_check_timeout = 60, promote_check_interval = 2; bool promote_sucess = false; - bool res; + bool success; /* We need to connect to check configuration */ log_info(_("%s connecting to standby database\n"), progname); @@ -1221,10 +1221,10 @@ do_standby_promote(void) log_notice(_("%s: Promoting standby\n"), progname); /* Get the data directory */ - res = get_data_directory(conn, data_dir); + success = get_pg_setting(conn, "data_directory", data_dir); PQfinish(conn); - if (res == false) + if (success == false) { log_err(_("Unable to determine data directory\n")); exit(ERR_BAD_CONFIG); @@ -1302,7 +1302,7 @@ do_standby_follow(void) char standby_version[MAXVERSIONSTR]; int standby_version_num = 0; - bool res; + bool success; /* We need to connect to check configuration */ @@ -1394,10 +1394,10 @@ do_standby_follow(void) log_info(_("%s Changing standby's master\n"), progname); /* Get the data directory full path */ - res = get_data_directory(conn, data_dir); + success = get_pg_setting(conn, "data_directory", data_dir); PQfinish(conn); - if (res == false) + if (success == false) { log_err(_("Unable to determine data directory\n")); exit(ERR_BAD_CONFIG); @@ -1439,9 +1439,9 @@ do_witness_create(void) int r = 0, retval; - int i; char master_hba_file[MAXLEN]; + bool success; /* Connection parameters for master only */ keywords[0] = "host"; @@ -1593,27 +1593,13 @@ do_witness_create(void) } /* Get the pg_hba.conf full path */ - sqlquery_snprintf(sqlquery, "SELECT name, setting " - " FROM pg_settings " - " WHERE name IN ('hba_file')"); - log_debug(_("witness create: %s"), sqlquery); - res = PQexec(masterconn, sqlquery); - if (PQresultStatus(res) != PGRES_TUPLES_OK) + success = get_pg_setting(masterconn, "hba_file", master_hba_file); + + if (success == false) { - log_err(_("Can't get info about pg_hba.conf: %s\n"), - PQerrorMessage(masterconn)); - PQclear(res); - PQfinish(masterconn); + log_err(_("Can't get info about pg_hba.conf\n")); exit(ERR_DB_QUERY); } - for (i = 0; i < PQntuples(res); i++) - { - if (strcmp(PQgetvalue(res, i, 0), "hba_file") == 0) - strcpy(master_hba_file, PQgetvalue(res, i, 1)); - else - log_err(_("unknown parameter: %s"), PQgetvalue(res, i, 0)); - } - PQclear(res); r = copy_remote_files(runtime_options.host, runtime_options.remote_user, master_hba_file, runtime_options.dest_dir);