Tidy up SQL statements

Improves readability
This commit is contained in:
Ian Barwick
2015-01-09 09:46:41 +09:00
parent acf2744ed7
commit f2fa60f5cf
2 changed files with 167 additions and 100 deletions

205
repmgr.c
View File

@@ -438,7 +438,8 @@ do_cluster_show(void)
log_info(_("%s connecting to database\n"), progname); log_info(_("%s connecting to database\n"), progname);
conn = establish_db_connection(options.conninfo, true); conn = establish_db_connection(options.conninfo, true);
sqlquery_snprintf(sqlquery, "SELECT conninfo, witness FROM %s.repl_nodes;", sqlquery_snprintf(sqlquery,
"SELECT conninfo, witness FROM %s.repl_nodes ",
repmgr_schema); repmgr_schema);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
@@ -501,13 +502,16 @@ do_cluster_cleanup(void)
if (runtime_options.keep_history > 0) if (runtime_options.keep_history > 0)
{ {
sqlquery_snprintf(sqlquery, "DELETE FROM %s.repl_monitor " sqlquery_snprintf(sqlquery,
" WHERE age(now(), last_monitor_time) >= '%d days'::interval;", "DELETE FROM %s.repl_monitor "
repmgr_schema, runtime_options.keep_history); " WHERE age(now(), last_monitor_time) >= '%d days'::interval ",
repmgr_schema,
runtime_options.keep_history);
} }
else else
{ {
sqlquery_snprintf(sqlquery, "TRUNCATE TABLE %s.repl_monitor;", sqlquery_snprintf(sqlquery,
"TRUNCATE TABLE %s.repl_monitor",
repmgr_schema); repmgr_schema);
} }
res = PQexec(master_conn, sqlquery); res = PQexec(master_conn, sqlquery);
@@ -525,7 +529,7 @@ do_cluster_cleanup(void)
* Let's VACUUM the table to avoid autovacuum to be launched in an * Let's VACUUM the table to avoid autovacuum to be launched in an
* unexpected hour * unexpected hour
*/ */
sqlquery_snprintf(sqlquery, "VACUUM %s.repl_monitor;", repmgr_schema); sqlquery_snprintf(sqlquery, "VACUUM %s.repl_monitor", repmgr_schema);
res = PQexec(master_conn, sqlquery); res = PQexec(master_conn, sqlquery);
/* XXX There is any need to check this VACUUM happens without problems? */ /* XXX There is any need to check this VACUUM happens without problems? */
@@ -581,7 +585,8 @@ do_master_register(void)
/* Check if there is a schema for this cluster */ /* Check if there is a schema for this cluster */
sqlquery_snprintf(sqlquery, sqlquery_snprintf(sqlquery,
"SELECT 1 FROM pg_namespace " "SELECT 1 FROM pg_namespace "
"WHERE nspname = '%s'", repmgr_schema); "WHERE nspname = '%s' ",
repmgr_schema);
log_debug(_("master register: %s\n"), sqlquery); log_debug(_("master register: %s\n"), sqlquery);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -621,9 +626,11 @@ do_master_register(void)
if (runtime_options.force) if (runtime_options.force)
{ {
sqlquery_snprintf(sqlquery, "DELETE FROM %s.repl_nodes " sqlquery_snprintf(sqlquery,
" WHERE id = %d", "DELETE FROM %s.repl_nodes "
repmgr_schema, options.node); " WHERE id = %d ",
repmgr_schema,
options.node);
log_debug(_("master register: %s\n"), sqlquery); log_debug(_("master register: %s\n"), sqlquery);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
@@ -651,10 +658,15 @@ do_master_register(void)
/* Now register the master */ /* Now register the master */
sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes (id, cluster, name, conninfo, priority) " sqlquery_snprintf(sqlquery,
"VALUES (%d, '%s', '%s', '%s', %d)", "INSERT INTO %s.repl_nodes (id, cluster, name, conninfo, priority) "
repmgr_schema, options.node, options.cluster_name, options.node_name, "VALUES (%d, '%s', '%s', '%s', %d) ",
options.conninfo, options.priority); repmgr_schema,
options.node,
options.cluster_name,
options.node_name,
options.conninfo,
options.priority);
log_debug(_("master register: %s\n"), sqlquery); log_debug(_("master register: %s\n"), sqlquery);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
@@ -725,7 +737,8 @@ do_standby_register(void)
} }
/* Check if there is a schema for this cluster */ /* Check if there is a schema for this cluster */
sqlquery_snprintf(sqlquery, "SELECT 1 FROM pg_namespace WHERE nspname = '%s'", sqlquery_snprintf(sqlquery,
"SELECT 1 FROM pg_namespace WHERE nspname = '%s'",
repmgr_schema); repmgr_schema);
log_debug(_("standby register: %s\n"), sqlquery); log_debug(_("standby register: %s\n"), sqlquery);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
@@ -783,9 +796,11 @@ do_standby_register(void)
log_info(_("%s registering the standby\n"), progname); log_info(_("%s registering the standby\n"), progname);
if (runtime_options.force) if (runtime_options.force)
{ {
sqlquery_snprintf(sqlquery, "DELETE FROM %s.repl_nodes " sqlquery_snprintf(sqlquery,
"DELETE FROM %s.repl_nodes "
" WHERE id = %d", " WHERE id = %d",
repmgr_schema, options.node); repmgr_schema,
options.node);
log_debug(_("standby register: %s\n"), sqlquery); log_debug(_("standby register: %s\n"), sqlquery);
@@ -801,10 +816,16 @@ do_standby_register(void)
PQclear(res); PQclear(res);
} }
sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes(id, cluster, name, conninfo, priority) " sqlquery_snprintf(sqlquery,
"VALUES (%d, '%s', '%s', '%s', %d)", "INSERT INTO %s.repl_nodes(id, cluster, name, conninfo, priority) "
repmgr_schema, options.node, options.cluster_name, options.node_name, "VALUES (%d, '%s', '%s', '%s', %d) ",
options.conninfo, options.priority); repmgr_schema,
options.node,
options.cluster_name,
options.node_name,
options.conninfo,
options.priority);
log_debug(_("standby register: %s\n"), sqlquery); log_debug(_("standby register: %s\n"), sqlquery);
res = PQexec(master_conn, sqlquery); res = PQexec(master_conn, sqlquery);
@@ -890,7 +911,7 @@ do_standby_clone(void)
sqlquery_snprintf(sqlquery, sqlquery_snprintf(sqlquery,
"SELECT pg_tablespace_location(oid) spclocation " "SELECT pg_tablespace_location(oid) spclocation "
" FROM pg_tablespace " " FROM pg_tablespace "
"WHERE spcname NOT IN ('pg_default', 'pg_global')"); "WHERE spcname NOT IN ('pg_default', 'pg_global') ");
log_debug("standby clone: %s\n", sqlquery); log_debug("standby clone: %s\n", sqlquery);
@@ -947,16 +968,16 @@ do_standby_clone(void)
* presence and if missing force copy by SSH * presence and if missing force copy by SSH
*/ */
sqlquery_snprintf(sqlquery, sqlquery_snprintf(sqlquery,
" WITH dd AS (" " WITH dd AS ( "
" SELECT setting" " SELECT setting "
" FROM pg_settings" " FROM pg_settings "
" WHERE name = 'data_directory'" " WHERE name = 'data_directory' "
" )" " ) "
" SELECT ps.name, ps.setting," " SELECT ps.name, ps.setting, "
" ps.setting ~ ('^' || dd.setting) AS in_data_dir" " ps.setting ~ ('^' || dd.setting) AS in_data_dir "
" FROM dd, pg_settings ps" " FROM dd, pg_settings ps "
" WHERE ps.name IN ('data_directory', 'config_file', 'hba_file', 'ident_file')" " WHERE ps.name IN ('data_directory', 'config_file', 'hba_file', 'ident_file') "
" ORDER BY 1" " ORDER BY 1 "
); );
log_debug(_("standby clone: %s\n"), sqlquery); log_debug(_("standby clone: %s\n"), sqlquery);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
@@ -1604,10 +1625,15 @@ do_witness_create(void)
} }
/* register ourselves in the master */ /* register ourselves in the master */
sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes(id, cluster, name, conninfo, priority, witness) " sqlquery_snprintf(sqlquery,
"VALUES (%d, '%s', '%s', '%s', %d, true)", "INSERT INTO %s.repl_nodes(id, cluster, name, conninfo, priority, witness) "
repmgr_schema, options.node, options.cluster_name, "VALUES (%d, '%s', '%s', '%s', %d, true) ",
options.node_name, options.conninfo, options.priority); repmgr_schema,
options.node,
options.cluster_name,
options.node_name,
options.conninfo,
options.priority);
log_debug(_("witness create: %s"), sqlquery); log_debug(_("witness create: %s"), sqlquery);
res = PQexec(masterconn, sqlquery); res = PQexec(masterconn, sqlquery);
@@ -2056,9 +2082,12 @@ create_schema(PGconn *conn)
* use these functions for providing the latest update timestamp * use these functions for providing the latest update timestamp
*/ */
sqlquery_snprintf(sqlquery, sqlquery_snprintf(sqlquery,
"CREATE FUNCTION %s.repmgr_update_last_updated() RETURNS TIMESTAMP WITH TIME ZONE " "CREATE FUNCTION %s.repmgr_update_last_updated() "
"AS '$libdir/repmgr_funcs', 'repmgr_update_last_updated' " " RETURNS TIMESTAMP WITH TIME ZONE "
" LANGUAGE C STRICT", repmgr_schema); " AS '$libdir/repmgr_funcs', 'repmgr_update_last_updated' "
" LANGUAGE C STRICT ",
repmgr_schema);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{ {
@@ -2070,9 +2099,12 @@ create_schema(PGconn *conn)
sqlquery_snprintf(sqlquery, sqlquery_snprintf(sqlquery,
"CREATE FUNCTION %s.repmgr_get_last_updated() RETURNS TIMESTAMP WITH TIME ZONE " "CREATE FUNCTION %s.repmgr_get_last_updated() "
"AS '$libdir/repmgr_funcs', 'repmgr_get_last_updated' " " RETURNS TIMESTAMP WITH TIME ZONE "
"LANGUAGE C STRICT", repmgr_schema); " AS '$libdir/repmgr_funcs', 'repmgr_get_last_updated' "
" LANGUAGE C STRICT ",
repmgr_schema);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{ {
@@ -2084,13 +2116,16 @@ create_schema(PGconn *conn)
/* ... the tables */ /* ... the tables */
sqlquery_snprintf(sqlquery, "CREATE TABLE %s.repl_nodes ( " sqlquery_snprintf(sqlquery,
" id integer primary key, " "CREATE TABLE %s.repl_nodes ( "
" cluster text not null, " " id INTEGER PRIMARY KEY, "
" name text not null, " " cluster TEXT NOT NULL, "
" conninfo text not null, " " name TEXT NOT NULL, "
" priority integer not null, " " conninfo TEXT NOT NULL, "
" witness boolean not null default false)", repmgr_schema); " priority INTEGER NOT NULL, "
" witness BOOLEAN NOT NULL DEFAULT FALSE) ",
repmgr_schema);
log_debug(_("master register: %s\n"), sqlquery); log_debug(_("master register: %s\n"), sqlquery);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
@@ -2102,15 +2137,17 @@ create_schema(PGconn *conn)
} }
PQclear(res); PQclear(res);
sqlquery_snprintf(sqlquery, "CREATE TABLE %s.repl_monitor ( " sqlquery_snprintf(sqlquery,
"CREATE TABLE %s.repl_monitor ( "
" primary_node INTEGER NOT NULL, " " primary_node INTEGER NOT NULL, "
" standby_node INTEGER NOT NULL, " " standby_node INTEGER NOT NULL, "
" last_monitor_time TIMESTAMP WITH TIME ZONE NOT NULL, " " last_monitor_time TIMESTAMP WITH TIME ZONE NOT NULL, "
" last_apply_time TIMESTAMP WITH TIME ZONE, " " last_apply_time TIMESTAMP WITH TIME ZONE, "
" last_wal_primary_location TEXT NOT NULL, " " last_wal_primary_location TEXT NOT NULL, "
" last_wal_standby_location TEXT, " " last_wal_standby_location TEXT, "
" replication_lag BIGINT NOT NULL, " " replication_lag BIGINT NOT NULL, "
" apply_lag BIGINT NOT NULL) ", repmgr_schema); " apply_lag BIGINT NOT NULL) ",
repmgr_schema);
log_debug(_("master register: %s\n"), sqlquery); log_debug(_("master register: %s\n"), sqlquery);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
@@ -2123,16 +2160,20 @@ create_schema(PGconn *conn)
PQclear(res); PQclear(res);
/* a view */ /* a view */
sqlquery_snprintf(sqlquery, "CREATE VIEW %s.repl_status AS " sqlquery_snprintf(sqlquery,
" SELECT primary_node, standby_node, name AS standby_name, last_monitor_time, " "CREATE VIEW %s.repl_status AS "
" last_wal_primary_location, last_wal_standby_location, " " SELECT primary_node, standby_node, name AS standby_name, last_monitor_time, "
" pg_size_pretty(replication_lag) replication_lag, " " last_wal_primary_location, last_wal_standby_location, "
" age(now(), last_apply_time) AS replication_time_lag, " " pg_size_pretty(replication_lag) replication_lag, "
" pg_size_pretty(apply_lag) apply_lag, " " age(now(), last_apply_time) AS replication_time_lag, "
" age(now(), CASE WHEN pg_is_in_recovery() THEN %s.repmgr_get_last_updated() ELSE last_monitor_time END) AS communication_time_lag " " pg_size_pretty(apply_lag) apply_lag, "
" FROM %s.repl_monitor JOIN %s.repl_nodes ON standby_node = id " " age(now(), CASE WHEN pg_is_in_recovery() THEN %s.repmgr_get_last_updated() ELSE last_monitor_time END) AS communication_time_lag "
" WHERE (standby_node, last_monitor_time) IN (SELECT standby_node, MAX(last_monitor_time) " " FROM %s.repl_monitor "
" FROM %s.repl_monitor GROUP BY 1)", " JOIN %s.repl_nodes ON standby_node = id "
" WHERE (standby_node, last_monitor_time) IN ( "
" SELECT standby_node, MAX(last_monitor_time) "
" FROM %s.repl_monitor GROUP BY 1 "
" )",
repmgr_schema, repmgr_schema, repmgr_schema, repmgr_schema, repmgr_schema, repmgr_schema,
repmgr_schema, repmgr_schema); repmgr_schema, repmgr_schema);
log_debug(_("master register: %s\n"), sqlquery); log_debug(_("master register: %s\n"), sqlquery);
@@ -2148,9 +2189,11 @@ create_schema(PGconn *conn)
PQclear(res); PQclear(res);
/* an index to improve performance of the view */ /* an index to improve performance of the view */
sqlquery_snprintf(sqlquery, "CREATE INDEX idx_repl_status_sort " sqlquery_snprintf(sqlquery,
" ON %s.repl_monitor (last_monitor_time, standby_node) ", "CREATE INDEX idx_repl_status_sort "
" ON %s.repl_monitor (last_monitor_time, standby_node) ",
repmgr_schema); repmgr_schema);
log_debug(_("master register: %s\n"), sqlquery); log_debug(_("master register: %s\n"), sqlquery);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
@@ -2167,9 +2210,12 @@ create_schema(PGconn *conn)
* here * here
*/ */
sqlquery_snprintf(sqlquery, sqlquery_snprintf(sqlquery,
"CREATE OR REPLACE FUNCTION %s.repmgr_update_standby_location(text) RETURNS boolean " "CREATE OR REPLACE FUNCTION %s.repmgr_update_standby_location(text) "
"AS '$libdir/repmgr_funcs', 'repmgr_update_standby_location' " " RETURNS boolean "
"LANGUAGE C STRICT ", repmgr_schema); " AS '$libdir/repmgr_funcs', 'repmgr_update_standby_location' "
" LANGUAGE C STRICT ",
repmgr_schema);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{ {
@@ -2180,9 +2226,12 @@ create_schema(PGconn *conn)
PQclear(res); PQclear(res);
sqlquery_snprintf(sqlquery, sqlquery_snprintf(sqlquery,
"CREATE OR REPLACE FUNCTION %s.repmgr_get_last_standby_location() RETURNS text " "CREATE OR REPLACE FUNCTION %s.repmgr_get_last_standby_location() "
"AS '$libdir/repmgr_funcs', 'repmgr_get_last_standby_location' " " RETURNS text "
"LANGUAGE C STRICT ", repmgr_schema); " AS '$libdir/repmgr_funcs', 'repmgr_get_last_standby_location' "
" LANGUAGE C STRICT ",
repmgr_schema);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{ {
@@ -2214,7 +2263,8 @@ copy_configuration(PGconn *masterconn, PGconn *witnessconn)
return false; return false;
} }
sqlquery_snprintf(sqlquery, "SELECT id, name, conninfo, priority, witness FROM %s.repl_nodes", sqlquery_snprintf(sqlquery,
"SELECT id, name, conninfo, priority, witness FROM %s.repl_nodes",
repmgr_schema); repmgr_schema);
res1 = PQexec(masterconn, sqlquery); res1 = PQexec(masterconn, sqlquery);
if (PQresultStatus(res1) != PGRES_TUPLES_OK) if (PQresultStatus(res1) != PGRES_TUPLES_OK)
@@ -2226,10 +2276,13 @@ copy_configuration(PGconn *masterconn, PGconn *witnessconn)
} }
for (i = 0; i < PQntuples(res1); i++) for (i = 0; i < PQntuples(res1); i++)
{ {
sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes(id, cluster, name, conninfo, priority, witness) " sqlquery_snprintf(sqlquery,
"VALUES (%d, '%s', '%s', '%s', %d, '%s')", "INSERT INTO %s.repl_nodes(id, cluster, name, conninfo, priority, witness) "
repmgr_schema, atoi(PQgetvalue(res1, i, 0)), "VALUES (%d, '%s', '%s', '%s', %d, '%s') ",
options.cluster_name, PQgetvalue(res1, i, 1), repmgr_schema,
atoi(PQgetvalue(res1, i, 0)),
options.cluster_name,
PQgetvalue(res1, i, 1),
PQgetvalue(res1, i, 2), PQgetvalue(res1, i, 2),
atoi(PQgetvalue(res1, i, 3)), atoi(PQgetvalue(res1, i, 3)),
PQgetvalue(res1, i, 4)); PQgetvalue(res1, i, 4));

View File

@@ -517,7 +517,7 @@ witness_monitor(void)
return; return;
/* Get local xlog info */ /* Get local xlog info */
sqlquery_snprintf(sqlquery, "SELECT CURRENT_TIMESTAMP "); sqlquery_snprintf(sqlquery, "SELECT CURRENT_TIMESTAMP");
res = PQexec(my_local_conn, sqlquery); res = PQexec(my_local_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -537,8 +537,8 @@ witness_monitor(void)
sqlquery_snprintf(sqlquery, sqlquery_snprintf(sqlquery,
"INSERT INTO %s.repl_monitor " "INSERT INTO %s.repl_monitor "
"VALUES(%d, %d, '%s'::timestamp with time zone, " "VALUES(%d, %d, '%s'::timestamp with time zone, "
" null, pg_current_xlog_location(), null, " " null, pg_current_xlog_location(), null, "
" 0, 0)", " 0, 0) ",
repmgr_schema, primary_options.node, local_options.node, repmgr_schema, primary_options.node, local_options.node,
monitor_witness_timestamp); monitor_witness_timestamp);
@@ -690,10 +690,9 @@ standby_monitor(void)
return; return;
/* Get local xlog info */ /* Get local xlog info */
sqlquery_snprintf( sqlquery_snprintf(sqlquery,
sqlquery, "SELECT CURRENT_TIMESTAMP, pg_last_xlog_receive_location(), "
"SELECT CURRENT_TIMESTAMP, pg_last_xlog_receive_location(), " "pg_last_xlog_replay_location(), pg_last_xact_replay_timestamp() ");
"pg_last_xlog_replay_location(), pg_last_xact_replay_timestamp()");
res = PQexec(my_local_conn, sqlquery); res = PQexec(my_local_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -711,7 +710,7 @@ standby_monitor(void)
PQclear(res); PQclear(res);
/* Get primary xlog info */ /* Get primary xlog info */
sqlquery_snprintf(sqlquery, "SELECT pg_current_xlog_location() "); sqlquery_snprintf(sqlquery, "SELECT pg_current_xlog_location()");
res = PQexec(primary_conn, sqlquery); res = PQexec(primary_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -735,9 +734,10 @@ standby_monitor(void)
sqlquery_snprintf(sqlquery, sqlquery_snprintf(sqlquery,
"INSERT INTO %s.repl_monitor " "INSERT INTO %s.repl_monitor "
"VALUES(%d, %d, '%s'::timestamp with time zone, " "VALUES(%d, %d, '%s'::timestamp with time zone, "
" '%s'::timestamp with time zone, '%s', '%s', " "'%s'::timestamp with time zone, '%s', '%s', "
" %lld, %lld)", repmgr_schema, "%lld, %lld) ",
primary_options.node, local_options.node, monitor_standby_timestamp, repmgr_schema,
primary_options.node, local_options.node, monitor_standby_timestamp,
last_wal_standby_applied_timestamp, last_wal_standby_applied_timestamp,
last_wal_primary_location, last_wal_primary_location,
last_wal_standby_received, last_wal_standby_received,
@@ -788,11 +788,15 @@ do_failover(void)
t_node_info best_candidate = {-1, "", InvalidXLogRecPtr, false, false, false}; t_node_info best_candidate = {-1, "", InvalidXLogRecPtr, false, false, false};
/* get a list of standby nodes, including myself */ /* get a list of standby nodes, including myself */
sprintf(sqlquery, "SELECT id, conninfo, witness " sprintf(sqlquery,
"SELECT id, conninfo, witness "
" FROM %s.repl_nodes " " FROM %s.repl_nodes "
" WHERE cluster = '%s' " " WHERE cluster = '%s' "
" ORDER BY priority, id LIMIT %i", " ORDER BY priority, id "
repmgr_schema, local_options.cluster_name, FAILOVER_NODES_MAX_CHECK); " LIMIT %i ",
repmgr_schema,
local_options.cluster_name,
FAILOVER_NODES_MAX_CHECK);
res = PQexec(my_local_conn, sqlquery); res = PQexec(my_local_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -860,7 +864,7 @@ do_failover(void)
{ {
log_err(_("Can't reach most of the nodes.\n" log_err(_("Can't reach most of the nodes.\n"
"Let the other standby servers decide which one will be the primary.\n" "Let the other standby servers decide which one will be the primary.\n"
"Manual action will be needed to readd this node to the cluster.\n")); "Manual action will be needed to re-add this node to the cluster.\n"));
terminate(ERR_FAILOVER_FAIL); terminate(ERR_FAILOVER_FAIL);
} }
@@ -1084,6 +1088,10 @@ do_failover(void)
/* once we know who is the best candidate, promote it */ /* once we know who is the best candidate, promote it */
if (find_best && (best_candidate.node_id == local_options.node)) if (find_best && (best_candidate.node_id == local_options.node))
{ {
// ZZZ from loop above this should never happen anyway???
// in fact do_failover is never called if rempgrd is running on a witness,
// as it's only called by standby_monitor()
if (best_candidate.is_witness) if (best_candidate.is_witness)
{ {
log_err(_("%s: Node selected as new master is a witness. Can't be promoted.\n"), log_err(_("%s: Node selected as new master is a witness. Can't be promoted.\n"),
@@ -1205,8 +1213,9 @@ check_cluster_configuration(PGconn *conn)
log_info(_("%s Checking cluster configuration with schema '%s'\n"), log_info(_("%s Checking cluster configuration with schema '%s'\n"),
progname, repmgr_schema); progname, repmgr_schema);
sqlquery_snprintf(sqlquery, "SELECT oid FROM pg_class " sqlquery_snprintf(sqlquery,
" WHERE oid = '%s.repl_nodes'::regclass", "SELECT oid FROM pg_class "
" WHERE oid = '%s.repl_nodes'::regclass ",
repmgr_schema); repmgr_schema);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -1244,7 +1253,8 @@ check_node_configuration(void)
*/ */
log_info(_("%s Checking node %d in cluster '%s'\n"), log_info(_("%s Checking node %d in cluster '%s'\n"),
progname, local_options.node, local_options.cluster_name); progname, local_options.node, local_options.cluster_name);
sqlquery_snprintf(sqlquery, "SELECT * FROM %s.repl_nodes " sqlquery_snprintf(sqlquery,
"SELECT * FROM %s.repl_nodes "
" WHERE id = %d AND cluster = '%s' ", " WHERE id = %d AND cluster = '%s' ",
repmgr_schema, local_options.node, repmgr_schema, local_options.node,
local_options.cluster_name); local_options.cluster_name);
@@ -1275,8 +1285,9 @@ check_node_configuration(void)
/* Adding the node */ /* Adding the node */
log_info(_("%s Adding node %d to cluster '%s'\n"), log_info(_("%s Adding node %d to cluster '%s'\n"),
progname, local_options.node, local_options.cluster_name); progname, local_options.node, local_options.cluster_name);
sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes " sqlquery_snprintf(sqlquery,
"VALUES (%d, '%s', '%s', '%s', 0, 'f')", "INSERT INTO %s.repl_nodes "
"VALUES (%d, '%s', '%s', '%s', 0, 'f') ",
repmgr_schema, local_options.node, repmgr_schema, local_options.node,
local_options.cluster_name, local_options.cluster_name,
local_options.node_name, local_options.node_name,
@@ -1409,12 +1420,15 @@ update_registration(void)
PGresult *res; PGresult *res;
char sqlquery[QUERY_STR_LEN]; char sqlquery[QUERY_STR_LEN];
sqlquery_snprintf(sqlquery, "UPDATE %s.repl_nodes " sqlquery_snprintf(sqlquery,
"UPDATE %s.repl_nodes "
" SET conninfo = '%s', " " SET conninfo = '%s', "
" priority = %d " " priority = %d "
" WHERE id = %d", " WHERE id = %d ",
repmgr_schema, local_options.conninfo, repmgr_schema,
local_options.priority, local_options.node); local_options.conninfo,
local_options.priority,
local_options.node);
res = PQexec(primary_conn, sqlquery); res = PQexec(primary_conn, sqlquery);
if (PQresultStatus(res) != PGRES_COMMAND_OK) if (PQresultStatus(res) != PGRES_COMMAND_OK)