Add function test_db_connection()

The difference between this and establish_db_connection() is that
it outputs any connection failure as a [NOTICE] rather than an
[ERROR]; it's intended for use in e.g. polling a server to wait
for it to come up/go down, while preventing [ERROR] log lines
which may cause confusion.
This commit is contained in:
Ian Barwick
2016-01-20 07:56:03 +09:00
parent 995083d66c
commit f982708b35
2 changed files with 31 additions and 4 deletions

View File

@@ -29,8 +29,9 @@
char repmgr_schema[MAXLEN] = "";
char repmgr_schema_quoted[MAXLEN] = "";
PGconn *
establish_db_connection(const char *conninfo, const bool exit_on_error)
_establish_db_connection(const char *conninfo, const bool exit_on_error, const bool log_notice)
{
/* Make a connection to the database */
PGconn *conn = NULL;
@@ -46,8 +47,16 @@ establish_db_connection(const char *conninfo, const bool exit_on_error)
/* 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));
if (log_notice)
{
log_notice(_("connection to database failed: %s\n"),
PQerrorMessage(conn));
}
else
{
log_err(_("connection to database failed: %s\n"),
PQerrorMessage(conn));
}
if (exit_on_error)
{
@@ -59,6 +68,19 @@ establish_db_connection(const char *conninfo, const bool exit_on_error)
return conn;
}
PGconn *
establish_db_connection(const char *conninfo, const bool exit_on_error)
{
return _establish_db_connection(conninfo, exit_on_error, false);
}
PGconn *
test_db_connection(const char *conninfo, const bool exit_on_error)
{
return _establish_db_connection(conninfo, exit_on_error, true);
}
PGconn *
establish_db_connection_by_params(const char *keywords[], const char *values[],
const bool exit_on_error)

View File

@@ -78,8 +78,13 @@ typedef struct s_replication_slot
InvalidXLogRecPtr \
}
PGconn *_establish_db_connection(const char *conninfo,
const bool exit_on_error,
const bool log_notice);
PGconn *establish_db_connection(const char *conninfo,
const bool exit_on_error);
const bool exit_on_error);
PGconn *test_db_connection(const char *conninfo,
const bool exit_on_error);
PGconn *establish_db_connection_by_params(const char *keywords[],
const char *values[],
const bool exit_on_error);