mirror of
https://github.com/EnterpriseDB/repmgr.git
synced 2026-03-22 22:56:29 +00:00
repmgr: initialise "voting_term" in "repmgr primary register"
This previously happened in the extension SQL code, which could potentially cause replay problems if installing on a BDR cluster. As this table is only required for streaming replication failover, move the initialisation to "repmgr primary register". Addresses GitHub #344 .
This commit is contained in:
36
dbutils.c
36
dbutils.c
@@ -3735,7 +3735,7 @@ int
|
||||
get_current_term(PGconn *conn)
|
||||
{
|
||||
PGresult *res = NULL;
|
||||
int term = -1;
|
||||
int term = VOTING_TERM_NOT_SET;
|
||||
|
||||
res = PQexec(conn, "SELECT term FROM repmgr.voting_term");
|
||||
|
||||
@@ -3747,13 +3747,43 @@ get_current_term(PGconn *conn)
|
||||
return -1;
|
||||
}
|
||||
|
||||
term = atoi(PQgetvalue(res, 0, 0));
|
||||
if (PQntuples(res) > 0)
|
||||
{
|
||||
term = atoi(PQgetvalue(res, 0, 0));
|
||||
}
|
||||
|
||||
PQclear(res);
|
||||
return term;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
initialize_voting_term(PGconn *conn)
|
||||
{
|
||||
PGresult *res = NULL;
|
||||
|
||||
int current_term = get_current_term(conn);
|
||||
|
||||
if (current_term == VOTING_TERM_NOT_SET)
|
||||
{
|
||||
res = PQexec(conn, "INSERT INTO repmgr.voting_term (term) VALUES (1)");
|
||||
}
|
||||
else
|
||||
{
|
||||
res = PQexec(conn, "UPDATE repmgr.voting_term SET term = 1");
|
||||
}
|
||||
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
log_error(_("unable to initialize repmgr.voting_term:\n %s"),
|
||||
PQerrorMessage(conn));
|
||||
}
|
||||
|
||||
PQclear(res);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
increment_current_term(PGconn *conn)
|
||||
{
|
||||
@@ -3765,8 +3795,6 @@ increment_current_term(PGconn *conn)
|
||||
{
|
||||
log_error(_("unable to increment repmgr.voting_term:\n %s"),
|
||||
PQerrorMessage(conn));
|
||||
PQclear(res);
|
||||
return;
|
||||
}
|
||||
|
||||
PQclear(res);
|
||||
|
||||
@@ -473,6 +473,7 @@ bool delete_monitoring_records(PGconn *primary_conn, int keep_history);
|
||||
|
||||
|
||||
/* node voting functions */
|
||||
void initialize_voting_term(PGconn *conn);
|
||||
int get_current_term(PGconn *conn);
|
||||
void increment_current_term(PGconn *conn);
|
||||
bool announce_candidature(PGconn *conn, t_node_info *this_node, t_node_info *other_node, int electoral_term);
|
||||
|
||||
@@ -91,9 +91,6 @@ CREATE RULE voting_term_delete AS
|
||||
ON DELETE TO repmgr.voting_term
|
||||
DO INSTEAD NOTHING;
|
||||
|
||||
/* XXX do this in "repmgr primary register" */
|
||||
INSERT INTO repmgr.voting_term (term) VALUES (1);
|
||||
|
||||
|
||||
/* ================= */
|
||||
/* repmgrd functions */
|
||||
|
||||
@@ -74,7 +74,11 @@ do_primary_register(void)
|
||||
|
||||
log_verbose(LOG_INFO, _("server is not in recovery"));
|
||||
|
||||
/* create the repmgr extension if it doesn't already exist */
|
||||
/*
|
||||
* create the repmgr extension if it doesn't already exist;
|
||||
* note that create_repmgr_extension() will take into account
|
||||
* the --dry-run option
|
||||
*/
|
||||
if (!create_repmgr_extension(conn))
|
||||
{
|
||||
PQfinish(conn);
|
||||
@@ -92,6 +96,7 @@ do_primary_register(void)
|
||||
return;
|
||||
}
|
||||
|
||||
initialize_voting_term(conn);
|
||||
|
||||
/* Ensure there isn't another registered node which is primary */
|
||||
primary_conn = get_primary_connection(conn, ¤t_primary_id, NULL);
|
||||
|
||||
Reference in New Issue
Block a user