From 72b75a21c6acf0c23d36d5a1c917a91ff97af8dd Mon Sep 17 00:00:00 2001
From: "Stephen R. van den Berg" <srb@cuci.nl>
Date: Mon, 21 May 2018 02:57:17 +0200
Subject: [PATCH] pgsql: Extra diagnostics and release all locks upon async
 query termination.

---
 lib/modules/Sql.pmod/pgsql_util.pmod | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/lib/modules/Sql.pmod/pgsql_util.pmod b/lib/modules/Sql.pmod/pgsql_util.pmod
index fdd19aa21c..ae53380246 100644
--- a/lib/modules/Sql.pmod/pgsql_util.pmod
+++ b/lib/modules/Sql.pmod/pgsql_util.pmod
@@ -469,11 +469,7 @@ class conxion {
 #endif
     while (lock = (waitforreal ? nostash->lock : nostash->trylock)(1)) {
       int mode;
-      if (!stashcount->drained()) {
-        lock = 0;			// Force release before acquiring next
-        stashcount->wait_till_drained();
-        continue;
-      }
+      stashcount->wait_till_drained();
 #ifdef PG_DEBUGRACE
       conxsess sess = conxsess(this);
 #endif
@@ -799,6 +795,7 @@ class sql_result {
                     "query: %O\n"
 #if PG_DEBUGHISTORY > 0
                     "history: %O\n"
+                    "stash: %O\n"
 #endif
                     "fd: %O portalname: %O  datarows: %d"
                     "  synctransact: %d laststatus: %s\n",
@@ -806,6 +803,8 @@ class sql_result {
                     qalreadyprinted == this ? "..." : _query,
 #if PG_DEBUGHISTORY > 0
                     qalreadyprinted == this ? 0 : c && c->i->history,
+                    qalreadyprinted == this ? 0
+                                            : c && (string)c->stash,
 #endif
 		    fd, _portalname,
                     datarowtypes && sizeof(datarowtypes), _synctransact,
@@ -1499,8 +1498,10 @@ class sql_result {
     if (!catch(plugbuffer = c->start()))
       plugbuffer->sendcmd(_closeportal(plugbuffer, reflock));
     reflock = 0;
-    if (_state < CLOSED)
+    if (_state < CLOSED) {
       _state = CLOSED;
+      stmtifkey = 0;
+    }
     datarows->write(1);				// Signal EOF
     releaseconditions();
   }
-- 
GitLab