]> granicus.if.org Git - postgresql/blob - src/backend/commands/async.c
Update copyright for 2016
[postgresql] / src / backend / commands / async.c
1 /*-------------------------------------------------------------------------
2  *
3  * async.c
4  *        Asynchronous notification: NOTIFY, LISTEN, UNLISTEN
5  *
6  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  *        src/backend/commands/async.c
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 /*-------------------------------------------------------------------------
16  * Async Notification Model as of 9.0:
17  *
18  * 1. Multiple backends on same machine. Multiple backends listening on
19  *        several channels. (Channels are also called "conditions" in other
20  *        parts of the code.)
21  *
22  * 2. There is one central queue in disk-based storage (directory pg_notify/),
23  *        with actively-used pages mapped into shared memory by the slru.c module.
24  *        All notification messages are placed in the queue and later read out
25  *        by listening backends.
26  *
27  *        There is no central knowledge of which backend listens on which channel;
28  *        every backend has its own list of interesting channels.
29  *
30  *        Although there is only one queue, notifications are treated as being
31  *        database-local; this is done by including the sender's database OID
32  *        in each notification message.  Listening backends ignore messages
33  *        that don't match their database OID.  This is important because it
34  *        ensures senders and receivers have the same database encoding and won't
35  *        misinterpret non-ASCII text in the channel name or payload string.
36  *
37  *        Since notifications are not expected to survive database crashes,
38  *        we can simply clean out the pg_notify data at any reboot, and there
39  *        is no need for WAL support or fsync'ing.
40  *
41  * 3. Every backend that is listening on at least one channel registers by
42  *        entering its PID into the array in AsyncQueueControl. It then scans all
43  *        incoming notifications in the central queue and first compares the
44  *        database OID of the notification with its own database OID and then
45  *        compares the notified channel with the list of channels that it listens
46  *        to. In case there is a match it delivers the notification event to its
47  *        frontend.  Non-matching events are simply skipped.
48  *
49  * 4. The NOTIFY statement (routine Async_Notify) stores the notification in
50  *        a backend-local list which will not be processed until transaction end.
51  *
52  *        Duplicate notifications from the same transaction are sent out as one
53  *        notification only. This is done to save work when for example a trigger
54  *        on a 2 million row table fires a notification for each row that has been
55  *        changed. If the application needs to receive every single notification
56  *        that has been sent, it can easily add some unique string into the extra
57  *        payload parameter.
58  *
59  *        When the transaction is ready to commit, PreCommit_Notify() adds the
60  *        pending notifications to the head of the queue. The head pointer of the
61  *        queue always points to the next free position and a position is just a
62  *        page number and the offset in that page. This is done before marking the
63  *        transaction as committed in clog. If we run into problems writing the
64  *        notifications, we can still call elog(ERROR, ...) and the transaction
65  *        will roll back.
66  *
67  *        Once we have put all of the notifications into the queue, we return to
68  *        CommitTransaction() which will then do the actual transaction commit.
69  *
70  *        After commit we are called another time (AtCommit_Notify()). Here we
71  *        make the actual updates to the effective listen state (listenChannels).
72  *
73  *        Finally, after we are out of the transaction altogether, we check if
74  *        we need to signal listening backends.  In SignalBackends() we scan the
75  *        list of listening backends and send a PROCSIG_NOTIFY_INTERRUPT signal
76  *        to every listening backend (we don't know which backend is listening on
77  *        which channel so we must signal them all). We can exclude backends that
78  *        are already up to date, though.  We don't bother with a self-signal
79  *        either, but just process the queue directly.
80  *
81  * 5. Upon receipt of a PROCSIG_NOTIFY_INTERRUPT signal, the signal handler
82  *        sets the process's latch, which triggers the event to be processed
83  *        immediately if this backend is idle (i.e., it is waiting for a frontend
84  *        command and is not within a transaction block. C.f.
85  *        ProcessClientReadInterrupt()).  Otherwise the handler may only set a
86  *        flag, which will cause the processing to occur just before we next go
87  *        idle.
88  *
89  *        Inbound-notify processing consists of reading all of the notifications
90  *        that have arrived since scanning last time. We read every notification
91  *        until we reach either a notification from an uncommitted transaction or
92  *        the head pointer's position. Then we check if we were the laziest
93  *        backend: if our pointer is set to the same position as the global tail
94  *        pointer is set, then we move the global tail pointer ahead to where the
95  *        second-laziest backend is (in general, we take the MIN of the current
96  *        head position and all active backends' new tail pointers). Whenever we
97  *        move the global tail pointer we also truncate now-unused pages (i.e.,
98  *        delete files in pg_notify/ that are no longer used).
99  *
100  * An application that listens on the same channel it notifies will get
101  * NOTIFY messages for its own NOTIFYs.  These can be ignored, if not useful,
102  * by comparing be_pid in the NOTIFY message to the application's own backend's
103  * PID.  (As of FE/BE protocol 2.0, the backend's PID is provided to the
104  * frontend during startup.)  The above design guarantees that notifies from
105  * other backends will never be missed by ignoring self-notifies.
106  *
107  * The amount of shared memory used for notify management (NUM_ASYNC_BUFFERS)
108  * can be varied without affecting anything but performance.  The maximum
109  * amount of notification data that can be queued at one time is determined
110  * by slru.c's wraparound limit; see QUEUE_MAX_PAGE below.
111  *-------------------------------------------------------------------------
112  */
113
114 #include "postgres.h"
115
116 #include <limits.h>
117 #include <unistd.h>
118 #include <signal.h>
119
120 #include "access/parallel.h"
121 #include "access/slru.h"
122 #include "access/transam.h"
123 #include "access/xact.h"
124 #include "catalog/pg_database.h"
125 #include "commands/async.h"
126 #include "funcapi.h"
127 #include "libpq/libpq.h"
128 #include "libpq/pqformat.h"
129 #include "miscadmin.h"
130 #include "storage/ipc.h"
131 #include "storage/lmgr.h"
132 #include "storage/proc.h"
133 #include "storage/procarray.h"
134 #include "storage/procsignal.h"
135 #include "storage/sinval.h"
136 #include "tcop/tcopprot.h"
137 #include "utils/builtins.h"
138 #include "utils/memutils.h"
139 #include "utils/ps_status.h"
140 #include "utils/timestamp.h"
141
142
143 /*
144  * Maximum size of a NOTIFY payload, including terminating NULL.  This
145  * must be kept small enough so that a notification message fits on one
146  * SLRU page.  The magic fudge factor here is noncritical as long as it's
147  * more than AsyncQueueEntryEmptySize --- we make it significantly bigger
148  * than that, so changes in that data structure won't affect user-visible
149  * restrictions.
150  */
151 #define NOTIFY_PAYLOAD_MAX_LENGTH       (BLCKSZ - NAMEDATALEN - 128)
152
153 /*
154  * Struct representing an entry in the global notify queue
155  *
156  * This struct declaration has the maximal length, but in a real queue entry
157  * the data area is only big enough for the actual channel and payload strings
158  * (each null-terminated).  AsyncQueueEntryEmptySize is the minimum possible
159  * entry size, if both channel and payload strings are empty (but note it
160  * doesn't include alignment padding).
161  *
162  * The "length" field should always be rounded up to the next QUEUEALIGN
163  * multiple so that all fields are properly aligned.
164  */
165 typedef struct AsyncQueueEntry
166 {
167         int                     length;                 /* total allocated length of entry */
168         Oid                     dboid;                  /* sender's database OID */
169         TransactionId xid;                      /* sender's XID */
170         int32           srcPid;                 /* sender's PID */
171         char            data[NAMEDATALEN + NOTIFY_PAYLOAD_MAX_LENGTH];
172 } AsyncQueueEntry;
173
174 /* Currently, no field of AsyncQueueEntry requires more than int alignment */
175 #define QUEUEALIGN(len)         INTALIGN(len)
176
177 #define AsyncQueueEntryEmptySize        (offsetof(AsyncQueueEntry, data) + 2)
178
179 /*
180  * Struct describing a queue position, and assorted macros for working with it
181  */
182 typedef struct QueuePosition
183 {
184         int                     page;                   /* SLRU page number */
185         int                     offset;                 /* byte offset within page */
186 } QueuePosition;
187
188 #define QUEUE_POS_PAGE(x)               ((x).page)
189 #define QUEUE_POS_OFFSET(x)             ((x).offset)
190
191 #define SET_QUEUE_POS(x,y,z) \
192         do { \
193                 (x).page = (y); \
194                 (x).offset = (z); \
195         } while (0)
196
197 #define QUEUE_POS_EQUAL(x,y) \
198          ((x).page == (y).page && (x).offset == (y).offset)
199
200 /* choose logically smaller QueuePosition */
201 #define QUEUE_POS_MIN(x,y) \
202         (asyncQueuePagePrecedes((x).page, (y).page) ? (x) : \
203          (x).page != (y).page ? (y) : \
204          (x).offset < (y).offset ? (x) : (y))
205
206 /* choose logically larger QueuePosition */
207 #define QUEUE_POS_MAX(x,y) \
208         (asyncQueuePagePrecedes((x).page, (y).page) ? (y) : \
209          (x).page != (y).page ? (x) : \
210          (x).offset > (y).offset ? (x) : (y))
211
212 /*
213  * Struct describing a listening backend's status
214  */
215 typedef struct QueueBackendStatus
216 {
217         int32           pid;                    /* either a PID or InvalidPid */
218         Oid                     dboid;                  /* backend's database OID, or InvalidOid */
219         QueuePosition pos;                      /* backend has read queue up to here */
220 } QueueBackendStatus;
221
222 /*
223  * Shared memory state for LISTEN/NOTIFY (excluding its SLRU stuff)
224  *
225  * The AsyncQueueControl structure is protected by the AsyncQueueLock.
226  *
227  * When holding the lock in SHARED mode, backends may only inspect their own
228  * entries as well as the head and tail pointers. Consequently we can allow a
229  * backend to update its own record while holding only SHARED lock (since no
230  * other backend will inspect it).
231  *
232  * When holding the lock in EXCLUSIVE mode, backends can inspect the entries
233  * of other backends and also change the head and tail pointers.
234  *
235  * AsyncCtlLock is used as the control lock for the pg_notify SLRU buffers.
236  * In order to avoid deadlocks, whenever we need both locks, we always first
237  * get AsyncQueueLock and then AsyncCtlLock.
238  *
239  * Each backend uses the backend[] array entry with index equal to its
240  * BackendId (which can range from 1 to MaxBackends).  We rely on this to make
241  * SendProcSignal fast.
242  */
243 typedef struct AsyncQueueControl
244 {
245         QueuePosition head;                     /* head points to the next free location */
246         QueuePosition tail;                     /* the global tail is equivalent to the pos of
247                                                                  * the "slowest" backend */
248         TimestampTz lastQueueFillWarn;          /* time of last queue-full msg */
249         QueueBackendStatus backend[FLEXIBLE_ARRAY_MEMBER];
250         /* backend[0] is not used; used entries are from [1] to [MaxBackends] */
251 } AsyncQueueControl;
252
253 static AsyncQueueControl *asyncQueueControl;
254
255 #define QUEUE_HEAD                                      (asyncQueueControl->head)
256 #define QUEUE_TAIL                                      (asyncQueueControl->tail)
257 #define QUEUE_BACKEND_PID(i)            (asyncQueueControl->backend[i].pid)
258 #define QUEUE_BACKEND_DBOID(i)          (asyncQueueControl->backend[i].dboid)
259 #define QUEUE_BACKEND_POS(i)            (asyncQueueControl->backend[i].pos)
260
261 /*
262  * The SLRU buffer area through which we access the notification queue
263  */
264 static SlruCtlData AsyncCtlData;
265
266 #define AsyncCtl                                        (&AsyncCtlData)
267 #define QUEUE_PAGESIZE                          BLCKSZ
268 #define QUEUE_FULL_WARN_INTERVAL        5000            /* warn at most once every 5s */
269
270 /*
271  * slru.c currently assumes that all filenames are four characters of hex
272  * digits. That means that we can use segments 0000 through FFFF.
273  * Each segment contains SLRU_PAGES_PER_SEGMENT pages which gives us
274  * the pages from 0 to SLRU_PAGES_PER_SEGMENT * 0x10000 - 1.
275  *
276  * It's of course possible to enhance slru.c, but this gives us so much
277  * space already that it doesn't seem worth the trouble.
278  *
279  * The most data we can have in the queue at a time is QUEUE_MAX_PAGE/2
280  * pages, because more than that would confuse slru.c into thinking there
281  * was a wraparound condition.  With the default BLCKSZ this means there
282  * can be up to 8GB of queued-and-not-read data.
283  *
284  * Note: it's possible to redefine QUEUE_MAX_PAGE with a smaller multiple of
285  * SLRU_PAGES_PER_SEGMENT, for easier testing of queue-full behaviour.
286  */
287 #define QUEUE_MAX_PAGE                  (SLRU_PAGES_PER_SEGMENT * 0x10000 - 1)
288
289 /*
290  * listenChannels identifies the channels we are actually listening to
291  * (ie, have committed a LISTEN on).  It is a simple list of channel names,
292  * allocated in TopMemoryContext.
293  */
294 static List *listenChannels = NIL;              /* list of C strings */
295
296 /*
297  * State for pending LISTEN/UNLISTEN actions consists of an ordered list of
298  * all actions requested in the current transaction.  As explained above,
299  * we don't actually change listenChannels until we reach transaction commit.
300  *
301  * The list is kept in CurTransactionContext.  In subtransactions, each
302  * subtransaction has its own list in its own CurTransactionContext, but
303  * successful subtransactions attach their lists to their parent's list.
304  * Failed subtransactions simply discard their lists.
305  */
306 typedef enum
307 {
308         LISTEN_LISTEN,
309         LISTEN_UNLISTEN,
310         LISTEN_UNLISTEN_ALL
311 } ListenActionKind;
312
313 typedef struct
314 {
315         ListenActionKind action;
316         char            channel[FLEXIBLE_ARRAY_MEMBER]; /* nul-terminated string */
317 } ListenAction;
318
319 static List *pendingActions = NIL;              /* list of ListenAction */
320
321 static List *upperPendingActions = NIL; /* list of upper-xact lists */
322
323 /*
324  * State for outbound notifies consists of a list of all channels+payloads
325  * NOTIFYed in the current transaction. We do not actually perform a NOTIFY
326  * until and unless the transaction commits.  pendingNotifies is NIL if no
327  * NOTIFYs have been done in the current transaction.
328  *
329  * The list is kept in CurTransactionContext.  In subtransactions, each
330  * subtransaction has its own list in its own CurTransactionContext, but
331  * successful subtransactions attach their lists to their parent's list.
332  * Failed subtransactions simply discard their lists.
333  *
334  * Note: the action and notify lists do not interact within a transaction.
335  * In particular, if a transaction does NOTIFY and then LISTEN on the same
336  * condition name, it will get a self-notify at commit.  This is a bit odd
337  * but is consistent with our historical behavior.
338  */
339 typedef struct Notification
340 {
341         char       *channel;            /* channel name */
342         char       *payload;            /* payload string (can be empty) */
343 } Notification;
344
345 static List *pendingNotifies = NIL;             /* list of Notifications */
346
347 static List *upperPendingNotifies = NIL;                /* list of upper-xact lists */
348
349 /*
350  * Inbound notifications are initially processed by HandleNotifyInterrupt(),
351  * called from inside a signal handler. That just sets the
352  * notifyInterruptPending flag and sets the process
353  * latch. ProcessNotifyInterrupt() will then be called whenever it's safe to
354  * actually deal with the interrupt.
355  */
356 volatile sig_atomic_t notifyInterruptPending = false;
357
358 /* True if we've registered an on_shmem_exit cleanup */
359 static bool unlistenExitRegistered = false;
360
361 /* True if we're currently registered as a listener in asyncQueueControl */
362 static bool amRegisteredListener = false;
363
364 /* has this backend sent notifications in the current transaction? */
365 static bool backendHasSentNotifications = false;
366
367 /* GUC parameter */
368 bool            Trace_notify = false;
369
370 /* local function prototypes */
371 static bool asyncQueuePagePrecedes(int p, int q);
372 static void queue_listen(ListenActionKind action, const char *channel);
373 static void Async_UnlistenOnExit(int code, Datum arg);
374 static void Exec_ListenPreCommit(void);
375 static void Exec_ListenCommit(const char *channel);
376 static void Exec_UnlistenCommit(const char *channel);
377 static void Exec_UnlistenAllCommit(void);
378 static bool IsListeningOn(const char *channel);
379 static void asyncQueueUnregister(void);
380 static bool asyncQueueIsFull(void);
381 static bool asyncQueueAdvance(volatile QueuePosition *position, int entryLength);
382 static void asyncQueueNotificationToEntry(Notification *n, AsyncQueueEntry *qe);
383 static ListCell *asyncQueueAddEntries(ListCell *nextNotify);
384 static double asyncQueueUsage(void);
385 static void asyncQueueFillWarning(void);
386 static bool SignalBackends(void);
387 static void asyncQueueReadAllNotifications(void);
388 static bool asyncQueueProcessPageEntries(volatile QueuePosition *current,
389                                                          QueuePosition stop,
390                                                          char *page_buffer);
391 static void asyncQueueAdvanceTail(void);
392 static void ProcessIncomingNotify(void);
393 static void NotifyMyFrontEnd(const char *channel,
394                                  const char *payload,
395                                  int32 srcPid);
396 static bool AsyncExistsPendingNotify(const char *channel, const char *payload);
397 static void ClearPendingActionsAndNotifies(void);
398
399 /*
400  * We will work on the page range of 0..QUEUE_MAX_PAGE.
401  */
402 static bool
403 asyncQueuePagePrecedes(int p, int q)
404 {
405         int                     diff;
406
407         /*
408          * We have to compare modulo (QUEUE_MAX_PAGE+1)/2.  Both inputs should be
409          * in the range 0..QUEUE_MAX_PAGE.
410          */
411         Assert(p >= 0 && p <= QUEUE_MAX_PAGE);
412         Assert(q >= 0 && q <= QUEUE_MAX_PAGE);
413
414         diff = p - q;
415         if (diff >= ((QUEUE_MAX_PAGE + 1) / 2))
416                 diff -= QUEUE_MAX_PAGE + 1;
417         else if (diff < -((QUEUE_MAX_PAGE + 1) / 2))
418                 diff += QUEUE_MAX_PAGE + 1;
419         return diff < 0;
420 }
421
422 /*
423  * Report space needed for our shared memory area
424  */
425 Size
426 AsyncShmemSize(void)
427 {
428         Size            size;
429
430         /* This had better match AsyncShmemInit */
431         size = mul_size(MaxBackends + 1, sizeof(QueueBackendStatus));
432         size = add_size(size, offsetof(AsyncQueueControl, backend));
433
434         size = add_size(size, SimpleLruShmemSize(NUM_ASYNC_BUFFERS, 0));
435
436         return size;
437 }
438
439 /*
440  * Initialize our shared memory area
441  */
442 void
443 AsyncShmemInit(void)
444 {
445         bool            found;
446         int                     slotno;
447         Size            size;
448
449         /*
450          * Create or attach to the AsyncQueueControl structure.
451          *
452          * The used entries in the backend[] array run from 1 to MaxBackends; the
453          * zero'th entry is unused but must be allocated.
454          */
455         size = mul_size(MaxBackends + 1, sizeof(QueueBackendStatus));
456         size = add_size(size, offsetof(AsyncQueueControl, backend));
457
458         asyncQueueControl = (AsyncQueueControl *)
459                 ShmemInitStruct("Async Queue Control", size, &found);
460
461         if (!found)
462         {
463                 /* First time through, so initialize it */
464                 int                     i;
465
466                 SET_QUEUE_POS(QUEUE_HEAD, 0, 0);
467                 SET_QUEUE_POS(QUEUE_TAIL, 0, 0);
468                 asyncQueueControl->lastQueueFillWarn = 0;
469                 /* zero'th entry won't be used, but let's initialize it anyway */
470                 for (i = 0; i <= MaxBackends; i++)
471                 {
472                         QUEUE_BACKEND_PID(i) = InvalidPid;
473                         QUEUE_BACKEND_DBOID(i) = InvalidOid;
474                         SET_QUEUE_POS(QUEUE_BACKEND_POS(i), 0, 0);
475                 }
476         }
477
478         /*
479          * Set up SLRU management of the pg_notify data.
480          */
481         AsyncCtl->PagePrecedes = asyncQueuePagePrecedes;
482         SimpleLruInit(AsyncCtl, "async", NUM_ASYNC_BUFFERS, 0,
483                                   AsyncCtlLock, "pg_notify");
484         /* Override default assumption that writes should be fsync'd */
485         AsyncCtl->do_fsync = false;
486
487         if (!found)
488         {
489                 /*
490                  * During start or reboot, clean out the pg_notify directory.
491                  */
492                 (void) SlruScanDirectory(AsyncCtl, SlruScanDirCbDeleteAll, NULL);
493
494                 /* Now initialize page zero to empty */
495                 LWLockAcquire(AsyncCtlLock, LW_EXCLUSIVE);
496                 slotno = SimpleLruZeroPage(AsyncCtl, QUEUE_POS_PAGE(QUEUE_HEAD));
497                 /* This write is just to verify that pg_notify/ is writable */
498                 SimpleLruWritePage(AsyncCtl, slotno);
499                 LWLockRelease(AsyncCtlLock);
500         }
501 }
502
503
504 /*
505  * pg_notify -
506  *        SQL function to send a notification event
507  */
508 Datum
509 pg_notify(PG_FUNCTION_ARGS)
510 {
511         const char *channel;
512         const char *payload;
513
514         if (PG_ARGISNULL(0))
515                 channel = "";
516         else
517                 channel = text_to_cstring(PG_GETARG_TEXT_PP(0));
518
519         if (PG_ARGISNULL(1))
520                 payload = "";
521         else
522                 payload = text_to_cstring(PG_GETARG_TEXT_PP(1));
523
524         /* For NOTIFY as a statement, this is checked in ProcessUtility */
525         PreventCommandDuringRecovery("NOTIFY");
526
527         Async_Notify(channel, payload);
528
529         PG_RETURN_VOID();
530 }
531
532
533 /*
534  * Async_Notify
535  *
536  *              This is executed by the SQL notify command.
537  *
538  *              Adds the message to the list of pending notifies.
539  *              Actual notification happens during transaction commit.
540  *              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
541  */
542 void
543 Async_Notify(const char *channel, const char *payload)
544 {
545         Notification *n;
546         MemoryContext oldcontext;
547
548         if (IsParallelWorker())
549                 elog(ERROR, "cannot send notifications from a parallel worker");
550
551         if (Trace_notify)
552                 elog(DEBUG1, "Async_Notify(%s)", channel);
553
554         /* a channel name must be specified */
555         if (!channel || !strlen(channel))
556                 ereport(ERROR,
557                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
558                                  errmsg("channel name cannot be empty")));
559
560         if (strlen(channel) >= NAMEDATALEN)
561                 ereport(ERROR,
562                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
563                                  errmsg("channel name too long")));
564
565         if (payload)
566         {
567                 if (strlen(payload) >= NOTIFY_PAYLOAD_MAX_LENGTH)
568                         ereport(ERROR,
569                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
570                                          errmsg("payload string too long")));
571         }
572
573         /* no point in making duplicate entries in the list ... */
574         if (AsyncExistsPendingNotify(channel, payload))
575                 return;
576
577         /*
578          * The notification list needs to live until end of transaction, so store
579          * it in the transaction context.
580          */
581         oldcontext = MemoryContextSwitchTo(CurTransactionContext);
582
583         n = (Notification *) palloc(sizeof(Notification));
584         n->channel = pstrdup(channel);
585         if (payload)
586                 n->payload = pstrdup(payload);
587         else
588                 n->payload = "";
589
590         /*
591          * We want to preserve the order so we need to append every notification.
592          * See comments at AsyncExistsPendingNotify().
593          */
594         pendingNotifies = lappend(pendingNotifies, n);
595
596         MemoryContextSwitchTo(oldcontext);
597 }
598
599 /*
600  * queue_listen
601  *              Common code for listen, unlisten, unlisten all commands.
602  *
603  *              Adds the request to the list of pending actions.
604  *              Actual update of the listenChannels list happens during transaction
605  *              commit.
606  */
607 static void
608 queue_listen(ListenActionKind action, const char *channel)
609 {
610         MemoryContext oldcontext;
611         ListenAction *actrec;
612
613         /*
614          * Unlike Async_Notify, we don't try to collapse out duplicates. It would
615          * be too complicated to ensure we get the right interactions of
616          * conflicting LISTEN/UNLISTEN/UNLISTEN_ALL, and it's unlikely that there
617          * would be any performance benefit anyway in sane applications.
618          */
619         oldcontext = MemoryContextSwitchTo(CurTransactionContext);
620
621         /* space for terminating null is included in sizeof(ListenAction) */
622         actrec = (ListenAction *) palloc(offsetof(ListenAction, channel) +
623                                                                          strlen(channel) + 1);
624         actrec->action = action;
625         strcpy(actrec->channel, channel);
626
627         pendingActions = lappend(pendingActions, actrec);
628
629         MemoryContextSwitchTo(oldcontext);
630 }
631
632 /*
633  * Async_Listen
634  *
635  *              This is executed by the SQL listen command.
636  */
637 void
638 Async_Listen(const char *channel)
639 {
640         if (Trace_notify)
641                 elog(DEBUG1, "Async_Listen(%s,%d)", channel, MyProcPid);
642
643         queue_listen(LISTEN_LISTEN, channel);
644 }
645
646 /*
647  * Async_Unlisten
648  *
649  *              This is executed by the SQL unlisten command.
650  */
651 void
652 Async_Unlisten(const char *channel)
653 {
654         if (Trace_notify)
655                 elog(DEBUG1, "Async_Unlisten(%s,%d)", channel, MyProcPid);
656
657         /* If we couldn't possibly be listening, no need to queue anything */
658         if (pendingActions == NIL && !unlistenExitRegistered)
659                 return;
660
661         queue_listen(LISTEN_UNLISTEN, channel);
662 }
663
664 /*
665  * Async_UnlistenAll
666  *
667  *              This is invoked by UNLISTEN * command, and also at backend exit.
668  */
669 void
670 Async_UnlistenAll(void)
671 {
672         if (Trace_notify)
673                 elog(DEBUG1, "Async_UnlistenAll(%d)", MyProcPid);
674
675         /* If we couldn't possibly be listening, no need to queue anything */
676         if (pendingActions == NIL && !unlistenExitRegistered)
677                 return;
678
679         queue_listen(LISTEN_UNLISTEN_ALL, "");
680 }
681
682 /*
683  * SQL function: return a set of the channel names this backend is actively
684  * listening to.
685  *
686  * Note: this coding relies on the fact that the listenChannels list cannot
687  * change within a transaction.
688  */
689 Datum
690 pg_listening_channels(PG_FUNCTION_ARGS)
691 {
692         FuncCallContext *funcctx;
693         ListCell  **lcp;
694
695         /* stuff done only on the first call of the function */
696         if (SRF_IS_FIRSTCALL())
697         {
698                 MemoryContext oldcontext;
699
700                 /* create a function context for cross-call persistence */
701                 funcctx = SRF_FIRSTCALL_INIT();
702
703                 /* switch to memory context appropriate for multiple function calls */
704                 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
705
706                 /* allocate memory for user context */
707                 lcp = (ListCell **) palloc(sizeof(ListCell *));
708                 *lcp = list_head(listenChannels);
709                 funcctx->user_fctx = (void *) lcp;
710
711                 MemoryContextSwitchTo(oldcontext);
712         }
713
714         /* stuff done on every call of the function */
715         funcctx = SRF_PERCALL_SETUP();
716         lcp = (ListCell **) funcctx->user_fctx;
717
718         while (*lcp != NULL)
719         {
720                 char       *channel = (char *) lfirst(*lcp);
721
722                 *lcp = lnext(*lcp);
723                 SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(channel));
724         }
725
726         SRF_RETURN_DONE(funcctx);
727 }
728
729 /*
730  * Async_UnlistenOnExit
731  *
732  * This is executed at backend exit if we have done any LISTENs in this
733  * backend.  It might not be necessary anymore, if the user UNLISTENed
734  * everything, but we don't try to detect that case.
735  */
736 static void
737 Async_UnlistenOnExit(int code, Datum arg)
738 {
739         Exec_UnlistenAllCommit();
740         asyncQueueUnregister();
741 }
742
743 /*
744  * AtPrepare_Notify
745  *
746  *              This is called at the prepare phase of a two-phase
747  *              transaction.  Save the state for possible commit later.
748  */
749 void
750 AtPrepare_Notify(void)
751 {
752         /* It's not allowed to have any pending LISTEN/UNLISTEN/NOTIFY actions */
753         if (pendingActions || pendingNotifies)
754                 ereport(ERROR,
755                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
756                                  errmsg("cannot PREPARE a transaction that has executed LISTEN, UNLISTEN, or NOTIFY")));
757 }
758
759 /*
760  * PreCommit_Notify
761  *
762  *              This is called at transaction commit, before actually committing to
763  *              clog.
764  *
765  *              If there are pending LISTEN actions, make sure we are listed in the
766  *              shared-memory listener array.  This must happen before commit to
767  *              ensure we don't miss any notifies from transactions that commit
768  *              just after ours.
769  *
770  *              If there are outbound notify requests in the pendingNotifies list,
771  *              add them to the global queue.  We do that before commit so that
772  *              we can still throw error if we run out of queue space.
773  */
774 void
775 PreCommit_Notify(void)
776 {
777         ListCell   *p;
778
779         if (pendingActions == NIL && pendingNotifies == NIL)
780                 return;                                 /* no relevant statements in this xact */
781
782         if (Trace_notify)
783                 elog(DEBUG1, "PreCommit_Notify");
784
785         /* Preflight for any pending listen/unlisten actions */
786         foreach(p, pendingActions)
787         {
788                 ListenAction *actrec = (ListenAction *) lfirst(p);
789
790                 switch (actrec->action)
791                 {
792                         case LISTEN_LISTEN:
793                                 Exec_ListenPreCommit();
794                                 break;
795                         case LISTEN_UNLISTEN:
796                                 /* there is no Exec_UnlistenPreCommit() */
797                                 break;
798                         case LISTEN_UNLISTEN_ALL:
799                                 /* there is no Exec_UnlistenAllPreCommit() */
800                                 break;
801                 }
802         }
803
804         /* Queue any pending notifies */
805         if (pendingNotifies)
806         {
807                 ListCell   *nextNotify;
808
809                 /*
810                  * Make sure that we have an XID assigned to the current transaction.
811                  * GetCurrentTransactionId is cheap if we already have an XID, but not
812                  * so cheap if we don't, and we'd prefer not to do that work while
813                  * holding AsyncQueueLock.
814                  */
815                 (void) GetCurrentTransactionId();
816
817                 /*
818                  * Serialize writers by acquiring a special lock that we hold till
819                  * after commit.  This ensures that queue entries appear in commit
820                  * order, and in particular that there are never uncommitted queue
821                  * entries ahead of committed ones, so an uncommitted transaction
822                  * can't block delivery of deliverable notifications.
823                  *
824                  * We use a heavyweight lock so that it'll automatically be released
825                  * after either commit or abort.  This also allows deadlocks to be
826                  * detected, though really a deadlock shouldn't be possible here.
827                  *
828                  * The lock is on "database 0", which is pretty ugly but it doesn't
829                  * seem worth inventing a special locktag category just for this.
830                  * (Historical note: before PG 9.0, a similar lock on "database 0" was
831                  * used by the flatfiles mechanism.)
832                  */
833                 LockSharedObject(DatabaseRelationId, InvalidOid, 0,
834                                                  AccessExclusiveLock);
835
836                 /* Now push the notifications into the queue */
837                 backendHasSentNotifications = true;
838
839                 nextNotify = list_head(pendingNotifies);
840                 while (nextNotify != NULL)
841                 {
842                         /*
843                          * Add the pending notifications to the queue.  We acquire and
844                          * release AsyncQueueLock once per page, which might be overkill
845                          * but it does allow readers to get in while we're doing this.
846                          *
847                          * A full queue is very uncommon and should really not happen,
848                          * given that we have so much space available in the SLRU pages.
849                          * Nevertheless we need to deal with this possibility. Note that
850                          * when we get here we are in the process of committing our
851                          * transaction, but we have not yet committed to clog, so at this
852                          * point in time we can still roll the transaction back.
853                          */
854                         LWLockAcquire(AsyncQueueLock, LW_EXCLUSIVE);
855                         asyncQueueFillWarning();
856                         if (asyncQueueIsFull())
857                                 ereport(ERROR,
858                                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
859                                           errmsg("too many notifications in the NOTIFY queue")));
860                         nextNotify = asyncQueueAddEntries(nextNotify);
861                         LWLockRelease(AsyncQueueLock);
862                 }
863         }
864 }
865
866 /*
867  * AtCommit_Notify
868  *
869  *              This is called at transaction commit, after committing to clog.
870  *
871  *              Update listenChannels and clear transaction-local state.
872  */
873 void
874 AtCommit_Notify(void)
875 {
876         ListCell   *p;
877
878         /*
879          * Allow transactions that have not executed LISTEN/UNLISTEN/NOTIFY to
880          * return as soon as possible
881          */
882         if (!pendingActions && !pendingNotifies)
883                 return;
884
885         if (Trace_notify)
886                 elog(DEBUG1, "AtCommit_Notify");
887
888         /* Perform any pending listen/unlisten actions */
889         foreach(p, pendingActions)
890         {
891                 ListenAction *actrec = (ListenAction *) lfirst(p);
892
893                 switch (actrec->action)
894                 {
895                         case LISTEN_LISTEN:
896                                 Exec_ListenCommit(actrec->channel);
897                                 break;
898                         case LISTEN_UNLISTEN:
899                                 Exec_UnlistenCommit(actrec->channel);
900                                 break;
901                         case LISTEN_UNLISTEN_ALL:
902                                 Exec_UnlistenAllCommit();
903                                 break;
904                 }
905         }
906
907         /* If no longer listening to anything, get out of listener array */
908         if (amRegisteredListener && listenChannels == NIL)
909                 asyncQueueUnregister();
910
911         /* And clean up */
912         ClearPendingActionsAndNotifies();
913 }
914
915 /*
916  * Exec_ListenPreCommit --- subroutine for PreCommit_Notify
917  *
918  * This function must make sure we are ready to catch any incoming messages.
919  */
920 static void
921 Exec_ListenPreCommit(void)
922 {
923         QueuePosition head;
924         QueuePosition max;
925         int                     i;
926
927         /*
928          * Nothing to do if we are already listening to something, nor if we
929          * already ran this routine in this transaction.
930          */
931         if (amRegisteredListener)
932                 return;
933
934         if (Trace_notify)
935                 elog(DEBUG1, "Exec_ListenPreCommit(%d)", MyProcPid);
936
937         /*
938          * Before registering, make sure we will unlisten before dying. (Note:
939          * this action does not get undone if we abort later.)
940          */
941         if (!unlistenExitRegistered)
942         {
943                 before_shmem_exit(Async_UnlistenOnExit, 0);
944                 unlistenExitRegistered = true;
945         }
946
947         /*
948          * This is our first LISTEN, so establish our pointer.
949          *
950          * We set our pointer to the global tail pointer and then move it forward
951          * over already-committed notifications.  This ensures we cannot miss any
952          * not-yet-committed notifications.  We might get a few more but that
953          * doesn't hurt.
954          *
955          * In some scenarios there might be a lot of committed notifications that
956          * have not yet been pruned away (because some backend is being lazy about
957          * reading them).  To reduce our startup time, we can look at other
958          * backends and adopt the maximum "pos" pointer of any backend that's in
959          * our database; any notifications it's already advanced over are surely
960          * committed and need not be re-examined by us.  (We must consider only
961          * backends connected to our DB, because others will not have bothered to
962          * check committed-ness of notifications in our DB.)  But we only bother
963          * with that if there's more than a page worth of notifications
964          * outstanding, otherwise scanning all the other backends isn't worth it.
965          *
966          * We need exclusive lock here so we can look at other backends' entries.
967          */
968         LWLockAcquire(AsyncQueueLock, LW_EXCLUSIVE);
969         head = QUEUE_HEAD;
970         max = QUEUE_TAIL;
971         if (QUEUE_POS_PAGE(max) != QUEUE_POS_PAGE(head))
972         {
973                 for (i = 1; i <= MaxBackends; i++)
974                 {
975                         if (QUEUE_BACKEND_DBOID(i) == MyDatabaseId)
976                                 max = QUEUE_POS_MAX(max, QUEUE_BACKEND_POS(i));
977                 }
978         }
979         QUEUE_BACKEND_POS(MyBackendId) = max;
980         QUEUE_BACKEND_PID(MyBackendId) = MyProcPid;
981         QUEUE_BACKEND_DBOID(MyBackendId) = MyDatabaseId;
982         LWLockRelease(AsyncQueueLock);
983
984         /* Now we are listed in the global array, so remember we're listening */
985         amRegisteredListener = true;
986
987         /*
988          * Try to move our pointer forward as far as possible. This will skip over
989          * already-committed notifications. Still, we could get notifications that
990          * have already committed before we started to LISTEN.
991          *
992          * Note that we are not yet listening on anything, so we won't deliver any
993          * notification to the frontend.
994          *
995          * This will also advance the global tail pointer if possible.
996          */
997         if (!QUEUE_POS_EQUAL(max, head))
998                 asyncQueueReadAllNotifications();
999 }
1000
1001 /*
1002  * Exec_ListenCommit --- subroutine for AtCommit_Notify
1003  *
1004  * Add the channel to the list of channels we are listening on.
1005  */
1006 static void
1007 Exec_ListenCommit(const char *channel)
1008 {
1009         MemoryContext oldcontext;
1010
1011         /* Do nothing if we are already listening on this channel */
1012         if (IsListeningOn(channel))
1013                 return;
1014
1015         /*
1016          * Add the new channel name to listenChannels.
1017          *
1018          * XXX It is theoretically possible to get an out-of-memory failure here,
1019          * which would be bad because we already committed.  For the moment it
1020          * doesn't seem worth trying to guard against that, but maybe improve this
1021          * later.
1022          */
1023         oldcontext = MemoryContextSwitchTo(TopMemoryContext);
1024         listenChannels = lappend(listenChannels, pstrdup(channel));
1025         MemoryContextSwitchTo(oldcontext);
1026 }
1027
1028 /*
1029  * Exec_UnlistenCommit --- subroutine for AtCommit_Notify
1030  *
1031  * Remove the specified channel name from listenChannels.
1032  */
1033 static void
1034 Exec_UnlistenCommit(const char *channel)
1035 {
1036         ListCell   *q;
1037         ListCell   *prev;
1038
1039         if (Trace_notify)
1040                 elog(DEBUG1, "Exec_UnlistenCommit(%s,%d)", channel, MyProcPid);
1041
1042         prev = NULL;
1043         foreach(q, listenChannels)
1044         {
1045                 char       *lchan = (char *) lfirst(q);
1046
1047                 if (strcmp(lchan, channel) == 0)
1048                 {
1049                         listenChannels = list_delete_cell(listenChannels, q, prev);
1050                         pfree(lchan);
1051                         break;
1052                 }
1053                 prev = q;
1054         }
1055
1056         /*
1057          * We do not complain about unlistening something not being listened;
1058          * should we?
1059          */
1060 }
1061
1062 /*
1063  * Exec_UnlistenAllCommit --- subroutine for AtCommit_Notify
1064  *
1065  *              Unlisten on all channels for this backend.
1066  */
1067 static void
1068 Exec_UnlistenAllCommit(void)
1069 {
1070         if (Trace_notify)
1071                 elog(DEBUG1, "Exec_UnlistenAllCommit(%d)", MyProcPid);
1072
1073         list_free_deep(listenChannels);
1074         listenChannels = NIL;
1075 }
1076
1077 /*
1078  * ProcessCompletedNotifies --- send out signals and self-notifies
1079  *
1080  * This is called from postgres.c just before going idle at the completion
1081  * of a transaction.  If we issued any notifications in the just-completed
1082  * transaction, send signals to other backends to process them, and also
1083  * process the queue ourselves to send messages to our own frontend.
1084  *
1085  * The reason that this is not done in AtCommit_Notify is that there is
1086  * a nonzero chance of errors here (for example, encoding conversion errors
1087  * while trying to format messages to our frontend).  An error during
1088  * AtCommit_Notify would be a PANIC condition.  The timing is also arranged
1089  * to ensure that a transaction's self-notifies are delivered to the frontend
1090  * before it gets the terminating ReadyForQuery message.
1091  *
1092  * Note that we send signals and process the queue even if the transaction
1093  * eventually aborted.  This is because we need to clean out whatever got
1094  * added to the queue.
1095  *
1096  * NOTE: we are outside of any transaction here.
1097  */
1098 void
1099 ProcessCompletedNotifies(void)
1100 {
1101         MemoryContext caller_context;
1102         bool            signalled;
1103
1104         /* Nothing to do if we didn't send any notifications */
1105         if (!backendHasSentNotifications)
1106                 return;
1107
1108         /*
1109          * We reset the flag immediately; otherwise, if any sort of error occurs
1110          * below, we'd be locked up in an infinite loop, because control will come
1111          * right back here after error cleanup.
1112          */
1113         backendHasSentNotifications = false;
1114
1115         /*
1116          * We must preserve the caller's memory context (probably MessageContext)
1117          * across the transaction we do here.
1118          */
1119         caller_context = CurrentMemoryContext;
1120
1121         if (Trace_notify)
1122                 elog(DEBUG1, "ProcessCompletedNotifies");
1123
1124         /*
1125          * We must run asyncQueueReadAllNotifications inside a transaction, else
1126          * bad things happen if it gets an error.
1127          */
1128         StartTransactionCommand();
1129
1130         /* Send signals to other backends */
1131         signalled = SignalBackends();
1132
1133         if (listenChannels != NIL)
1134         {
1135                 /* Read the queue ourselves, and send relevant stuff to the frontend */
1136                 asyncQueueReadAllNotifications();
1137         }
1138         else if (!signalled)
1139         {
1140                 /*
1141                  * If we found no other listening backends, and we aren't listening
1142                  * ourselves, then we must execute asyncQueueAdvanceTail to flush the
1143                  * queue, because ain't nobody else gonna do it.  This prevents queue
1144                  * overflow when we're sending useless notifies to nobody. (A new
1145                  * listener could have joined since we looked, but if so this is
1146                  * harmless.)
1147                  */
1148                 asyncQueueAdvanceTail();
1149         }
1150
1151         CommitTransactionCommand();
1152
1153         MemoryContextSwitchTo(caller_context);
1154
1155         /* We don't need pq_flush() here since postgres.c will do one shortly */
1156 }
1157
1158 /*
1159  * Test whether we are actively listening on the given channel name.
1160  *
1161  * Note: this function is executed for every notification found in the queue.
1162  * Perhaps it is worth further optimization, eg convert the list to a sorted
1163  * array so we can binary-search it.  In practice the list is likely to be
1164  * fairly short, though.
1165  */
1166 static bool
1167 IsListeningOn(const char *channel)
1168 {
1169         ListCell   *p;
1170
1171         foreach(p, listenChannels)
1172         {
1173                 char       *lchan = (char *) lfirst(p);
1174
1175                 if (strcmp(lchan, channel) == 0)
1176                         return true;
1177         }
1178         return false;
1179 }
1180
1181 /*
1182  * Remove our entry from the listeners array when we are no longer listening
1183  * on any channel.  NB: must not fail if we're already not listening.
1184  */
1185 static void
1186 asyncQueueUnregister(void)
1187 {
1188         bool            advanceTail;
1189
1190         Assert(listenChannels == NIL);          /* else caller error */
1191
1192         if (!amRegisteredListener)      /* nothing to do */
1193                 return;
1194
1195         LWLockAcquire(AsyncQueueLock, LW_SHARED);
1196         /* check if entry is valid and oldest ... */
1197         advanceTail = (MyProcPid == QUEUE_BACKEND_PID(MyBackendId)) &&
1198                 QUEUE_POS_EQUAL(QUEUE_BACKEND_POS(MyBackendId), QUEUE_TAIL);
1199         /* ... then mark it invalid */
1200         QUEUE_BACKEND_PID(MyBackendId) = InvalidPid;
1201         QUEUE_BACKEND_DBOID(MyBackendId) = InvalidOid;
1202         LWLockRelease(AsyncQueueLock);
1203
1204         /* mark ourselves as no longer listed in the global array */
1205         amRegisteredListener = false;
1206
1207         /* If we were the laziest backend, try to advance the tail pointer */
1208         if (advanceTail)
1209                 asyncQueueAdvanceTail();
1210 }
1211
1212 /*
1213  * Test whether there is room to insert more notification messages.
1214  *
1215  * Caller must hold at least shared AsyncQueueLock.
1216  */
1217 static bool
1218 asyncQueueIsFull(void)
1219 {
1220         int                     nexthead;
1221         int                     boundary;
1222
1223         /*
1224          * The queue is full if creating a new head page would create a page that
1225          * logically precedes the current global tail pointer, ie, the head
1226          * pointer would wrap around compared to the tail.  We cannot create such
1227          * a head page for fear of confusing slru.c.  For safety we round the tail
1228          * pointer back to a segment boundary (compare the truncation logic in
1229          * asyncQueueAdvanceTail).
1230          *
1231          * Note that this test is *not* dependent on how much space there is on
1232          * the current head page.  This is necessary because asyncQueueAddEntries
1233          * might try to create the next head page in any case.
1234          */
1235         nexthead = QUEUE_POS_PAGE(QUEUE_HEAD) + 1;
1236         if (nexthead > QUEUE_MAX_PAGE)
1237                 nexthead = 0;                   /* wrap around */
1238         boundary = QUEUE_POS_PAGE(QUEUE_TAIL);
1239         boundary -= boundary % SLRU_PAGES_PER_SEGMENT;
1240         return asyncQueuePagePrecedes(nexthead, boundary);
1241 }
1242
1243 /*
1244  * Advance the QueuePosition to the next entry, assuming that the current
1245  * entry is of length entryLength.  If we jump to a new page the function
1246  * returns true, else false.
1247  */
1248 static bool
1249 asyncQueueAdvance(volatile QueuePosition *position, int entryLength)
1250 {
1251         int                     pageno = QUEUE_POS_PAGE(*position);
1252         int                     offset = QUEUE_POS_OFFSET(*position);
1253         bool            pageJump = false;
1254
1255         /*
1256          * Move to the next writing position: First jump over what we have just
1257          * written or read.
1258          */
1259         offset += entryLength;
1260         Assert(offset <= QUEUE_PAGESIZE);
1261
1262         /*
1263          * In a second step check if another entry can possibly be written to the
1264          * page. If so, stay here, we have reached the next position. If not, then
1265          * we need to move on to the next page.
1266          */
1267         if (offset + QUEUEALIGN(AsyncQueueEntryEmptySize) > QUEUE_PAGESIZE)
1268         {
1269                 pageno++;
1270                 if (pageno > QUEUE_MAX_PAGE)
1271                         pageno = 0;                     /* wrap around */
1272                 offset = 0;
1273                 pageJump = true;
1274         }
1275
1276         SET_QUEUE_POS(*position, pageno, offset);
1277         return pageJump;
1278 }
1279
1280 /*
1281  * Fill the AsyncQueueEntry at *qe with an outbound notification message.
1282  */
1283 static void
1284 asyncQueueNotificationToEntry(Notification *n, AsyncQueueEntry *qe)
1285 {
1286         size_t          channellen = strlen(n->channel);
1287         size_t          payloadlen = strlen(n->payload);
1288         int                     entryLength;
1289
1290         Assert(channellen < NAMEDATALEN);
1291         Assert(payloadlen < NOTIFY_PAYLOAD_MAX_LENGTH);
1292
1293         /* The terminators are already included in AsyncQueueEntryEmptySize */
1294         entryLength = AsyncQueueEntryEmptySize + payloadlen + channellen;
1295         entryLength = QUEUEALIGN(entryLength);
1296         qe->length = entryLength;
1297         qe->dboid = MyDatabaseId;
1298         qe->xid = GetCurrentTransactionId();
1299         qe->srcPid = MyProcPid;
1300         memcpy(qe->data, n->channel, channellen + 1);
1301         memcpy(qe->data + channellen + 1, n->payload, payloadlen + 1);
1302 }
1303
1304 /*
1305  * Add pending notifications to the queue.
1306  *
1307  * We go page by page here, i.e. we stop once we have to go to a new page but
1308  * we will be called again and then fill that next page. If an entry does not
1309  * fit into the current page, we write a dummy entry with an InvalidOid as the
1310  * database OID in order to fill the page. So every page is always used up to
1311  * the last byte which simplifies reading the page later.
1312  *
1313  * We are passed the list cell containing the next notification to write
1314  * and return the first still-unwritten cell back.  Eventually we will return
1315  * NULL indicating all is done.
1316  *
1317  * We are holding AsyncQueueLock already from the caller and grab AsyncCtlLock
1318  * locally in this function.
1319  */
1320 static ListCell *
1321 asyncQueueAddEntries(ListCell *nextNotify)
1322 {
1323         AsyncQueueEntry qe;
1324         QueuePosition queue_head;
1325         int                     pageno;
1326         int                     offset;
1327         int                     slotno;
1328
1329         /* We hold both AsyncQueueLock and AsyncCtlLock during this operation */
1330         LWLockAcquire(AsyncCtlLock, LW_EXCLUSIVE);
1331
1332         /*
1333          * We work with a local copy of QUEUE_HEAD, which we write back to shared
1334          * memory upon exiting.  The reason for this is that if we have to advance
1335          * to a new page, SimpleLruZeroPage might fail (out of disk space, for
1336          * instance), and we must not advance QUEUE_HEAD if it does.  (Otherwise,
1337          * subsequent insertions would try to put entries into a page that slru.c
1338          * thinks doesn't exist yet.)  So, use a local position variable.  Note
1339          * that if we do fail, any already-inserted queue entries are forgotten;
1340          * this is okay, since they'd be useless anyway after our transaction
1341          * rolls back.
1342          */
1343         queue_head = QUEUE_HEAD;
1344
1345         /* Fetch the current page */
1346         pageno = QUEUE_POS_PAGE(queue_head);
1347         slotno = SimpleLruReadPage(AsyncCtl, pageno, true, InvalidTransactionId);
1348         /* Note we mark the page dirty before writing in it */
1349         AsyncCtl->shared->page_dirty[slotno] = true;
1350
1351         while (nextNotify != NULL)
1352         {
1353                 Notification *n = (Notification *) lfirst(nextNotify);
1354
1355                 /* Construct a valid queue entry in local variable qe */
1356                 asyncQueueNotificationToEntry(n, &qe);
1357
1358                 offset = QUEUE_POS_OFFSET(queue_head);
1359
1360                 /* Check whether the entry really fits on the current page */
1361                 if (offset + qe.length <= QUEUE_PAGESIZE)
1362                 {
1363                         /* OK, so advance nextNotify past this item */
1364                         nextNotify = lnext(nextNotify);
1365                 }
1366                 else
1367                 {
1368                         /*
1369                          * Write a dummy entry to fill up the page. Actually readers will
1370                          * only check dboid and since it won't match any reader's database
1371                          * OID, they will ignore this entry and move on.
1372                          */
1373                         qe.length = QUEUE_PAGESIZE - offset;
1374                         qe.dboid = InvalidOid;
1375                         qe.data[0] = '\0';      /* empty channel */
1376                         qe.data[1] = '\0';      /* empty payload */
1377                 }
1378
1379                 /* Now copy qe into the shared buffer page */
1380                 memcpy(AsyncCtl->shared->page_buffer[slotno] + offset,
1381                            &qe,
1382                            qe.length);
1383
1384                 /* Advance queue_head appropriately, and detect if page is full */
1385                 if (asyncQueueAdvance(&(queue_head), qe.length))
1386                 {
1387                         /*
1388                          * Page is full, so we're done here, but first fill the next page
1389                          * with zeroes.  The reason to do this is to ensure that slru.c's
1390                          * idea of the head page is always the same as ours, which avoids
1391                          * boundary problems in SimpleLruTruncate.  The test in
1392                          * asyncQueueIsFull() ensured that there is room to create this
1393                          * page without overrunning the queue.
1394                          */
1395                         slotno = SimpleLruZeroPage(AsyncCtl, QUEUE_POS_PAGE(queue_head));
1396                         /* And exit the loop */
1397                         break;
1398                 }
1399         }
1400
1401         /* Success, so update the global QUEUE_HEAD */
1402         QUEUE_HEAD = queue_head;
1403
1404         LWLockRelease(AsyncCtlLock);
1405
1406         return nextNotify;
1407 }
1408
1409 /*
1410  * SQL function to return the fraction of the notification queue currently
1411  * occupied.
1412  */
1413 Datum
1414 pg_notification_queue_usage(PG_FUNCTION_ARGS)
1415 {
1416         double          usage;
1417
1418         LWLockAcquire(AsyncQueueLock, LW_SHARED);
1419         usage = asyncQueueUsage();
1420         LWLockRelease(AsyncQueueLock);
1421
1422         PG_RETURN_FLOAT8(usage);
1423 }
1424
1425 /*
1426  * Return the fraction of the queue that is currently occupied.
1427  *
1428  * The caller must hold AsyncQueueLock in (at least) shared mode.
1429  */
1430 static double
1431 asyncQueueUsage(void)
1432 {
1433         int                     headPage = QUEUE_POS_PAGE(QUEUE_HEAD);
1434         int                     tailPage = QUEUE_POS_PAGE(QUEUE_TAIL);
1435         int                     occupied;
1436
1437         occupied = headPage - tailPage;
1438
1439         if (occupied == 0)
1440                 return (double) 0;              /* fast exit for common case */
1441
1442         if (occupied < 0)
1443         {
1444                 /* head has wrapped around, tail not yet */
1445                 occupied += QUEUE_MAX_PAGE + 1;
1446         }
1447
1448         return (double) occupied / (double) ((QUEUE_MAX_PAGE + 1) / 2);
1449 }
1450
1451 /*
1452  * Check whether the queue is at least half full, and emit a warning if so.
1453  *
1454  * This is unlikely given the size of the queue, but possible.
1455  * The warnings show up at most once every QUEUE_FULL_WARN_INTERVAL.
1456  *
1457  * Caller must hold exclusive AsyncQueueLock.
1458  */
1459 static void
1460 asyncQueueFillWarning(void)
1461 {
1462         double          fillDegree;
1463         TimestampTz t;
1464
1465         fillDegree = asyncQueueUsage();
1466         if (fillDegree < 0.5)
1467                 return;
1468
1469         t = GetCurrentTimestamp();
1470
1471         if (TimestampDifferenceExceeds(asyncQueueControl->lastQueueFillWarn,
1472                                                                    t, QUEUE_FULL_WARN_INTERVAL))
1473         {
1474                 QueuePosition min = QUEUE_HEAD;
1475                 int32           minPid = InvalidPid;
1476                 int                     i;
1477
1478                 for (i = 1; i <= MaxBackends; i++)
1479                 {
1480                         if (QUEUE_BACKEND_PID(i) != InvalidPid)
1481                         {
1482                                 min = QUEUE_POS_MIN(min, QUEUE_BACKEND_POS(i));
1483                                 if (QUEUE_POS_EQUAL(min, QUEUE_BACKEND_POS(i)))
1484                                         minPid = QUEUE_BACKEND_PID(i);
1485                         }
1486                 }
1487
1488                 ereport(WARNING,
1489                                 (errmsg("NOTIFY queue is %.0f%% full", fillDegree * 100),
1490                                  (minPid != InvalidPid ?
1491                                   errdetail("The server process with PID %d is among those with the oldest transactions.", minPid)
1492                                   : 0),
1493                                  (minPid != InvalidPid ?
1494                                   errhint("The NOTIFY queue cannot be emptied until that process ends its current transaction.")
1495                                   : 0)));
1496
1497                 asyncQueueControl->lastQueueFillWarn = t;
1498         }
1499 }
1500
1501 /*
1502  * Send signals to all listening backends (except our own).
1503  *
1504  * Returns true if we sent at least one signal.
1505  *
1506  * Since we need EXCLUSIVE lock anyway we also check the position of the other
1507  * backends and in case one is already up-to-date we don't signal it.
1508  * This can happen if concurrent notifying transactions have sent a signal and
1509  * the signaled backend has read the other notifications and ours in the same
1510  * step.
1511  *
1512  * Since we know the BackendId and the Pid the signalling is quite cheap.
1513  */
1514 static bool
1515 SignalBackends(void)
1516 {
1517         bool            signalled = false;
1518         int32      *pids;
1519         BackendId  *ids;
1520         int                     count;
1521         int                     i;
1522         int32           pid;
1523
1524         /*
1525          * Identify all backends that are listening and not already up-to-date. We
1526          * don't want to send signals while holding the AsyncQueueLock, so we just
1527          * build a list of target PIDs.
1528          *
1529          * XXX in principle these pallocs could fail, which would be bad. Maybe
1530          * preallocate the arrays?      But in practice this is only run in trivial
1531          * transactions, so there should surely be space available.
1532          */
1533         pids = (int32 *) palloc(MaxBackends * sizeof(int32));
1534         ids = (BackendId *) palloc(MaxBackends * sizeof(BackendId));
1535         count = 0;
1536
1537         LWLockAcquire(AsyncQueueLock, LW_EXCLUSIVE);
1538         for (i = 1; i <= MaxBackends; i++)
1539         {
1540                 pid = QUEUE_BACKEND_PID(i);
1541                 if (pid != InvalidPid && pid != MyProcPid)
1542                 {
1543                         QueuePosition pos = QUEUE_BACKEND_POS(i);
1544
1545                         if (!QUEUE_POS_EQUAL(pos, QUEUE_HEAD))
1546                         {
1547                                 pids[count] = pid;
1548                                 ids[count] = i;
1549                                 count++;
1550                         }
1551                 }
1552         }
1553         LWLockRelease(AsyncQueueLock);
1554
1555         /* Now send signals */
1556         for (i = 0; i < count; i++)
1557         {
1558                 pid = pids[i];
1559
1560                 /*
1561                  * Note: assuming things aren't broken, a signal failure here could
1562                  * only occur if the target backend exited since we released
1563                  * AsyncQueueLock; which is unlikely but certainly possible. So we
1564                  * just log a low-level debug message if it happens.
1565                  */
1566                 if (SendProcSignal(pid, PROCSIG_NOTIFY_INTERRUPT, ids[i]) < 0)
1567                         elog(DEBUG3, "could not signal backend with PID %d: %m", pid);
1568                 else
1569                         signalled = true;
1570         }
1571
1572         pfree(pids);
1573         pfree(ids);
1574
1575         return signalled;
1576 }
1577
1578 /*
1579  * AtAbort_Notify
1580  *
1581  *      This is called at transaction abort.
1582  *
1583  *      Gets rid of pending actions and outbound notifies that we would have
1584  *      executed if the transaction got committed.
1585  */
1586 void
1587 AtAbort_Notify(void)
1588 {
1589         /*
1590          * If we LISTEN but then roll back the transaction after PreCommit_Notify,
1591          * we have registered as a listener but have not made any entry in
1592          * listenChannels.  In that case, deregister again.
1593          */
1594         if (amRegisteredListener && listenChannels == NIL)
1595                 asyncQueueUnregister();
1596
1597         /* And clean up */
1598         ClearPendingActionsAndNotifies();
1599 }
1600
1601 /*
1602  * AtSubStart_Notify() --- Take care of subtransaction start.
1603  *
1604  * Push empty state for the new subtransaction.
1605  */
1606 void
1607 AtSubStart_Notify(void)
1608 {
1609         MemoryContext old_cxt;
1610
1611         /* Keep the list-of-lists in TopTransactionContext for simplicity */
1612         old_cxt = MemoryContextSwitchTo(TopTransactionContext);
1613
1614         upperPendingActions = lcons(pendingActions, upperPendingActions);
1615
1616         Assert(list_length(upperPendingActions) ==
1617                    GetCurrentTransactionNestLevel() - 1);
1618
1619         pendingActions = NIL;
1620
1621         upperPendingNotifies = lcons(pendingNotifies, upperPendingNotifies);
1622
1623         Assert(list_length(upperPendingNotifies) ==
1624                    GetCurrentTransactionNestLevel() - 1);
1625
1626         pendingNotifies = NIL;
1627
1628         MemoryContextSwitchTo(old_cxt);
1629 }
1630
1631 /*
1632  * AtSubCommit_Notify() --- Take care of subtransaction commit.
1633  *
1634  * Reassign all items in the pending lists to the parent transaction.
1635  */
1636 void
1637 AtSubCommit_Notify(void)
1638 {
1639         List       *parentPendingActions;
1640         List       *parentPendingNotifies;
1641
1642         parentPendingActions = (List *) linitial(upperPendingActions);
1643         upperPendingActions = list_delete_first(upperPendingActions);
1644
1645         Assert(list_length(upperPendingActions) ==
1646                    GetCurrentTransactionNestLevel() - 2);
1647
1648         /*
1649          * Mustn't try to eliminate duplicates here --- see queue_listen()
1650          */
1651         pendingActions = list_concat(parentPendingActions, pendingActions);
1652
1653         parentPendingNotifies = (List *) linitial(upperPendingNotifies);
1654         upperPendingNotifies = list_delete_first(upperPendingNotifies);
1655
1656         Assert(list_length(upperPendingNotifies) ==
1657                    GetCurrentTransactionNestLevel() - 2);
1658
1659         /*
1660          * We could try to eliminate duplicates here, but it seems not worthwhile.
1661          */
1662         pendingNotifies = list_concat(parentPendingNotifies, pendingNotifies);
1663 }
1664
1665 /*
1666  * AtSubAbort_Notify() --- Take care of subtransaction abort.
1667  */
1668 void
1669 AtSubAbort_Notify(void)
1670 {
1671         int                     my_level = GetCurrentTransactionNestLevel();
1672
1673         /*
1674          * All we have to do is pop the stack --- the actions/notifies made in
1675          * this subxact are no longer interesting, and the space will be freed
1676          * when CurTransactionContext is recycled.
1677          *
1678          * This routine could be called more than once at a given nesting level if
1679          * there is trouble during subxact abort.  Avoid dumping core by using
1680          * GetCurrentTransactionNestLevel as the indicator of how far we need to
1681          * prune the list.
1682          */
1683         while (list_length(upperPendingActions) > my_level - 2)
1684         {
1685                 pendingActions = (List *) linitial(upperPendingActions);
1686                 upperPendingActions = list_delete_first(upperPendingActions);
1687         }
1688
1689         while (list_length(upperPendingNotifies) > my_level - 2)
1690         {
1691                 pendingNotifies = (List *) linitial(upperPendingNotifies);
1692                 upperPendingNotifies = list_delete_first(upperPendingNotifies);
1693         }
1694 }
1695
1696 /*
1697  * HandleNotifyInterrupt
1698  *
1699  *              Signal handler portion of interrupt handling. Let the backend know
1700  *              that there's a pending notify interrupt. If we're currently reading
1701  *              from the client, this will interrupt the read and
1702  *              ProcessClientReadInterrupt() will call ProcessNotifyInterrupt().
1703  */
1704 void
1705 HandleNotifyInterrupt(void)
1706 {
1707         /*
1708          * Note: this is called by a SIGNAL HANDLER. You must be very wary what
1709          * you do here.
1710          */
1711
1712         /* signal that work needs to be done */
1713         notifyInterruptPending = true;
1714
1715         /* make sure the event is processed in due course */
1716         SetLatch(MyLatch);
1717 }
1718
1719 /*
1720  * ProcessNotifyInterrupt
1721  *
1722  *              This is called just after waiting for a frontend command.  If a
1723  *              interrupt arrives (via HandleNotifyInterrupt()) while reading, the
1724  *              read will be interrupted via the process's latch, and this routine
1725  *              will get called.  If we are truly idle (ie, *not* inside a transaction
1726  *              block), process the incoming notifies.
1727  */
1728 void
1729 ProcessNotifyInterrupt(void)
1730 {
1731         if (IsTransactionOrTransactionBlock())
1732                 return;                                 /* not really idle */
1733
1734         while (notifyInterruptPending)
1735                 ProcessIncomingNotify();
1736 }
1737
1738
1739 /*
1740  * Read all pending notifications from the queue, and deliver appropriate
1741  * ones to my frontend.  Stop when we reach queue head or an uncommitted
1742  * notification.
1743  */
1744 static void
1745 asyncQueueReadAllNotifications(void)
1746 {
1747         volatile QueuePosition pos;
1748         QueuePosition oldpos;
1749         QueuePosition head;
1750         bool            advanceTail;
1751
1752         /* page_buffer must be adequately aligned, so use a union */
1753         union
1754         {
1755                 char            buf[QUEUE_PAGESIZE];
1756                 AsyncQueueEntry align;
1757         }                       page_buffer;
1758
1759         /* Fetch current state */
1760         LWLockAcquire(AsyncQueueLock, LW_SHARED);
1761         /* Assert checks that we have a valid state entry */
1762         Assert(MyProcPid == QUEUE_BACKEND_PID(MyBackendId));
1763         pos = oldpos = QUEUE_BACKEND_POS(MyBackendId);
1764         head = QUEUE_HEAD;
1765         LWLockRelease(AsyncQueueLock);
1766
1767         if (QUEUE_POS_EQUAL(pos, head))
1768         {
1769                 /* Nothing to do, we have read all notifications already. */
1770                 return;
1771         }
1772
1773         /*----------
1774          * Note that we deliver everything that we see in the queue and that
1775          * matches our _current_ listening state.
1776          * Especially we do not take into account different commit times.
1777          * Consider the following example:
1778          *
1779          * Backend 1:                                    Backend 2:
1780          *
1781          * transaction starts
1782          * NOTIFY foo;
1783          * commit starts
1784          *                                                               transaction starts
1785          *                                                               LISTEN foo;
1786          *                                                               commit starts
1787          * commit to clog
1788          *                                                               commit to clog
1789          *
1790          * It could happen that backend 2 sees the notification from backend 1 in
1791          * the queue.  Even though the notifying transaction committed before
1792          * the listening transaction, we still deliver the notification.
1793          *
1794          * The idea is that an additional notification does not do any harm, we
1795          * just need to make sure that we do not miss a notification.
1796          *
1797          * It is possible that we fail while trying to send a message to our
1798          * frontend (for example, because of encoding conversion failure).
1799          * If that happens it is critical that we not try to send the same
1800          * message over and over again.  Therefore, we place a PG_TRY block
1801          * here that will forcibly advance our backend position before we lose
1802          * control to an error.  (We could alternatively retake AsyncQueueLock
1803          * and move the position before handling each individual message, but
1804          * that seems like too much lock traffic.)
1805          *----------
1806          */
1807         PG_TRY();
1808         {
1809                 bool            reachedStop;
1810
1811                 do
1812                 {
1813                         int                     curpage = QUEUE_POS_PAGE(pos);
1814                         int                     curoffset = QUEUE_POS_OFFSET(pos);
1815                         int                     slotno;
1816                         int                     copysize;
1817
1818                         /*
1819                          * We copy the data from SLRU into a local buffer, so as to avoid
1820                          * holding the AsyncCtlLock while we are examining the entries and
1821                          * possibly transmitting them to our frontend.  Copy only the part
1822                          * of the page we will actually inspect.
1823                          */
1824                         slotno = SimpleLruReadPage_ReadOnly(AsyncCtl, curpage,
1825                                                                                                 InvalidTransactionId);
1826                         if (curpage == QUEUE_POS_PAGE(head))
1827                         {
1828                                 /* we only want to read as far as head */
1829                                 copysize = QUEUE_POS_OFFSET(head) - curoffset;
1830                                 if (copysize < 0)
1831                                         copysize = 0;           /* just for safety */
1832                         }
1833                         else
1834                         {
1835                                 /* fetch all the rest of the page */
1836                                 copysize = QUEUE_PAGESIZE - curoffset;
1837                         }
1838                         memcpy(page_buffer.buf + curoffset,
1839                                    AsyncCtl->shared->page_buffer[slotno] + curoffset,
1840                                    copysize);
1841                         /* Release lock that we got from SimpleLruReadPage_ReadOnly() */
1842                         LWLockRelease(AsyncCtlLock);
1843
1844                         /*
1845                          * Process messages up to the stop position, end of page, or an
1846                          * uncommitted message.
1847                          *
1848                          * Our stop position is what we found to be the head's position
1849                          * when we entered this function. It might have changed already.
1850                          * But if it has, we will receive (or have already received and
1851                          * queued) another signal and come here again.
1852                          *
1853                          * We are not holding AsyncQueueLock here! The queue can only
1854                          * extend beyond the head pointer (see above) and we leave our
1855                          * backend's pointer where it is so nobody will truncate or
1856                          * rewrite pages under us. Especially we don't want to hold a lock
1857                          * while sending the notifications to the frontend.
1858                          */
1859                         reachedStop = asyncQueueProcessPageEntries(&pos, head,
1860                                                                                                            page_buffer.buf);
1861                 } while (!reachedStop);
1862         }
1863         PG_CATCH();
1864         {
1865                 /* Update shared state */
1866                 LWLockAcquire(AsyncQueueLock, LW_SHARED);
1867                 QUEUE_BACKEND_POS(MyBackendId) = pos;
1868                 advanceTail = QUEUE_POS_EQUAL(oldpos, QUEUE_TAIL);
1869                 LWLockRelease(AsyncQueueLock);
1870
1871                 /* If we were the laziest backend, try to advance the tail pointer */
1872                 if (advanceTail)
1873                         asyncQueueAdvanceTail();
1874
1875                 PG_RE_THROW();
1876         }
1877         PG_END_TRY();
1878
1879         /* Update shared state */
1880         LWLockAcquire(AsyncQueueLock, LW_SHARED);
1881         QUEUE_BACKEND_POS(MyBackendId) = pos;
1882         advanceTail = QUEUE_POS_EQUAL(oldpos, QUEUE_TAIL);
1883         LWLockRelease(AsyncQueueLock);
1884
1885         /* If we were the laziest backend, try to advance the tail pointer */
1886         if (advanceTail)
1887                 asyncQueueAdvanceTail();
1888 }
1889
1890 /*
1891  * Fetch notifications from the shared queue, beginning at position current,
1892  * and deliver relevant ones to my frontend.
1893  *
1894  * The current page must have been fetched into page_buffer from shared
1895  * memory.  (We could access the page right in shared memory, but that
1896  * would imply holding the AsyncCtlLock throughout this routine.)
1897  *
1898  * We stop if we reach the "stop" position, or reach a notification from an
1899  * uncommitted transaction, or reach the end of the page.
1900  *
1901  * The function returns true once we have reached the stop position or an
1902  * uncommitted notification, and false if we have finished with the page.
1903  * In other words: once it returns true there is no need to look further.
1904  * The QueuePosition *current is advanced past all processed messages.
1905  */
1906 static bool
1907 asyncQueueProcessPageEntries(volatile QueuePosition *current,
1908                                                          QueuePosition stop,
1909                                                          char *page_buffer)
1910 {
1911         bool            reachedStop = false;
1912         bool            reachedEndOfPage;
1913         AsyncQueueEntry *qe;
1914
1915         do
1916         {
1917                 QueuePosition thisentry = *current;
1918
1919                 if (QUEUE_POS_EQUAL(thisentry, stop))
1920                         break;
1921
1922                 qe = (AsyncQueueEntry *) (page_buffer + QUEUE_POS_OFFSET(thisentry));
1923
1924                 /*
1925                  * Advance *current over this message, possibly to the next page. As
1926                  * noted in the comments for asyncQueueReadAllNotifications, we must
1927                  * do this before possibly failing while processing the message.
1928                  */
1929                 reachedEndOfPage = asyncQueueAdvance(current, qe->length);
1930
1931                 /* Ignore messages destined for other databases */
1932                 if (qe->dboid == MyDatabaseId)
1933                 {
1934                         if (TransactionIdIsInProgress(qe->xid))
1935                         {
1936                                 /*
1937                                  * The source transaction is still in progress, so we can't
1938                                  * process this message yet.  Break out of the loop, but first
1939                                  * back up *current so we will reprocess the message next
1940                                  * time.  (Note: it is unlikely but not impossible for
1941                                  * TransactionIdDidCommit to fail, so we can't really avoid
1942                                  * this advance-then-back-up behavior when dealing with an
1943                                  * uncommitted message.)
1944                                  *
1945                                  * Note that we must test TransactionIdIsInProgress before we
1946                                  * test TransactionIdDidCommit, else we might return a message
1947                                  * from a transaction that is not yet visible to snapshots;
1948                                  * compare the comments at the head of tqual.c.
1949                                  */
1950                                 *current = thisentry;
1951                                 reachedStop = true;
1952                                 break;
1953                         }
1954                         else if (TransactionIdDidCommit(qe->xid))
1955                         {
1956                                 /* qe->data is the null-terminated channel name */
1957                                 char       *channel = qe->data;
1958
1959                                 if (IsListeningOn(channel))
1960                                 {
1961                                         /* payload follows channel name */
1962                                         char       *payload = qe->data + strlen(channel) + 1;
1963
1964                                         NotifyMyFrontEnd(channel, payload, qe->srcPid);
1965                                 }
1966                         }
1967                         else
1968                         {
1969                                 /*
1970                                  * The source transaction aborted or crashed, so we just
1971                                  * ignore its notifications.
1972                                  */
1973                         }
1974                 }
1975
1976                 /* Loop back if we're not at end of page */
1977         } while (!reachedEndOfPage);
1978
1979         if (QUEUE_POS_EQUAL(*current, stop))
1980                 reachedStop = true;
1981
1982         return reachedStop;
1983 }
1984
1985 /*
1986  * Advance the shared queue tail variable to the minimum of all the
1987  * per-backend tail pointers.  Truncate pg_notify space if possible.
1988  */
1989 static void
1990 asyncQueueAdvanceTail(void)
1991 {
1992         QueuePosition min;
1993         int                     i;
1994         int                     oldtailpage;
1995         int                     newtailpage;
1996         int                     boundary;
1997
1998         LWLockAcquire(AsyncQueueLock, LW_EXCLUSIVE);
1999         min = QUEUE_HEAD;
2000         for (i = 1; i <= MaxBackends; i++)
2001         {
2002                 if (QUEUE_BACKEND_PID(i) != InvalidPid)
2003                         min = QUEUE_POS_MIN(min, QUEUE_BACKEND_POS(i));
2004         }
2005         oldtailpage = QUEUE_POS_PAGE(QUEUE_TAIL);
2006         QUEUE_TAIL = min;
2007         LWLockRelease(AsyncQueueLock);
2008
2009         /*
2010          * We can truncate something if the global tail advanced across an SLRU
2011          * segment boundary.
2012          *
2013          * XXX it might be better to truncate only once every several segments, to
2014          * reduce the number of directory scans.
2015          */
2016         newtailpage = QUEUE_POS_PAGE(min);
2017         boundary = newtailpage - (newtailpage % SLRU_PAGES_PER_SEGMENT);
2018         if (asyncQueuePagePrecedes(oldtailpage, boundary))
2019         {
2020                 /*
2021                  * SimpleLruTruncate() will ask for AsyncCtlLock but will also release
2022                  * the lock again.
2023                  */
2024                 SimpleLruTruncate(AsyncCtl, newtailpage);
2025         }
2026 }
2027
2028 /*
2029  * ProcessIncomingNotify
2030  *
2031  *              Deal with arriving NOTIFYs from other backends as soon as it's safe to
2032  *              do so. This used to be called from the PROCSIG_NOTIFY_INTERRUPT
2033  *              signal handler, but isn't anymore.
2034  *
2035  *              Scan the queue for arriving notifications and report them to my front
2036  *              end.
2037  *
2038  *              NOTE: since we are outside any transaction, we must create our own.
2039  */
2040 static void
2041 ProcessIncomingNotify(void)
2042 {
2043         /* We *must* reset the flag */
2044         notifyInterruptPending = false;
2045
2046         /* Do nothing else if we aren't actively listening */
2047         if (listenChannels == NIL)
2048                 return;
2049
2050         if (Trace_notify)
2051                 elog(DEBUG1, "ProcessIncomingNotify");
2052
2053         set_ps_display("notify interrupt", false);
2054
2055         /*
2056          * We must run asyncQueueReadAllNotifications inside a transaction, else
2057          * bad things happen if it gets an error.
2058          */
2059         StartTransactionCommand();
2060
2061         asyncQueueReadAllNotifications();
2062
2063         CommitTransactionCommand();
2064
2065         /*
2066          * Must flush the notify messages to ensure frontend gets them promptly.
2067          */
2068         pq_flush();
2069
2070         set_ps_display("idle", false);
2071
2072         if (Trace_notify)
2073                 elog(DEBUG1, "ProcessIncomingNotify: done");
2074 }
2075
2076 /*
2077  * Send NOTIFY message to my front end.
2078  */
2079 static void
2080 NotifyMyFrontEnd(const char *channel, const char *payload, int32 srcPid)
2081 {
2082         if (whereToSendOutput == DestRemote)
2083         {
2084                 StringInfoData buf;
2085
2086                 pq_beginmessage(&buf, 'A');
2087                 pq_sendint(&buf, srcPid, sizeof(int32));
2088                 pq_sendstring(&buf, channel);
2089                 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
2090                         pq_sendstring(&buf, payload);
2091                 pq_endmessage(&buf);
2092
2093                 /*
2094                  * NOTE: we do not do pq_flush() here.  For a self-notify, it will
2095                  * happen at the end of the transaction, and for incoming notifies
2096                  * ProcessIncomingNotify will do it after finding all the notifies.
2097                  */
2098         }
2099         else
2100                 elog(INFO, "NOTIFY for \"%s\" payload \"%s\"", channel, payload);
2101 }
2102
2103 /* Does pendingNotifies include the given channel/payload? */
2104 static bool
2105 AsyncExistsPendingNotify(const char *channel, const char *payload)
2106 {
2107         ListCell   *p;
2108         Notification *n;
2109
2110         if (pendingNotifies == NIL)
2111                 return false;
2112
2113         if (payload == NULL)
2114                 payload = "";
2115
2116         /*----------
2117          * We need to append new elements to the end of the list in order to keep
2118          * the order. However, on the other hand we'd like to check the list
2119          * backwards in order to make duplicate-elimination a tad faster when the
2120          * same condition is signaled many times in a row. So as a compromise we
2121          * check the tail element first which we can access directly. If this
2122          * doesn't match, we check the whole list.
2123          *
2124          * As we are not checking our parents' lists, we can still get duplicates
2125          * in combination with subtransactions, like in:
2126          *
2127          * begin;
2128          * notify foo '1';
2129          * savepoint foo;
2130          * notify foo '1';
2131          * commit;
2132          *----------
2133          */
2134         n = (Notification *) llast(pendingNotifies);
2135         if (strcmp(n->channel, channel) == 0 &&
2136                 strcmp(n->payload, payload) == 0)
2137                 return true;
2138
2139         foreach(p, pendingNotifies)
2140         {
2141                 n = (Notification *) lfirst(p);
2142
2143                 if (strcmp(n->channel, channel) == 0 &&
2144                         strcmp(n->payload, payload) == 0)
2145                         return true;
2146         }
2147
2148         return false;
2149 }
2150
2151 /* Clear the pendingActions and pendingNotifies lists. */
2152 static void
2153 ClearPendingActionsAndNotifies(void)
2154 {
2155         /*
2156          * We used to have to explicitly deallocate the list members and nodes,
2157          * because they were malloc'd.  Now, since we know they are palloc'd in
2158          * CurTransactionContext, we need not do that --- they'll go away
2159          * automatically at transaction exit.  We need only reset the list head
2160          * pointers.
2161          */
2162         pendingActions = NIL;
2163         pendingNotifies = NIL;
2164 }