Fix UNLISTEN to fall out quickly if the current backend has never executed
authorTom Lane <[email protected]>
Fri, 13 Feb 2009 17:12:04 +0000 (17:12 +0000)
committerTom Lane <[email protected]>
Fri, 13 Feb 2009 17:12:04 +0000 (17:12 +0000)
any LISTEN command.  This is more important than it used to be because
DISCARD ALL invokes UNLISTEN.  Connection-pooled applications making heavy
use of DISCARD ALL were seeing significant contention for pg_listener,
as reported by Matteo Beccati.  It seems unlikely that clients using LISTEN
would use pooled connections, so this simple tweak seems sufficient,
especially since the pg_listener implementation is slated to go away soon
anyway.

Back-patch to 8.3, where DISCARD ALL was introduced.

src/backend/commands/async.c

index a6b93e5910fc2ac11e5971c9816670ea1e51ab32..d410b2d47db9ca07e7b485af0a84a30dc558844e 100644 (file)
@@ -277,6 +277,10 @@ Async_Unlisten(const char *relname)
        if (Trace_notify)
                elog(DEBUG1, "Async_Unlisten(%s,%d)", relname, MyProcPid);
 
+       /* If we couldn't possibly be listening, no need to queue anything */
+       if (pendingActions == NIL && !unlistenExitRegistered)
+               return;
+
        queue_listen(LISTEN_UNLISTEN, relname);
 }
 
@@ -291,6 +295,10 @@ Async_UnlistenAll(void)
        if (Trace_notify)
                elog(DEBUG1, "Async_UnlistenAll(%d)", MyProcPid);
 
+       /* If we couldn't possibly be listening, no need to queue anything */
+       if (pendingActions == NIL && !unlistenExitRegistered)
+               return;
+
        queue_listen(LISTEN_UNLISTEN_ALL, "");
 }