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:
Ian Barwick
2017-11-28 10:51:32 +09:00
parent de34e4e89b
commit 472d703d2e
5 changed files with 40 additions and 8 deletions

View File

@@ -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);