diff --git a/lib/modules/Sql.pmod/pgsql_util.pmod b/lib/modules/Sql.pmod/pgsql_util.pmod
index 8ae289da7bd861a9cca447865e7ea00fb90ee7b4..425724c60cfe694232f3aafa5a99932dbbd4e75e 100644
--- a/lib/modules/Sql.pmod/pgsql_util.pmod
+++ b/lib/modules/Sql.pmod/pgsql_util.pmod
@@ -481,8 +481,9 @@ class conxion {
       PD("Nostash locked by %s\n",
        describe_backtrace(nostash->current_locking_thread()->backtrace()));
 #endif
-    while (lock = (intp(waitforreal) && waitforreal > 0
-                   ? nostash->lock : nostash->trylock)(1)) {
+    while (lock = nostash
+               && ((intp(waitforreal) && waitforreal > 0
+                   ? nostash->lock : nostash->trylock)(1))) {
       int mode;
       if (sizeof(stash) && (mode = getstash(KEEP)) > KEEP)
         sendcmd(mode);		// Force out stash to the server