Skip to content
This repository has been archived by the owner on Mar 12, 2019. It is now read-only.

Try postgres reconnect on database connection lost #339

Merged
merged 4 commits into from
Apr 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions be-postgres.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ int be_pg_getuser(void *handle, const char *username, const char *password, char

if (PQresultStatus(res) != PGRES_TUPLES_OK) {
_log(LOG_DEBUG, "%s\n", PQresultErrorMessage(res));
if(PQstatus(conf->conn) == CONNECTION_BAD){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if this was executed prior to running a query - will that still work?

Copy link
Author

@jklimke jklimke Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if i understand your comment correctly. How should that be possible at that position ? I understand that all postgres queries are currently performed synchronously. That means that the query has been finished (not sucessfully) when this piece of code comes into play.

On line 174 there is a check if the database connection has been initialized

if (!conf || !conf->userquery || !username || !*username)
		return BACKEND_DEFER;

That means a connection should exist a the moment and a query has finished

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked at the source, and you are of course correct.

_log(LOG_NOTICE, "Noticed a postgres connection loss. Trying to reconnect ...\n");
//try to reinitiate the database connection
PQreset(conf->conn);
}

goto out;
}
if ((nrows = PQntuples(res)) != 1) {
Expand All @@ -194,7 +200,7 @@ int be_pg_getuser(void *handle, const char *username, const char *password, char
}
if ((v = PQgetvalue(res, 0, 0)) == NULL) {
goto out;
}
}
value = (v) ? strdup(v) : NULL;


Expand Down Expand Up @@ -233,6 +239,13 @@ int be_pg_superuser(void *handle, const char *username)
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "%s\n", PQresultErrorMessage(res));
issuper = BACKEND_ERROR;
//try to reset connection if failing because of database connection lost
if(PQstatus(conf->conn) == CONNECTION_BAD){
_log(LOG_NOTICE, "Noticed a postgres connection loss. Trying to reconnect ...\n");
//try to reinitiate the database connection
PQreset(conf->conn);
}

goto out;
}
if ((nrows = PQntuples(res)) != 1) {
Expand Down Expand Up @@ -296,6 +309,14 @@ int be_pg_aclcheck(void *handle, const char *clientid, const char *username, con
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "%s\n", PQresultErrorMessage(res));
match = BACKEND_ERROR;

//try to reset connection if failing because of database connection lost
if(PQstatus(conf->conn) == CONNECTION_BAD){
_log(LOG_NOTICE, "Noticed a postgres connection loss. Trying to reconnect ...\n");
//try to reinitiate the database connection
PQreset(conf->conn);
}

goto out;
}
if (PQnfields(res) != 1) {
Expand Down Expand Up @@ -413,8 +434,8 @@ int addKeyValue(char **keywords, char **values, char *key, char *value,
n++;

// In case of not zero-terminated dictionary
keywords[n] = '\0';
values[n] = '\0';
keywords[n] = 0;
values[n] = 0;

return n;
}
Expand Down