diff --git a/dbutils.c b/dbutils.c index 5aa190f4..b9f8b99a 100644 --- a/dbutils.c +++ b/dbutils.c @@ -1427,6 +1427,50 @@ create_event_record(PGconn *conn, t_configuration_options *options, int node_id, return success; } +/* + * Update node record following change of status + * (e.g. inactive primary converted to standby) + */ +bool +update_node_record_status(PGconn *conn, char *cluster_name, int this_node_id, char *type, int upstream_node_id, bool active) +{ + PGresult *res; + char sqlquery[QUERY_STR_LEN]; + + sqlquery_snprintf(sqlquery, + " UPDATE %s.repl_nodes " + " SET type = '%s', " + " upstream_node_id = %i, " + " active = %s " + " WHERE cluster = '%s' " + " AND id = %i ", + get_repmgr_schema_quoted(conn), + type, + upstream_node_id, + active ? "TRUE" : "FALSE", + cluster_name, + this_node_id); + + log_verbose(LOG_DEBUG, "update_node_record_status():\n%s\n", sqlquery); + + res = PQexec(conn, sqlquery); + + if (PQresultStatus(res) != PGRES_COMMAND_OK) + { + log_err(_("Unable to update node record: %s\n"), + PQerrorMessage(conn)); + PQclear(res); + + return false; + } + + PQclear(res); + + return true; + +} + + bool update_node_record_set_upstream(PGconn *conn, char *cluster_name, int this_node_id, int new_upstream_node_id) { diff --git a/dbutils.h b/dbutils.h index 3d9d8565..5232ed80 100644 --- a/dbutils.h +++ b/dbutils.h @@ -108,6 +108,7 @@ bool copy_configuration(PGconn *masterconn, PGconn *witnessconn, char *cluster_ bool create_node_record(PGconn *conn, char *action, int node, char *type, int upstream_node, char *cluster_name, char *node_name, char *conninfo, int priority, char *slot_name); bool delete_node_record(PGconn *conn, int node, char *action); bool create_event_record(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details); +bool update_node_record_status(PGconn *conn, char *cluster_name, int this_node_id, char *type, int upstream_node_id, bool active); bool update_node_record_set_upstream(PGconn *conn, char *cluster_name, int this_node_id, int new_upstream_node_id); PGresult * get_node_record(PGconn *conn, char *cluster, int node_id);