]> granicus.if.org Git - postgresql/blobdiff - src/backend/commands/async.c
Create an internal semaphore API that is not tied to SysV semaphores.
[postgresql] / src / backend / commands / async.c
index 6333efafc64ea1ce5149cbed4547ca398222aba4..8193174aec946576d9595b627dd0a933c6f8eb61 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/commands/async.c,v 1.80 2001/09/08 01:10:20 tgl Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/commands/async.c,v 1.84 2002/05/05 00:03:28 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -85,6 +85,7 @@
 #include "libpq/libpq.h"
 #include "libpq/pqformat.h"
 #include "miscadmin.h"
+#include "storage/ipc.h"
 #include "tcop/tcopprot.h"
 #include "utils/fmgroids.h"
 #include "utils/ps_status.h"
@@ -120,7 +121,7 @@ static volatile int notifyInterruptOccurred = 0;
 /* True if we've registered an on_shmem_exit cleanup */
 static bool unlistenExitRegistered = false;
 
-bool   Trace_notify = false;
+bool           Trace_notify = false;
 
 
 static void Async_UnlistenAll(void);
@@ -150,16 +151,16 @@ void
 Async_Notify(char *relname)
 {
        if (Trace_notify)
-               elog(DEBUG, "Async_Notify: %s", relname);
+               elog(LOG, "Async_Notify: %s", relname);
 
        /* no point in making duplicate entries in the list ... */
-       if (! AsyncExistsPendingNotify(relname))
+       if (!AsyncExistsPendingNotify(relname))
        {
                /*
-                * The name list needs to live until end of transaction, so
-                * store it in the top transaction context.
+                * The name list needs to live until end of transaction, so store
+                * it in the top transaction context.
                 */
-               MemoryContext   oldcontext;
+               MemoryContext oldcontext;
 
                oldcontext = MemoryContextSwitchTo(TopTransactionContext);
 
@@ -198,7 +199,7 @@ Async_Listen(char *relname, int pid)
        bool            alreadyListener = false;
 
        if (Trace_notify)
-               elog(DEBUG, "Async_Listen: %s", relname);
+               elog(LOG, "Async_Listen: %s", relname);
 
        lRel = heap_openr(ListenerRelationName, AccessExclusiveLock);
 
@@ -209,7 +210,7 @@ Async_Listen(char *relname, int pid)
                Form_pg_listener listener = (Form_pg_listener) GETSTRUCT(tuple);
 
                if (listener->listenerpid == pid &&
-                       strncmp(NameStr(listener->relname), relname, NAMEDATALEN) == 0)
+                 strncmp(NameStr(listener->relname), relname, NAMEDATALEN) == 0)
                {
                        alreadyListener = true;
                        /* No need to scan the rest of the table */
@@ -221,7 +222,7 @@ Async_Listen(char *relname, int pid)
        if (alreadyListener)
        {
                heap_close(lRel, AccessExclusiveLock);
-               elog(NOTICE, "Async_Listen: We are already listening on %s", relname);
+               elog(WARNING, "Async_Listen: We are already listening on %s", relname);
                return;
        }
 
@@ -300,7 +301,7 @@ Async_Unlisten(char *relname, int pid)
        }
 
        if (Trace_notify)
-               elog(DEBUG, "Async_Unlisten %s", relname);
+               elog(LOG, "Async_Unlisten %s", relname);
 
        lRel = heap_openr(ListenerRelationName, AccessExclusiveLock);
 
@@ -310,13 +311,14 @@ Async_Unlisten(char *relname, int pid)
                Form_pg_listener listener = (Form_pg_listener) GETSTRUCT(tuple);
 
                if (listener->listenerpid == pid &&
-                       strncmp(NameStr(listener->relname), relname, NAMEDATALEN) == 0)
+                 strncmp(NameStr(listener->relname), relname, NAMEDATALEN) == 0)
                {
                        /* Found the matching tuple, delete it */
                        simple_heap_delete(lRel, &tuple->t_self);
+
                        /*
-                        * We assume there can be only one match, so no need
-                        * to scan the rest of the table
+                        * We assume there can be only one match, so no need to scan
+                        * the rest of the table
                         */
                        break;
                }
@@ -357,7 +359,7 @@ Async_UnlistenAll(void)
        ScanKeyData key[1];
 
        if (Trace_notify)
-               elog(DEBUG, "Async_UnlistenAll");
+               elog(LOG, "Async_UnlistenAll");
 
        lRel = heap_openr(ListenerRelationName, AccessExclusiveLock);
        tdesc = RelationGetDescr(lRel);
@@ -459,7 +461,7 @@ AtCommit_Notify(void)
        }
 
        if (Trace_notify)
-               elog(DEBUG, "AtCommit_Notify");
+               elog(LOG, "AtCommit_Notify");
 
        /* preset data to update notify column to MyProcPid */
        nulls[0] = nulls[1] = nulls[2] = ' ';
@@ -478,47 +480,45 @@ AtCommit_Notify(void)
                char       *relname = NameStr(listener->relname);
                int32           listenerPID = listener->listenerpid;
 
-               if (! AsyncExistsPendingNotify(relname))
+               if (!AsyncExistsPendingNotify(relname))
                        continue;
 
                if (listenerPID == MyProcPid)
                {
                        /*
-                        * Self-notify: no need to bother with table update.
-                        * Indeed, we *must not* clear the notification field in
-                        * this path, or we could lose an outside notify, which'd
-                        * be bad for applications that ignore self-notify messages.
+                        * Self-notify: no need to bother with table update. Indeed,
+                        * we *must not* clear the notification field in this path, or
+                        * we could lose an outside notify, which'd be bad for
+                        * applications that ignore self-notify messages.
                         */
 
                        if (Trace_notify)
-                               elog(DEBUG, "AtCommit_Notify: notifying self");
+                               elog(LOG, "AtCommit_Notify: notifying self");
 
                        NotifyMyFrontEnd(relname, listenerPID);
                }
                else
                {
                        if (Trace_notify)
-                               elog(DEBUG, "AtCommit_Notify: notifying pid %d",
+                               elog(LOG, "AtCommit_Notify: notifying pid %d",
                                         listenerPID);
 
                        /*
                         * If someone has already notified this listener, we don't
-                        * bother modifying the table, but we do still send a
-                        * SIGUSR2 signal, just in case that backend missed the
-                        * earlier signal for some reason.      It's OK to send the
-                        * signal first, because the other guy can't read
-                        * pg_listener until we unlock it.
+                        * bother modifying the table, but we do still send a SIGUSR2
+                        * signal, just in case that backend missed the earlier signal
+                        * for some reason.  It's OK to send the signal first, because
+                        * the other guy can't read pg_listener until we unlock it.
                         */
                        if (kill(listenerPID, SIGUSR2) < 0)
                        {
                                /*
-                                * Get rid of pg_listener entry if it refers to a PID
-                                * that no longer exists.  Presumably, that backend
-                                * crashed without deleting its pg_listener entries.
-                                * This code used to only delete the entry if
-                                * errno==ESRCH, but as far as I can see we should
-                                * just do it for any failure (certainly at least for
-                                * EPERM too...)
+                                * Get rid of pg_listener entry if it refers to a PID that
+                                * no longer exists.  Presumably, that backend crashed
+                                * without deleting its pg_listener entries. This code
+                                * used to only delete the entry if errno==ESRCH, but as
+                                * far as I can see we should just do it for any failure
+                                * (certainly at least for EPERM too...)
                                 */
                                simple_heap_delete(lRel, &lTuple->t_self);
                        }
@@ -556,7 +556,7 @@ AtCommit_Notify(void)
        ClearPendingNotifies();
 
        if (Trace_notify)
-               elog(DEBUG, "AtCommit_Notify: done");
+               elog(LOG, "AtCommit_Notify: done");
 }
 
 /*
@@ -610,7 +610,6 @@ Async_NotifyHandler(SIGNAL_ARGS)
 
        if (notifyInterruptEnabled)
        {
-
                /*
                 * I'm not sure whether some flavors of Unix might allow another
                 * SIGUSR2 occurrence to recursively interrupt this routine. To
@@ -630,18 +629,17 @@ Async_NotifyHandler(SIGNAL_ARGS)
                        {
                                /* Here, it is finally safe to do stuff. */
                                if (Trace_notify)
-                                       elog(DEBUG, "Async_NotifyHandler: perform async notify");
+                                       elog(LOG, "Async_NotifyHandler: perform async notify");
 
                                ProcessIncomingNotify();
 
                                if (Trace_notify)
-                                       elog(DEBUG, "Async_NotifyHandler: done");
+                                       elog(LOG, "Async_NotifyHandler: done");
                        }
                }
        }
        else
        {
-
                /*
                 * In this path it is NOT SAFE to do much of anything, except
                 * this:
@@ -703,12 +701,12 @@ EnableNotifyInterrupt(void)
                if (notifyInterruptOccurred)
                {
                        if (Trace_notify)
-                               elog(DEBUG, "EnableNotifyInterrupt: perform async notify");
+                               elog(LOG, "EnableNotifyInterrupt: perform async notify");
 
                        ProcessIncomingNotify();
 
                        if (Trace_notify)
-                               elog(DEBUG, "EnableNotifyInterrupt: done");
+                               elog(LOG, "EnableNotifyInterrupt: done");
                }
        }
 }
@@ -759,7 +757,7 @@ ProcessIncomingNotify(void)
                                nulls[Natts_pg_listener];
 
        if (Trace_notify)
-               elog(DEBUG, "ProcessIncomingNotify");
+               elog(LOG, "ProcessIncomingNotify");
 
        set_ps_display("async_notify");
 
@@ -795,7 +793,7 @@ ProcessIncomingNotify(void)
                        /* Notify the frontend */
 
                        if (Trace_notify)
-                               elog(DEBUG, "ProcessIncomingNotify: received %s from %d",
+                               elog(LOG, "ProcessIncomingNotify: received %s from %d",
                                         relname, (int) sourcePID);
 
                        NotifyMyFrontEnd(relname, sourcePID);
@@ -837,7 +835,7 @@ ProcessIncomingNotify(void)
        set_ps_display("idle");
 
        if (Trace_notify)
-               elog(DEBUG, "ProcessIncomingNotify: done");
+               elog(LOG, "ProcessIncomingNotify: done");
 }
 
 /*
@@ -864,7 +862,7 @@ NotifyMyFrontEnd(char *relname, int32 listenerPID)
                 */
        }
        else
-               elog(NOTICE, "NOTIFY for %s", relname);
+               elog(INFO, "NOTIFY for %s", relname);
 }
 
 /* Does pendingNotifies include the given relname? */
@@ -888,11 +886,11 @@ static void
 ClearPendingNotifies(void)
 {
        /*
-        * We used to have to explicitly deallocate the list members and nodes,
-        * because they were malloc'd.  Now, since we know they are palloc'd
-        * in TopTransactionContext, we need not do that --- they'll go away
-        * automatically at transaction exit.  We need only reset the list head
-        * pointer.
+        * We used to have to explicitly deallocate the list members and
+        * nodes, because they were malloc'd.  Now, since we know they are
+        * palloc'd in TopTransactionContext, we need not do that --- they'll
+        * go away automatically at transaction exit.  We need only reset the
+        * list head pointer.
         */
        pendingNotifies = NIL;
 }