Reset the connection to Postgres on a 57P01 admin_shutdown error
authorTom Hughes <tom@compton.nu>
Wed, 22 Aug 2012 08:50:52 +0000 (09:50 +0100)
committerTom Hughes <tom@compton.nu>
Wed, 22 Aug 2012 08:50:52 +0000 (09:50 +0100)
src/postgres.c

index 51555b4..1faede6 100644 (file)
@@ -39,20 +39,26 @@ static char escape_buffer[STMT_BUFLEN];
 static PGresult *
 db_execute(const char *statement, ExecStatusType expected)
 {
-   PGresult *result;
+  PGresult *result;
 
-   result = PQexec(handle, statement);
+  result = PQexec(handle, statement);
 
-   if (PQresultStatus(result) != expected) {
-      ERROR("Failure executing PostgreSQL statement: %s: %s",
-            PQresultErrorField(result, PG_DIAG_SQLSTATE),
-            PQresultErrorMessage(result));
+  if (PQresultStatus(result) != expected) {
+    const char *sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
+    ERROR("Failure executing PostgreSQL statement: %s: %s",
+          sqlstate, PQresultErrorMessage(result));
+    if (strcmp(sqlstate, "57P01") != 0) {
+      INFO("Resetting connection to PostgreSQL server");
+      PQclear(result);
+      PQreset(handle);
+    } else {
       PQclear(result);
       PQexec(handle, "ROLLBACK");
-      return NULL;
-   }
+    }
+    return NULL;
+  }
 
-   return result;
+  return result;
 }
 
 /* Postgres has slightly different semantics to MySQL when doing queries