diff --git a/lib/modules/Sql.pmod/pgsql_util.pmod b/lib/modules/Sql.pmod/pgsql_util.pmod
index 3b6f232fe29ac586e476b5487b880cdedf0070fb..85e3fbdb8fb31c645882091e0c670c3fb4709aed 100644
--- a/lib/modules/Sql.pmod/pgsql_util.pmod
+++ b/lib/modules/Sql.pmod/pgsql_util.pmod
@@ -468,14 +468,18 @@ class conxion {
       PD("Nostash locked by %s\n",
        describe_backtrace(nostash->current_locking_thread()->backtrace()));
 #endif
-    if (lock = (waitforreal ? nostash->lock : nostash->trylock)(1)) {
-      stashcount->wait_till_drained();
-#ifdef PG_DEBUGRACE
-      conxsess sess = conxsess(this);
-#endif
+    while (lock = (waitforreal ? nostash->lock : nostash->trylock)(1)) {
       int mode;
       if (sizeof(stash) && (mode = getstash(KEEP)) > KEEP)
         sendcmd(mode);		// Force out stash to the server
+      if (!stashcount->drained()) {
+        lock = 0;				// Unlock while we wait
+        stashcount->wait_till_drained();
+        continue;				// Try again
+      }
+#ifdef PG_DEBUGRACE
+      conxsess sess = conxsess(this);
+#endif
       started = lock;		// sendcmd() clears started, so delay assignment
 #ifdef PG_DEBUGRACE
       return sess;