mirror of
https://github.com/EnterpriseDB/repmgr.git
synced 2026-03-26 16:46:28 +00:00
pg_indent'ing all files…
Conflicts: version.h
This commit is contained in:
committed by
Jaime Casanova
parent
069f9ff2ed
commit
1c67e105ff
175
dbutils.c
175
dbutils.c
@@ -29,8 +29,8 @@ PGconn *
|
||||
establishDBConnection(const char *conninfo, const bool exit_on_error)
|
||||
{
|
||||
/* Make a connection to the database */
|
||||
PGconn *conn = NULL;
|
||||
char connection_string[MAXLEN];
|
||||
PGconn *conn = NULL;
|
||||
char connection_string[MAXLEN];
|
||||
|
||||
strcpy(connection_string, conninfo);
|
||||
strcat(connection_string, " fallback_application_name='repmgr'");
|
||||
@@ -40,7 +40,7 @@ establishDBConnection(const char *conninfo, const bool exit_on_error)
|
||||
if ((PQstatus(conn) != CONNECTION_OK))
|
||||
{
|
||||
log_err(_("Connection to database failed: %s\n"),
|
||||
PQerrorMessage(conn));
|
||||
PQerrorMessage(conn));
|
||||
|
||||
if (exit_on_error)
|
||||
{
|
||||
@@ -53,16 +53,17 @@ establishDBConnection(const char *conninfo, const bool exit_on_error)
|
||||
}
|
||||
|
||||
PGconn *
|
||||
establishDBConnectionByParams(const char *keywords[], const char *values[],const bool exit_on_error)
|
||||
establishDBConnectionByParams(const char *keywords[], const char *values[],
|
||||
const bool exit_on_error)
|
||||
{
|
||||
/* Make a connection to the database */
|
||||
PGconn *conn = PQconnectdbParams(keywords, values, true);
|
||||
PGconn *conn = PQconnectdbParams(keywords, values, true);
|
||||
|
||||
/* Check to see that the backend connection was successfully made */
|
||||
if ((PQstatus(conn) != CONNECTION_OK))
|
||||
{
|
||||
log_err(_("Connection to database failed: %s\n"),
|
||||
PQerrorMessage(conn));
|
||||
PQerrorMessage(conn));
|
||||
if (exit_on_error)
|
||||
{
|
||||
PQfinish(conn);
|
||||
@@ -84,7 +85,7 @@ is_standby(PGconn *conn)
|
||||
if (res == NULL || PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
log_err(_("Can't query server mode: %s"),
|
||||
PQerrorMessage(conn));
|
||||
PQerrorMessage(conn));
|
||||
result = -1;
|
||||
}
|
||||
else if (PQntuples(res) == 1 && strcmp(PQgetvalue(res, 0, 0), "t") == 0)
|
||||
@@ -104,7 +105,7 @@ is_witness(PGconn *conn, char *schema, char *cluster, int node_id)
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
|
||||
sqlquery_snprintf(sqlquery, "SELECT witness from %s.repl_nodes where cluster = '%s' and id = %d",
|
||||
schema, cluster, node_id);
|
||||
schema, cluster, node_id);
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
@@ -124,6 +125,7 @@ bool
|
||||
is_pgup(PGconn *conn, int timeout)
|
||||
{
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
|
||||
/* Check the connection status twice in case it changes after reset */
|
||||
bool twice = false;
|
||||
|
||||
@@ -134,14 +136,14 @@ is_pgup(PGconn *conn, int timeout)
|
||||
{
|
||||
if (twice)
|
||||
return false;
|
||||
PQreset(conn); /* reconnect */
|
||||
PQreset(conn); /* reconnect */
|
||||
twice = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* Send a SELECT 1 just to check if the connection is OK
|
||||
*/
|
||||
* Send a SELECT 1 just to check if the connection is OK
|
||||
*/
|
||||
if (!CancelQuery(conn, timeout))
|
||||
goto failed;
|
||||
if (wait_connection_availability(conn, timeout) != 1)
|
||||
@@ -151,7 +153,7 @@ is_pgup(PGconn *conn, int timeout)
|
||||
if (PQsendQuery(conn, sqlquery) == 0)
|
||||
{
|
||||
log_warning(_("PQsendQuery: Query could not be sent to primary. %s\n"),
|
||||
PQerrorMessage(conn));
|
||||
PQerrorMessage(conn));
|
||||
goto failed;
|
||||
}
|
||||
if (wait_connection_availability(conn, timeout) != 1)
|
||||
@@ -159,11 +161,15 @@ is_pgup(PGconn *conn, int timeout)
|
||||
|
||||
break;
|
||||
|
||||
failed:
|
||||
/* we need to retry, because we might just have loose the connection once */
|
||||
failed:
|
||||
|
||||
/*
|
||||
* we need to retry, because we might just have loose the
|
||||
* connection once
|
||||
*/
|
||||
if (twice)
|
||||
return false;
|
||||
PQreset(conn); /* reconnect */
|
||||
PQreset(conn); /* reconnect */
|
||||
twice = true;
|
||||
}
|
||||
}
|
||||
@@ -176,23 +182,23 @@ failed:
|
||||
* if 8 or inferior returns an empty string
|
||||
*/
|
||||
char *
|
||||
pg_version(PGconn *conn, char* major_version)
|
||||
pg_version(PGconn *conn, char *major_version)
|
||||
{
|
||||
PGresult *res;
|
||||
PGresult *res;
|
||||
|
||||
int major_version1;
|
||||
char *major_version2;
|
||||
int major_version1;
|
||||
char *major_version2;
|
||||
|
||||
res = PQexec(conn,
|
||||
"WITH pg_version(ver) AS "
|
||||
"(SELECT split_part(version(), ' ', 2)) "
|
||||
"SELECT split_part(ver, '.', 1), split_part(ver, '.', 2) "
|
||||
"FROM pg_version");
|
||||
"WITH pg_version(ver) AS "
|
||||
"(SELECT split_part(version(), ' ', 2)) "
|
||||
"SELECT split_part(ver, '.', 1), split_part(ver, '.', 2) "
|
||||
"FROM pg_version");
|
||||
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
log_err(_("Version check PQexec failed: %s"),
|
||||
PQerrorMessage(conn));
|
||||
PQerrorMessage(conn));
|
||||
PQclear(res);
|
||||
return NULL;
|
||||
}
|
||||
@@ -204,7 +210,7 @@ pg_version(PGconn *conn, char* major_version)
|
||||
{
|
||||
/* form a major version string */
|
||||
xsnprintf(major_version, MAXVERSIONSTR, "%d.%s", major_version1,
|
||||
major_version2);
|
||||
major_version2);
|
||||
}
|
||||
else
|
||||
strcpy(major_version, "");
|
||||
@@ -217,21 +223,21 @@ pg_version(PGconn *conn, char* major_version)
|
||||
|
||||
int
|
||||
guc_set(PGconn *conn, const char *parameter, const char *op,
|
||||
const char *value)
|
||||
const char *value)
|
||||
{
|
||||
PGresult *res;
|
||||
PGresult *res;
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
int retval = 1;
|
||||
|
||||
sqlquery_snprintf(sqlquery, "SELECT true FROM pg_settings "
|
||||
" WHERE name = '%s' AND setting %s '%s'",
|
||||
parameter, op, value);
|
||||
" WHERE name = '%s' AND setting %s '%s'",
|
||||
parameter, op, value);
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
log_err(_("GUC setting check PQexec failed: %s"),
|
||||
PQerrorMessage(conn));
|
||||
PQerrorMessage(conn));
|
||||
retval = -1;
|
||||
}
|
||||
else if (PQntuples(res) == 0)
|
||||
@@ -249,22 +255,22 @@ guc_set(PGconn *conn, const char *parameter, const char *op,
|
||||
* the pg datatype so that the comparison can be done properly.
|
||||
*/
|
||||
int
|
||||
guc_set_typed(PGconn *conn, const char *parameter, const char *op,
|
||||
const char *value, const char *datatype)
|
||||
guc_set_typed(PGconn *conn, const char *parameter, const char *op,
|
||||
const char *value, const char *datatype)
|
||||
{
|
||||
PGresult *res;
|
||||
PGresult *res;
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
int retval = 1;
|
||||
|
||||
sqlquery_snprintf(sqlquery, "SELECT true FROM pg_settings "
|
||||
" WHERE name = '%s' AND setting::%s %s '%s'::%s",
|
||||
parameter, datatype, op, value, datatype);
|
||||
" WHERE name = '%s' AND setting::%s %s '%s'::%s",
|
||||
parameter, datatype, op, value, datatype);
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
log_err(_("GUC setting check PQexec failed: %s"),
|
||||
PQerrorMessage(conn));
|
||||
PQerrorMessage(conn));
|
||||
retval = -1;
|
||||
}
|
||||
else if (PQntuples(res) == 0)
|
||||
@@ -281,20 +287,20 @@ guc_set_typed(PGconn *conn, const char *parameter, const char *op,
|
||||
const char *
|
||||
get_cluster_size(PGconn *conn)
|
||||
{
|
||||
PGresult *res;
|
||||
const char *size = NULL;
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
PGresult *res;
|
||||
const char *size = NULL;
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
|
||||
sqlquery_snprintf(
|
||||
sqlquery,
|
||||
"SELECT pg_size_pretty(SUM(pg_database_size(oid))::bigint) "
|
||||
" FROM pg_database ");
|
||||
sqlquery,
|
||||
"SELECT pg_size_pretty(SUM(pg_database_size(oid))::bigint) "
|
||||
" FROM pg_database ");
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
log_err(_("Get cluster size PQexec failed: %s"),
|
||||
PQerrorMessage(conn));
|
||||
PQerrorMessage(conn));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -309,23 +315,23 @@ get_cluster_size(PGconn *conn)
|
||||
* get a connection to master by reading repl_nodes, creating a connection
|
||||
* to each node (one at a time) and finding if it is a master or a standby
|
||||
*
|
||||
* NB: If master_conninfo_out may be NULL. If it is non-null, it is assumed to
|
||||
* NB: If master_conninfo_out may be NULL. If it is non-null, it is assumed to
|
||||
* point to allocated memory of MAXCONNINFO in length, and the master server
|
||||
* connection string is placed there.
|
||||
*/
|
||||
PGconn *
|
||||
getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
||||
int *master_id, char *master_conninfo_out)
|
||||
int *master_id, char *master_conninfo_out)
|
||||
{
|
||||
PGconn *master_conn = NULL;
|
||||
PGresult *res1;
|
||||
PGresult *res2;
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
char master_conninfo_stack[MAXCONNINFO];
|
||||
char *master_conninfo = &*master_conninfo_stack;
|
||||
char schema_quoted[MAXLEN];
|
||||
PGconn *master_conn = NULL;
|
||||
PGresult *res1;
|
||||
PGresult *res2;
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
char master_conninfo_stack[MAXCONNINFO];
|
||||
char *master_conninfo = &*master_conninfo_stack;
|
||||
char schema_quoted[MAXLEN];
|
||||
|
||||
int i;
|
||||
int i;
|
||||
|
||||
/*
|
||||
* If the caller wanted to get a copy of the connection info string, sub
|
||||
@@ -340,8 +346,8 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
||||
* Assemble the unquoted schema name
|
||||
*/
|
||||
{
|
||||
char *identifier = PQescapeIdentifier(standby_conn, schema,
|
||||
strlen(schema));
|
||||
char *identifier = PQescapeIdentifier(standby_conn, schema,
|
||||
strlen(schema));
|
||||
|
||||
maxlen_snprintf(schema_quoted, "%s", identifier);
|
||||
PQfreemem(identifier);
|
||||
@@ -349,17 +355,17 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
||||
|
||||
/* find all nodes belonging to this cluster */
|
||||
log_info(_("finding node list for cluster '%s'\n"),
|
||||
cluster);
|
||||
cluster);
|
||||
|
||||
sqlquery_snprintf(sqlquery, "SELECT id, conninfo FROM %s.repl_nodes "
|
||||
" WHERE cluster = '%s' and not witness",
|
||||
schema_quoted, cluster);
|
||||
" WHERE cluster = '%s' and not witness",
|
||||
schema_quoted, cluster);
|
||||
|
||||
res1 = PQexec(standby_conn, sqlquery);
|
||||
if (PQresultStatus(res1) != PGRES_TUPLES_OK)
|
||||
{
|
||||
log_err(_("Can't get nodes info: %s\n"),
|
||||
PQerrorMessage(standby_conn));
|
||||
PQerrorMessage(standby_conn));
|
||||
PQclear(res1);
|
||||
return NULL;
|
||||
}
|
||||
@@ -370,7 +376,7 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
||||
*master_id = atoi(PQgetvalue(res1, i, 0));
|
||||
strncpy(master_conninfo, PQgetvalue(res1, i, 1), MAXCONNINFO);
|
||||
log_info(_("checking role of cluster node '%s'\n"),
|
||||
master_conninfo);
|
||||
master_conninfo);
|
||||
master_conn = establishDBConnection(master_conninfo, false);
|
||||
|
||||
if (PQstatus(master_conn) != CONNECTION_OK)
|
||||
@@ -378,15 +384,15 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
||||
|
||||
/*
|
||||
* Can't use the is_standby() function here because on error that
|
||||
* function closes the connection passed and exits. This still
|
||||
* needs to close master_conn first.
|
||||
* function closes the connection passed and exits. This still needs
|
||||
* to close master_conn first.
|
||||
*/
|
||||
res2 = PQexec(master_conn, "SELECT pg_is_in_recovery()");
|
||||
|
||||
if (PQresultStatus(res2) != PGRES_TUPLES_OK)
|
||||
{
|
||||
log_err(_("Can't get recovery state from this node: %s\n"),
|
||||
PQerrorMessage(master_conn));
|
||||
PQerrorMessage(master_conn));
|
||||
PQclear(res2);
|
||||
PQfinish(master_conn);
|
||||
continue;
|
||||
@@ -408,14 +414,13 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
||||
}
|
||||
}
|
||||
|
||||
/* If we finish this loop without finding a master then
|
||||
* we doesn't have the info or the master has failed (or we
|
||||
* reached max_connections or superuser_reserved_connections,
|
||||
* anything else I'm missing?).
|
||||
/*
|
||||
* If we finish this loop without finding a master then we doesn't have
|
||||
* the info or the master has failed (or we reached max_connections or
|
||||
* superuser_reserved_connections, anything else I'm missing?).
|
||||
*
|
||||
* Probably we will need to check the error to know if we need
|
||||
* to start failover procedure or just fix some situation on the
|
||||
* standby.
|
||||
* Probably we will need to check the error to know if we need to start
|
||||
* failover procedure or just fix some situation on the standby.
|
||||
*/
|
||||
PQclear(res1);
|
||||
return NULL;
|
||||
@@ -423,17 +428,19 @@ getMasterConnection(PGconn *standby_conn, char *schema, char *cluster,
|
||||
|
||||
|
||||
/*
|
||||
* wait until current query finishes ignoring any results, this could be an async command
|
||||
* or a cancelation of a query
|
||||
* wait until current query finishes ignoring any results, this could be an
|
||||
* async command or a cancelation of a query
|
||||
* return 1 if Ok; 0 if any error ocurred; -1 if timeout reached
|
||||
*/
|
||||
int
|
||||
wait_connection_availability(PGconn *conn, long long timeout)
|
||||
{
|
||||
PGresult *res;
|
||||
fd_set read_set;
|
||||
int sock = PQsocket(conn);
|
||||
struct timeval tmout, before, after;
|
||||
fd_set read_set;
|
||||
int sock = PQsocket(conn);
|
||||
struct timeval tmout,
|
||||
before,
|
||||
after;
|
||||
struct timezone tz;
|
||||
|
||||
/* recalc to microseconds */
|
||||
@@ -450,10 +457,11 @@ wait_connection_availability(PGconn *conn, long long timeout)
|
||||
|
||||
if (PQisBusy(conn) == 0)
|
||||
{
|
||||
do {
|
||||
do
|
||||
{
|
||||
res = PQgetResult(conn);
|
||||
PQclear(res);
|
||||
} while(res != NULL);
|
||||
} while (res != NULL);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -469,8 +477,8 @@ wait_connection_availability(PGconn *conn, long long timeout)
|
||||
if (select(sock, &read_set, NULL, NULL, &tmout) == -1)
|
||||
{
|
||||
log_warning(
|
||||
_("wait_connection_availability: select() returned with error: %s"),
|
||||
strerror(errno));
|
||||
_("wait_connection_availability: select() returned with error: %s"),
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
gettimeofday(&after, &tz);
|
||||
@@ -493,8 +501,8 @@ wait_connection_availability(PGconn *conn, long long timeout)
|
||||
bool
|
||||
CancelQuery(PGconn *conn, int timeout)
|
||||
{
|
||||
char errbuf[ERRBUFF_SIZE];
|
||||
PGcancel *pgcancel;
|
||||
char errbuf[ERRBUFF_SIZE];
|
||||
PGcancel *pgcancel;
|
||||
|
||||
if (wait_connection_availability(conn, timeout) != 1)
|
||||
return false;
|
||||
@@ -505,9 +513,8 @@ CancelQuery(PGconn *conn, int timeout)
|
||||
return false;
|
||||
|
||||
/*
|
||||
* PQcancel can only return 0 if socket()/connect()/send()
|
||||
* fails, in any of those cases we can assume something
|
||||
* bad happened to the connection
|
||||
* PQcancel can only return 0 if socket()/connect()/send() fails, in any
|
||||
* of those cases we can assume something bad happened to the connection
|
||||
*/
|
||||
if (PQcancel(pgcancel, errbuf, ERRBUFF_SIZE) == 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user