check pg connection when preparing the resource

Sun, 07 Dec 2025 17:58:47 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 07 Dec 2025 17:58:47 +0100
changeset 655
337cda289f34
parent 654
be69e8196cc9
child 656
59dd1fb27639

check pg connection when preparing the resource

src/server/daemon/resourcepool.c file | annotate | diff | comparison | revisions
src/server/plugins/postgresql/resource.c file | annotate | diff | comparison | revisions
--- a/src/server/daemon/resourcepool.c	Sun Dec 07 17:50:52 2025 +0100
+++ b/src/server/daemon/resourcepool.c	Sun Dec 07 17:58:47 2025 +0100
@@ -201,8 +201,7 @@
     }
     
     if(err) {
-        // err == 1 caused by OOM
-        log_ereport(LOG_FAILURE, "resourcepool_lookup: OOM");
+        log_ereport(LOG_FAILURE, "resourcepool_lookup error");
         // cleanup
         resourcepool_destroy_resource(resource);
         resource = NULL;
--- a/src/server/plugins/postgresql/resource.c	Sun Dec 07 17:50:52 2025 +0100
+++ b/src/server/plugins/postgresql/resource.c	Sun Dec 07 17:58:47 2025 +0100
@@ -118,6 +118,16 @@
 }
 
 int pg_resourcepool_prepare(PgResourcePool *pg, PgResource *res) {
+    if(PQstatus(res->connection) != CONNECTION_OK) {
+        log_ereport(LOG_WARN, "pg_resourcepool_prepare: connection not ok");
+        // try to recreate the resource
+        PQfinish(res->connection);
+        res->connection = PQconnectdb(pg->connection);
+        if(pg_check_connection(LOG_FAILURE, pg->name, res->connection)) {
+            return 1;
+        }
+    }
+    
     PGresult *result = PQexec(res->connection, "BEGIN");
     PQclear(result); // TODO: handle error
     return 0;

mercurial