From 276c947202e8753b1c812a572e29e557f6ee318d Mon Sep 17 00:00:00 2001 From: Jaime Casanova Date: Tue, 26 Jul 2011 16:58:02 -0500 Subject: [PATCH] Add name to nodes and show it for the standby in repl_status. --- config.c | 6 ++++++ repmgr.c | 31 ++++++++++++++++++------------- repmgr.sql | 15 +++++++++------ repmgrd.c | 3 ++- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/config.c b/config.c index a40f4480..50085131 100644 --- a/config.c +++ b/config.c @@ -203,6 +203,12 @@ reload_configuration(char *config_file, t_configuration_options *orig_options) return false; } + if (new_options.standby_name != orig_options->standby_name) + { + log_warning(_("\nCannot change standby name, will keep current configuration.\n")); + return false; + } + if (new_options.failover != MANUAL_FAILOVER && new_options.failover != AUTOMATIC_FAILOVER) { log_warning(_("\nNew value for failover is not valid. Should be MANUAL or AUTOMATIC.\n")); diff --git a/repmgr.c b/repmgr.c index d97d08e2..3055a9ba 100644 --- a/repmgr.c +++ b/repmgr.c @@ -518,9 +518,10 @@ do_master_register(void) } } - sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes (id, cluster, conninfo, priority) " + sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes (id, cluster, name, conninfo, priority) " "VALUES (%d, '%s', '%s', %d)", - repmgr_schema, options.node, options.cluster_name, options.conninfo, options.priority); + repmgr_schema, options.node, options.cluster_name, options.standby_name, + options.conninfo, options.priority); log_debug(_("master register: %s\n"), sqlquery); if (!PQexec(conn, sqlquery)) @@ -660,9 +661,10 @@ do_standby_register(void) } } - sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes(id, cluster, conninfo, priority) " + sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes(id, cluster, name, conninfo, priority) " "VALUES (%d, '%s', '%s', %d)", - repmgr_schema, options.node, options.cluster_name, options.conninfo, options.priority); + repmgr_schema, options.node, options.cluster_name, options.standby_name, + options.conninfo, options.priority); log_debug(_("standby register: %s\n"), sqlquery); if (!PQexec(master_conn, sqlquery)) @@ -1923,6 +1925,7 @@ create_schema(PGconn *conn) sqlquery_snprintf(sqlquery, "CREATE TABLE %s.repl_nodes ( " " id integer primary key, " " cluster text not null, " + " name text not null, " " conninfo text not null, " " priority integer not null, " " witness boolean not null default false)", repmgr_schema); @@ -1954,14 +1957,15 @@ create_schema(PGconn *conn) /* a view */ sqlquery_snprintf(sqlquery, "CREATE VIEW %s.repl_status AS " - " SELECT primary_node, standby_node, last_monitor_time, last_wal_primary_location, " - " last_wal_standby_location, pg_size_pretty(replication_lag) replication_lag, " + " SELECT primary_node, standby_node, name AS standby_name, last_monitor_time, " + " last_wal_primary_location, last_wal_standby_location, " + " pg_size_pretty(replication_lag) replication_lag, " " pg_size_pretty(apply_lag) apply_lag, " " age(now(), last_monitor_time) AS time_lag " - " FROM %s.repl_monitor " + " FROM %s.repl_monitor 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); log_debug(_("master register: %s\n"), sqlquery); if (!PQexec(conn, sqlquery)) { @@ -2027,7 +2031,7 @@ copy_configuration(PGconn *masterconn, PGconn *witnessconn) return false; } - sqlquery_snprintf(sqlquery, "SELECT id, conninfo, priority, witness FROM %s.repl_nodes", repmgr_schema); + sqlquery_snprintf(sqlquery, "SELECT id, name, conninfo, priority, witness FROM %s.repl_nodes", repmgr_schema); res = PQexec(masterconn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) { @@ -2038,12 +2042,13 @@ copy_configuration(PGconn *masterconn, PGconn *witnessconn) } for (i = 0; i < PQntuples(res); i++) { - sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes(id, cluster, conninfo, priority, witness) " - "VALUES (%d, '%s', '%s', %d, '%s')", + sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes(id, cluster, name, conninfo, priority, witness) " + "VALUES (%d, '%s', '%s', '%s', %d, '%s')", repmgr_schema, atoi(PQgetvalue(res, i, 0)), options.cluster_name, PQgetvalue(res, i, 1), - atoi(PQgetvalue(res, i, 2)), - PQgetvalue(res, i, 3)); + PQgetvalue(res, i, 2), + atoi(PQgetvalue(res, i, 3)), + PQgetvalue(res, i, 4)); if (!PQexec(witnessconn, sqlquery)) { diff --git a/repmgr.sql b/repmgr.sql index 204bb45d..02d93333 100644 --- a/repmgr.sql +++ b/repmgr.sql @@ -14,8 +14,11 @@ CREATE SCHEMA repmgr; */ CREATE TABLE repl_nodes ( id integer primary key, - cluster text not null, -- Name to identify the cluster - conninfo text not null + cluster text not null, -- Name to identify the cluster + name text not null, + conninfo text not null, + priority integer not null, + witness boolean not null default false ); ALTER TABLE repl_nodes OWNER TO repmgr; @@ -34,8 +37,6 @@ CREATE TABLE repl_monitor ( ); ALTER TABLE repl_monitor OWNER TO repmgr; -CREATE INDEX idx_repl_monitor_last_monitor_sort ON repl_monitor(last_monitor_time, standby_node); - /* * This view shows the latest monitor info about every node. * Interesting thing to see: @@ -47,12 +48,14 @@ CREATE INDEX idx_repl_monitor_last_monitor_sort ON repl_monitor(last_monitor_tim * time_lag: how many seconds are we from being up-to-date with master */ CREATE VIEW repl_status AS -SELECT primary_node, standby_node, last_monitor_time, last_wal_primary_location, +SELECT primary_node, standby_node, name AS standby_name, last_monitor_time, last_wal_primary_location, last_wal_standby_location, pg_size_pretty(replication_lag) replication_lag, pg_size_pretty(apply_lag) apply_lag, age(now(), last_monitor_time) AS time_lag - FROM repl_monitor + FROM repl_monitor JOIN repl_nodes ON standby_node = id WHERE (standby_node, last_monitor_time) IN (SELECT standby_node, MAX(last_monitor_time) FROM repl_monitor GROUP BY 1); ALTER VIEW repl_status OWNER TO repmgr; + +CREATE INDEX idx_repl_status_sort ON repl_monitor(last_monitor_time, standby_node); diff --git a/repmgrd.c b/repmgrd.c index 92f3f1b5..fafa0396 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -858,9 +858,10 @@ checkNodeConfiguration(char *conninfo) log_info(_("%s Adding node %d to cluster '%s'\n"), progname, local_options.node, local_options.cluster_name); sqlquery_snprintf(sqlquery, "INSERT INTO %s.repl_nodes " - "VALUES (%d, '%s', '%s', 'f')", + "VALUES (%d, '%s', '%s', '%s', 0, 'f')", repmgr_schema, local_options.node, local_options.cluster_name, + local_options.standby_name, local_options.conninfo); if (!PQexec(primaryConn, sqlquery))