PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
async.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * async.c
4  * Asynchronous notification: NOTIFY, LISTEN, UNLISTEN
5  *
6  * Portions Copyright (c) 1996-2017, 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/snapmgr.h"
141 #include "utils/timestamp.h"
142 #include "utils/tqual.h"
143 
144 
145 /*
146  * Maximum size of a NOTIFY payload, including terminating NULL. This
147  * must be kept small enough so that a notification message fits on one
148  * SLRU page. The magic fudge factor here is noncritical as long as it's
149  * more than AsyncQueueEntryEmptySize --- we make it significantly bigger
150  * than that, so changes in that data structure won't affect user-visible
151  * restrictions.
152  */
153 #define NOTIFY_PAYLOAD_MAX_LENGTH (BLCKSZ - NAMEDATALEN - 128)
154 
155 /*
156  * Struct representing an entry in the global notify queue
157  *
158  * This struct declaration has the maximal length, but in a real queue entry
159  * the data area is only big enough for the actual channel and payload strings
160  * (each null-terminated). AsyncQueueEntryEmptySize is the minimum possible
161  * entry size, if both channel and payload strings are empty (but note it
162  * doesn't include alignment padding).
163  *
164  * The "length" field should always be rounded up to the next QUEUEALIGN
165  * multiple so that all fields are properly aligned.
166  */
167 typedef struct AsyncQueueEntry
168 {
169  int length; /* total allocated length of entry */
170  Oid dboid; /* sender's database OID */
171  TransactionId xid; /* sender's XID */
172  int32 srcPid; /* sender's PID */
175 
176 /* Currently, no field of AsyncQueueEntry requires more than int alignment */
177 #define QUEUEALIGN(len) INTALIGN(len)
178 
179 #define AsyncQueueEntryEmptySize (offsetof(AsyncQueueEntry, data) + 2)
180 
181 /*
182  * Struct describing a queue position, and assorted macros for working with it
183  */
184 typedef struct QueuePosition
185 {
186  int page; /* SLRU page number */
187  int offset; /* byte offset within page */
188 } QueuePosition;
189 
190 #define QUEUE_POS_PAGE(x) ((x).page)
191 #define QUEUE_POS_OFFSET(x) ((x).offset)
192 
193 #define SET_QUEUE_POS(x,y,z) \
194  do { \
195  (x).page = (y); \
196  (x).offset = (z); \
197  } while (0)
198 
199 #define QUEUE_POS_EQUAL(x,y) \
200  ((x).page == (y).page && (x).offset == (y).offset)
201 
202 /* choose logically smaller QueuePosition */
203 #define QUEUE_POS_MIN(x,y) \
204  (asyncQueuePagePrecedes((x).page, (y).page) ? (x) : \
205  (x).page != (y).page ? (y) : \
206  (x).offset < (y).offset ? (x) : (y))
207 
208 /* choose logically larger QueuePosition */
209 #define QUEUE_POS_MAX(x,y) \
210  (asyncQueuePagePrecedes((x).page, (y).page) ? (y) : \
211  (x).page != (y).page ? (x) : \
212  (x).offset > (y).offset ? (x) : (y))
213 
214 /*
215  * Struct describing a listening backend's status
216  */
217 typedef struct QueueBackendStatus
218 {
219  int32 pid; /* either a PID or InvalidPid */
220  Oid dboid; /* backend's database OID, or InvalidOid */
221  QueuePosition pos; /* backend has read queue up to here */
223 
224 /*
225  * Shared memory state for LISTEN/NOTIFY (excluding its SLRU stuff)
226  *
227  * The AsyncQueueControl structure is protected by the AsyncQueueLock.
228  *
229  * When holding the lock in SHARED mode, backends may only inspect their own
230  * entries as well as the head and tail pointers. Consequently we can allow a
231  * backend to update its own record while holding only SHARED lock (since no
232  * other backend will inspect it).
233  *
234  * When holding the lock in EXCLUSIVE mode, backends can inspect the entries
235  * of other backends and also change the head and tail pointers.
236  *
237  * AsyncCtlLock is used as the control lock for the pg_notify SLRU buffers.
238  * In order to avoid deadlocks, whenever we need both locks, we always first
239  * get AsyncQueueLock and then AsyncCtlLock.
240  *
241  * Each backend uses the backend[] array entry with index equal to its
242  * BackendId (which can range from 1 to MaxBackends). We rely on this to make
243  * SendProcSignal fast.
244  */
245 typedef struct AsyncQueueControl
246 {
247  QueuePosition head; /* head points to the next free location */
248  QueuePosition tail; /* the global tail is equivalent to the pos of
249  * the "slowest" backend */
250  TimestampTz lastQueueFillWarn; /* time of last queue-full msg */
251  QueueBackendStatus backend[FLEXIBLE_ARRAY_MEMBER];
252  /* backend[0] is not used; used entries are from [1] to [MaxBackends] */
254 
256 
257 #define QUEUE_HEAD (asyncQueueControl->head)
258 #define QUEUE_TAIL (asyncQueueControl->tail)
259 #define QUEUE_BACKEND_PID(i) (asyncQueueControl->backend[i].pid)
260 #define QUEUE_BACKEND_DBOID(i) (asyncQueueControl->backend[i].dboid)
261 #define QUEUE_BACKEND_POS(i) (asyncQueueControl->backend[i].pos)
262 
263 /*
264  * The SLRU buffer area through which we access the notification queue
265  */
267 
268 #define AsyncCtl (&AsyncCtlData)
269 #define QUEUE_PAGESIZE BLCKSZ
270 #define QUEUE_FULL_WARN_INTERVAL 5000 /* warn at most once every 5s */
271 
272 /*
273  * slru.c currently assumes that all filenames are four characters of hex
274  * digits. That means that we can use segments 0000 through FFFF.
275  * Each segment contains SLRU_PAGES_PER_SEGMENT pages which gives us
276  * the pages from 0 to SLRU_PAGES_PER_SEGMENT * 0x10000 - 1.
277  *
278  * It's of course possible to enhance slru.c, but this gives us so much
279  * space already that it doesn't seem worth the trouble.
280  *
281  * The most data we can have in the queue at a time is QUEUE_MAX_PAGE/2
282  * pages, because more than that would confuse slru.c into thinking there
283  * was a wraparound condition. With the default BLCKSZ this means there
284  * can be up to 8GB of queued-and-not-read data.
285  *
286  * Note: it's possible to redefine QUEUE_MAX_PAGE with a smaller multiple of
287  * SLRU_PAGES_PER_SEGMENT, for easier testing of queue-full behaviour.
288  */
289 #define QUEUE_MAX_PAGE (SLRU_PAGES_PER_SEGMENT * 0x10000 - 1)
290 
291 /*
292  * listenChannels identifies the channels we are actually listening to
293  * (ie, have committed a LISTEN on). It is a simple list of channel names,
294  * allocated in TopMemoryContext.
295  */
296 static List *listenChannels = NIL; /* list of C strings */
297 
298 /*
299  * State for pending LISTEN/UNLISTEN actions consists of an ordered list of
300  * all actions requested in the current transaction. As explained above,
301  * we don't actually change listenChannels until we reach transaction commit.
302  *
303  * The list is kept in CurTransactionContext. In subtransactions, each
304  * subtransaction has its own list in its own CurTransactionContext, but
305  * successful subtransactions attach their lists to their parent's list.
306  * Failed subtransactions simply discard their lists.
307  */
308 typedef enum
309 {
314 
315 typedef struct
316 {
318  char channel[FLEXIBLE_ARRAY_MEMBER]; /* nul-terminated string */
319 } ListenAction;
320 
321 static List *pendingActions = NIL; /* list of ListenAction */
322 
323 static List *upperPendingActions = NIL; /* list of upper-xact lists */
324 
325 /*
326  * State for outbound notifies consists of a list of all channels+payloads
327  * NOTIFYed in the current transaction. We do not actually perform a NOTIFY
328  * until and unless the transaction commits. pendingNotifies is NIL if no
329  * NOTIFYs have been done in the current transaction.
330  *
331  * The list is kept in CurTransactionContext. In subtransactions, each
332  * subtransaction has its own list in its own CurTransactionContext, but
333  * successful subtransactions attach their lists to their parent's list.
334  * Failed subtransactions simply discard their lists.
335  *
336  * Note: the action and notify lists do not interact within a transaction.
337  * In particular, if a transaction does NOTIFY and then LISTEN on the same
338  * condition name, it will get a self-notify at commit. This is a bit odd
339  * but is consistent with our historical behavior.
340  */
341 typedef struct Notification
342 {
343  char *channel; /* channel name */
344  char *payload; /* payload string (can be empty) */
345 } Notification;
346 
347 static List *pendingNotifies = NIL; /* list of Notifications */
348 
349 static List *upperPendingNotifies = NIL; /* list of upper-xact lists */
350 
351 /*
352  * Inbound notifications are initially processed by HandleNotifyInterrupt(),
353  * called from inside a signal handler. That just sets the
354  * notifyInterruptPending flag and sets the process
355  * latch. ProcessNotifyInterrupt() will then be called whenever it's safe to
356  * actually deal with the interrupt.
357  */
358 volatile sig_atomic_t notifyInterruptPending = false;
359 
360 /* True if we've registered an on_shmem_exit cleanup */
361 static bool unlistenExitRegistered = false;
362 
363 /* True if we're currently registered as a listener in asyncQueueControl */
364 static bool amRegisteredListener = false;
365 
366 /* has this backend sent notifications in the current transaction? */
367 static bool backendHasSentNotifications = false;
368 
369 /* GUC parameter */
370 bool Trace_notify = false;
371 
372 /* local function prototypes */
373 static bool asyncQueuePagePrecedes(int p, int q);
374 static void queue_listen(ListenActionKind action, const char *channel);
375 static void Async_UnlistenOnExit(int code, Datum arg);
376 static void Exec_ListenPreCommit(void);
377 static void Exec_ListenCommit(const char *channel);
378 static void Exec_UnlistenCommit(const char *channel);
379 static void Exec_UnlistenAllCommit(void);
380 static bool IsListeningOn(const char *channel);
381 static void asyncQueueUnregister(void);
382 static bool asyncQueueIsFull(void);
383 static bool asyncQueueAdvance(volatile QueuePosition *position, int entryLength);
385 static ListCell *asyncQueueAddEntries(ListCell *nextNotify);
386 static double asyncQueueUsage(void);
387 static void asyncQueueFillWarning(void);
388 static bool SignalBackends(void);
389 static void asyncQueueReadAllNotifications(void);
390 static bool asyncQueueProcessPageEntries(volatile QueuePosition *current,
391  QueuePosition stop,
392  char *page_buffer,
393  Snapshot snapshot);
394 static void asyncQueueAdvanceTail(void);
395 static void ProcessIncomingNotify(void);
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
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
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 
435 
436  return size;
437 }
438 
439 /*
440  * Initialize our shared memory area
441  */
442 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  {
475  }
476  }
477 
478  /*
479  * Set up SLRU management of the pg_notify data.
480  */
481  AsyncCtl->PagePrecedes = asyncQueuePagePrecedes;
483  AsyncCtlLock, "pg_notify", LWTRANCHE_ASYNC_BUFFERS);
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  */
493 
494  /* Now initialize page zero to empty */
495  LWLockAcquire(AsyncCtlLock, LW_EXCLUSIVE);
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
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 */
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  */
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  */
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
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 
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
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
738 {
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
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
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:
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 (must happen after the above) */
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  */
835 
836  /* Now push the notifications into the queue */
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);
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
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:
903  break;
904  }
905  }
906 
907  /* If no longer listening to anything, get out of listener array */
908  if (amRegisteredListener && listenChannels == NIL)
910 
911  /* And clean up */
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
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  */
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  */
942  {
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  {
976  max = QUEUE_POS_MAX(max, QUEUE_BACKEND_POS(i));
977  }
978  }
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. Also, although our transaction might
994  * have executed NOTIFY, those message(s) aren't queued yet so we can't
995  * see them in the queue.
996  *
997  * This will also advance the global tail pointer if possible.
998  */
999  if (!QUEUE_POS_EQUAL(max, head))
1001 }
1002 
1003 /*
1004  * Exec_ListenCommit --- subroutine for AtCommit_Notify
1005  *
1006  * Add the channel to the list of channels we are listening on.
1007  */
1008 static void
1009 Exec_ListenCommit(const char *channel)
1010 {
1011  MemoryContext oldcontext;
1012 
1013  /* Do nothing if we are already listening on this channel */
1014  if (IsListeningOn(channel))
1015  return;
1016 
1017  /*
1018  * Add the new channel name to listenChannels.
1019  *
1020  * XXX It is theoretically possible to get an out-of-memory failure here,
1021  * which would be bad because we already committed. For the moment it
1022  * doesn't seem worth trying to guard against that, but maybe improve this
1023  * later.
1024  */
1026  listenChannels = lappend(listenChannels, pstrdup(channel));
1027  MemoryContextSwitchTo(oldcontext);
1028 }
1029 
1030 /*
1031  * Exec_UnlistenCommit --- subroutine for AtCommit_Notify
1032  *
1033  * Remove the specified channel name from listenChannels.
1034  */
1035 static void
1036 Exec_UnlistenCommit(const char *channel)
1037 {
1038  ListCell *q;
1039  ListCell *prev;
1040 
1041  if (Trace_notify)
1042  elog(DEBUG1, "Exec_UnlistenCommit(%s,%d)", channel, MyProcPid);
1043 
1044  prev = NULL;
1045  foreach(q, listenChannels)
1046  {
1047  char *lchan = (char *) lfirst(q);
1048 
1049  if (strcmp(lchan, channel) == 0)
1050  {
1051  listenChannels = list_delete_cell(listenChannels, q, prev);
1052  pfree(lchan);
1053  break;
1054  }
1055  prev = q;
1056  }
1057 
1058  /*
1059  * We do not complain about unlistening something not being listened;
1060  * should we?
1061  */
1062 }
1063 
1064 /*
1065  * Exec_UnlistenAllCommit --- subroutine for AtCommit_Notify
1066  *
1067  * Unlisten on all channels for this backend.
1068  */
1069 static void
1071 {
1072  if (Trace_notify)
1073  elog(DEBUG1, "Exec_UnlistenAllCommit(%d)", MyProcPid);
1074 
1075  list_free_deep(listenChannels);
1076  listenChannels = NIL;
1077 }
1078 
1079 /*
1080  * ProcessCompletedNotifies --- send out signals and self-notifies
1081  *
1082  * This is called from postgres.c just before going idle at the completion
1083  * of a transaction. If we issued any notifications in the just-completed
1084  * transaction, send signals to other backends to process them, and also
1085  * process the queue ourselves to send messages to our own frontend.
1086  *
1087  * The reason that this is not done in AtCommit_Notify is that there is
1088  * a nonzero chance of errors here (for example, encoding conversion errors
1089  * while trying to format messages to our frontend). An error during
1090  * AtCommit_Notify would be a PANIC condition. The timing is also arranged
1091  * to ensure that a transaction's self-notifies are delivered to the frontend
1092  * before it gets the terminating ReadyForQuery message.
1093  *
1094  * Note that we send signals and process the queue even if the transaction
1095  * eventually aborted. This is because we need to clean out whatever got
1096  * added to the queue.
1097  *
1098  * NOTE: we are outside of any transaction here.
1099  */
1100 void
1102 {
1103  MemoryContext caller_context;
1104  bool signalled;
1105 
1106  /* Nothing to do if we didn't send any notifications */
1108  return;
1109 
1110  /*
1111  * We reset the flag immediately; otherwise, if any sort of error occurs
1112  * below, we'd be locked up in an infinite loop, because control will come
1113  * right back here after error cleanup.
1114  */
1116 
1117  /*
1118  * We must preserve the caller's memory context (probably MessageContext)
1119  * across the transaction we do here.
1120  */
1121  caller_context = CurrentMemoryContext;
1122 
1123  if (Trace_notify)
1124  elog(DEBUG1, "ProcessCompletedNotifies");
1125 
1126  /*
1127  * We must run asyncQueueReadAllNotifications inside a transaction, else
1128  * bad things happen if it gets an error.
1129  */
1131 
1132  /* Send signals to other backends */
1133  signalled = SignalBackends();
1134 
1135  if (listenChannels != NIL)
1136  {
1137  /* Read the queue ourselves, and send relevant stuff to the frontend */
1139  }
1140  else if (!signalled)
1141  {
1142  /*
1143  * If we found no other listening backends, and we aren't listening
1144  * ourselves, then we must execute asyncQueueAdvanceTail to flush the
1145  * queue, because ain't nobody else gonna do it. This prevents queue
1146  * overflow when we're sending useless notifies to nobody. (A new
1147  * listener could have joined since we looked, but if so this is
1148  * harmless.)
1149  */
1151  }
1152 
1154 
1155  MemoryContextSwitchTo(caller_context);
1156 
1157  /* We don't need pq_flush() here since postgres.c will do one shortly */
1158 }
1159 
1160 /*
1161  * Test whether we are actively listening on the given channel name.
1162  *
1163  * Note: this function is executed for every notification found in the queue.
1164  * Perhaps it is worth further optimization, eg convert the list to a sorted
1165  * array so we can binary-search it. In practice the list is likely to be
1166  * fairly short, though.
1167  */
1168 static bool
1169 IsListeningOn(const char *channel)
1170 {
1171  ListCell *p;
1172 
1173  foreach(p, listenChannels)
1174  {
1175  char *lchan = (char *) lfirst(p);
1176 
1177  if (strcmp(lchan, channel) == 0)
1178  return true;
1179  }
1180  return false;
1181 }
1182 
1183 /*
1184  * Remove our entry from the listeners array when we are no longer listening
1185  * on any channel. NB: must not fail if we're already not listening.
1186  */
1187 static void
1189 {
1190  bool advanceTail;
1191 
1192  Assert(listenChannels == NIL); /* else caller error */
1193 
1194  if (!amRegisteredListener) /* nothing to do */
1195  return;
1196 
1197  LWLockAcquire(AsyncQueueLock, LW_SHARED);
1198  /* check if entry is valid and oldest ... */
1199  advanceTail = (MyProcPid == QUEUE_BACKEND_PID(MyBackendId)) &&
1201  /* ... then mark it invalid */
1204  LWLockRelease(AsyncQueueLock);
1205 
1206  /* mark ourselves as no longer listed in the global array */
1207  amRegisteredListener = false;
1208 
1209  /* If we were the laziest backend, try to advance the tail pointer */
1210  if (advanceTail)
1212 }
1213 
1214 /*
1215  * Test whether there is room to insert more notification messages.
1216  *
1217  * Caller must hold at least shared AsyncQueueLock.
1218  */
1219 static bool
1221 {
1222  int nexthead;
1223  int boundary;
1224 
1225  /*
1226  * The queue is full if creating a new head page would create a page that
1227  * logically precedes the current global tail pointer, ie, the head
1228  * pointer would wrap around compared to the tail. We cannot create such
1229  * a head page for fear of confusing slru.c. For safety we round the tail
1230  * pointer back to a segment boundary (compare the truncation logic in
1231  * asyncQueueAdvanceTail).
1232  *
1233  * Note that this test is *not* dependent on how much space there is on
1234  * the current head page. This is necessary because asyncQueueAddEntries
1235  * might try to create the next head page in any case.
1236  */
1237  nexthead = QUEUE_POS_PAGE(QUEUE_HEAD) + 1;
1238  if (nexthead > QUEUE_MAX_PAGE)
1239  nexthead = 0; /* wrap around */
1240  boundary = QUEUE_POS_PAGE(QUEUE_TAIL);
1241  boundary -= boundary % SLRU_PAGES_PER_SEGMENT;
1242  return asyncQueuePagePrecedes(nexthead, boundary);
1243 }
1244 
1245 /*
1246  * Advance the QueuePosition to the next entry, assuming that the current
1247  * entry is of length entryLength. If we jump to a new page the function
1248  * returns true, else false.
1249  */
1250 static bool
1251 asyncQueueAdvance(volatile QueuePosition *position, int entryLength)
1252 {
1253  int pageno = QUEUE_POS_PAGE(*position);
1254  int offset = QUEUE_POS_OFFSET(*position);
1255  bool pageJump = false;
1256 
1257  /*
1258  * Move to the next writing position: First jump over what we have just
1259  * written or read.
1260  */
1261  offset += entryLength;
1262  Assert(offset <= QUEUE_PAGESIZE);
1263 
1264  /*
1265  * In a second step check if another entry can possibly be written to the
1266  * page. If so, stay here, we have reached the next position. If not, then
1267  * we need to move on to the next page.
1268  */
1270  {
1271  pageno++;
1272  if (pageno > QUEUE_MAX_PAGE)
1273  pageno = 0; /* wrap around */
1274  offset = 0;
1275  pageJump = true;
1276  }
1277 
1278  SET_QUEUE_POS(*position, pageno, offset);
1279  return pageJump;
1280 }
1281 
1282 /*
1283  * Fill the AsyncQueueEntry at *qe with an outbound notification message.
1284  */
1285 static void
1287 {
1288  size_t channellen = strlen(n->channel);
1289  size_t payloadlen = strlen(n->payload);
1290  int entryLength;
1291 
1292  Assert(channellen < NAMEDATALEN);
1293  Assert(payloadlen < NOTIFY_PAYLOAD_MAX_LENGTH);
1294 
1295  /* The terminators are already included in AsyncQueueEntryEmptySize */
1296  entryLength = AsyncQueueEntryEmptySize + payloadlen + channellen;
1297  entryLength = QUEUEALIGN(entryLength);
1298  qe->length = entryLength;
1299  qe->dboid = MyDatabaseId;
1300  qe->xid = GetCurrentTransactionId();
1301  qe->srcPid = MyProcPid;
1302  memcpy(qe->data, n->channel, channellen + 1);
1303  memcpy(qe->data + channellen + 1, n->payload, payloadlen + 1);
1304 }
1305 
1306 /*
1307  * Add pending notifications to the queue.
1308  *
1309  * We go page by page here, i.e. we stop once we have to go to a new page but
1310  * we will be called again and then fill that next page. If an entry does not
1311  * fit into the current page, we write a dummy entry with an InvalidOid as the
1312  * database OID in order to fill the page. So every page is always used up to
1313  * the last byte which simplifies reading the page later.
1314  *
1315  * We are passed the list cell containing the next notification to write
1316  * and return the first still-unwritten cell back. Eventually we will return
1317  * NULL indicating all is done.
1318  *
1319  * We are holding AsyncQueueLock already from the caller and grab AsyncCtlLock
1320  * locally in this function.
1321  */
1322 static ListCell *
1324 {
1325  AsyncQueueEntry qe;
1326  QueuePosition queue_head;
1327  int pageno;
1328  int offset;
1329  int slotno;
1330 
1331  /* We hold both AsyncQueueLock and AsyncCtlLock during this operation */
1332  LWLockAcquire(AsyncCtlLock, LW_EXCLUSIVE);
1333 
1334  /*
1335  * We work with a local copy of QUEUE_HEAD, which we write back to shared
1336  * memory upon exiting. The reason for this is that if we have to advance
1337  * to a new page, SimpleLruZeroPage might fail (out of disk space, for
1338  * instance), and we must not advance QUEUE_HEAD if it does. (Otherwise,
1339  * subsequent insertions would try to put entries into a page that slru.c
1340  * thinks doesn't exist yet.) So, use a local position variable. Note
1341  * that if we do fail, any already-inserted queue entries are forgotten;
1342  * this is okay, since they'd be useless anyway after our transaction
1343  * rolls back.
1344  */
1345  queue_head = QUEUE_HEAD;
1346 
1347  /* Fetch the current page */
1348  pageno = QUEUE_POS_PAGE(queue_head);
1349  slotno = SimpleLruReadPage(AsyncCtl, pageno, true, InvalidTransactionId);
1350  /* Note we mark the page dirty before writing in it */
1351  AsyncCtl->shared->page_dirty[slotno] = true;
1352 
1353  while (nextNotify != NULL)
1354  {
1355  Notification *n = (Notification *) lfirst(nextNotify);
1356 
1357  /* Construct a valid queue entry in local variable qe */
1359 
1360  offset = QUEUE_POS_OFFSET(queue_head);
1361 
1362  /* Check whether the entry really fits on the current page */
1363  if (offset + qe.length <= QUEUE_PAGESIZE)
1364  {
1365  /* OK, so advance nextNotify past this item */
1366  nextNotify = lnext(nextNotify);
1367  }
1368  else
1369  {
1370  /*
1371  * Write a dummy entry to fill up the page. Actually readers will
1372  * only check dboid and since it won't match any reader's database
1373  * OID, they will ignore this entry and move on.
1374  */
1375  qe.length = QUEUE_PAGESIZE - offset;
1376  qe.dboid = InvalidOid;
1377  qe.data[0] = '\0'; /* empty channel */
1378  qe.data[1] = '\0'; /* empty payload */
1379  }
1380 
1381  /* Now copy qe into the shared buffer page */
1382  memcpy(AsyncCtl->shared->page_buffer[slotno] + offset,
1383  &qe,
1384  qe.length);
1385 
1386  /* Advance queue_head appropriately, and detect if page is full */
1387  if (asyncQueueAdvance(&(queue_head), qe.length))
1388  {
1389  /*
1390  * Page is full, so we're done here, but first fill the next page
1391  * with zeroes. The reason to do this is to ensure that slru.c's
1392  * idea of the head page is always the same as ours, which avoids
1393  * boundary problems in SimpleLruTruncate. The test in
1394  * asyncQueueIsFull() ensured that there is room to create this
1395  * page without overrunning the queue.
1396  */
1397  slotno = SimpleLruZeroPage(AsyncCtl, QUEUE_POS_PAGE(queue_head));
1398  /* And exit the loop */
1399  break;
1400  }
1401  }
1402 
1403  /* Success, so update the global QUEUE_HEAD */
1404  QUEUE_HEAD = queue_head;
1405 
1406  LWLockRelease(AsyncCtlLock);
1407 
1408  return nextNotify;
1409 }
1410 
1411 /*
1412  * SQL function to return the fraction of the notification queue currently
1413  * occupied.
1414  */
1415 Datum
1417 {
1418  double usage;
1419 
1420  LWLockAcquire(AsyncQueueLock, LW_SHARED);
1421  usage = asyncQueueUsage();
1422  LWLockRelease(AsyncQueueLock);
1423 
1424  PG_RETURN_FLOAT8(usage);
1425 }
1426 
1427 /*
1428  * Return the fraction of the queue that is currently occupied.
1429  *
1430  * The caller must hold AsyncQueueLock in (at least) shared mode.
1431  */
1432 static double
1434 {
1435  int headPage = QUEUE_POS_PAGE(QUEUE_HEAD);
1436  int tailPage = QUEUE_POS_PAGE(QUEUE_TAIL);
1437  int occupied;
1438 
1439  occupied = headPage - tailPage;
1440 
1441  if (occupied == 0)
1442  return (double) 0; /* fast exit for common case */
1443 
1444  if (occupied < 0)
1445  {
1446  /* head has wrapped around, tail not yet */
1447  occupied += QUEUE_MAX_PAGE + 1;
1448  }
1449 
1450  return (double) occupied / (double) ((QUEUE_MAX_PAGE + 1) / 2);
1451 }
1452 
1453 /*
1454  * Check whether the queue is at least half full, and emit a warning if so.
1455  *
1456  * This is unlikely given the size of the queue, but possible.
1457  * The warnings show up at most once every QUEUE_FULL_WARN_INTERVAL.
1458  *
1459  * Caller must hold exclusive AsyncQueueLock.
1460  */
1461 static void
1463 {
1464  double fillDegree;
1465  TimestampTz t;
1466 
1467  fillDegree = asyncQueueUsage();
1468  if (fillDegree < 0.5)
1469  return;
1470 
1471  t = GetCurrentTimestamp();
1472 
1473  if (TimestampDifferenceExceeds(asyncQueueControl->lastQueueFillWarn,
1475  {
1476  QueuePosition min = QUEUE_HEAD;
1477  int32 minPid = InvalidPid;
1478  int i;
1479 
1480  for (i = 1; i <= MaxBackends; i++)
1481  {
1482  if (QUEUE_BACKEND_PID(i) != InvalidPid)
1483  {
1484  min = QUEUE_POS_MIN(min, QUEUE_BACKEND_POS(i));
1485  if (QUEUE_POS_EQUAL(min, QUEUE_BACKEND_POS(i)))
1486  minPid = QUEUE_BACKEND_PID(i);
1487  }
1488  }
1489 
1490  ereport(WARNING,
1491  (errmsg("NOTIFY queue is %.0f%% full", fillDegree * 100),
1492  (minPid != InvalidPid ?
1493  errdetail("The server process with PID %d is among those with the oldest transactions.", minPid)
1494  : 0),
1495  (minPid != InvalidPid ?
1496  errhint("The NOTIFY queue cannot be emptied until that process ends its current transaction.")
1497  : 0)));
1498 
1499  asyncQueueControl->lastQueueFillWarn = t;
1500  }
1501 }
1502 
1503 /*
1504  * Send signals to all listening backends (except our own).
1505  *
1506  * Returns true if we sent at least one signal.
1507  *
1508  * Since we need EXCLUSIVE lock anyway we also check the position of the other
1509  * backends and in case one is already up-to-date we don't signal it.
1510  * This can happen if concurrent notifying transactions have sent a signal and
1511  * the signaled backend has read the other notifications and ours in the same
1512  * step.
1513  *
1514  * Since we know the BackendId and the Pid the signalling is quite cheap.
1515  */
1516 static bool
1518 {
1519  bool signalled = false;
1520  int32 *pids;
1521  BackendId *ids;
1522  int count;
1523  int i;
1524  int32 pid;
1525 
1526  /*
1527  * Identify all backends that are listening and not already up-to-date. We
1528  * don't want to send signals while holding the AsyncQueueLock, so we just
1529  * build a list of target PIDs.
1530  *
1531  * XXX in principle these pallocs could fail, which would be bad. Maybe
1532  * preallocate the arrays? But in practice this is only run in trivial
1533  * transactions, so there should surely be space available.
1534  */
1535  pids = (int32 *) palloc(MaxBackends * sizeof(int32));
1536  ids = (BackendId *) palloc(MaxBackends * sizeof(BackendId));
1537  count = 0;
1538 
1539  LWLockAcquire(AsyncQueueLock, LW_EXCLUSIVE);
1540  for (i = 1; i <= MaxBackends; i++)
1541  {
1542  pid = QUEUE_BACKEND_PID(i);
1543  if (pid != InvalidPid && pid != MyProcPid)
1544  {
1546 
1547  if (!QUEUE_POS_EQUAL(pos, QUEUE_HEAD))
1548  {
1549  pids[count] = pid;
1550  ids[count] = i;
1551  count++;
1552  }
1553  }
1554  }
1555  LWLockRelease(AsyncQueueLock);
1556 
1557  /* Now send signals */
1558  for (i = 0; i < count; i++)
1559  {
1560  pid = pids[i];
1561 
1562  /*
1563  * Note: assuming things aren't broken, a signal failure here could
1564  * only occur if the target backend exited since we released
1565  * AsyncQueueLock; which is unlikely but certainly possible. So we
1566  * just log a low-level debug message if it happens.
1567  */
1568  if (SendProcSignal(pid, PROCSIG_NOTIFY_INTERRUPT, ids[i]) < 0)
1569  elog(DEBUG3, "could not signal backend with PID %d: %m", pid);
1570  else
1571  signalled = true;
1572  }
1573 
1574  pfree(pids);
1575  pfree(ids);
1576 
1577  return signalled;
1578 }
1579 
1580 /*
1581  * AtAbort_Notify
1582  *
1583  * This is called at transaction abort.
1584  *
1585  * Gets rid of pending actions and outbound notifies that we would have
1586  * executed if the transaction got committed.
1587  */
1588 void
1590 {
1591  /*
1592  * If we LISTEN but then roll back the transaction after PreCommit_Notify,
1593  * we have registered as a listener but have not made any entry in
1594  * listenChannels. In that case, deregister again.
1595  */
1596  if (amRegisteredListener && listenChannels == NIL)
1598 
1599  /* And clean up */
1601 }
1602 
1603 /*
1604  * AtSubStart_Notify() --- Take care of subtransaction start.
1605  *
1606  * Push empty state for the new subtransaction.
1607  */
1608 void
1610 {
1611  MemoryContext old_cxt;
1612 
1613  /* Keep the list-of-lists in TopTransactionContext for simplicity */
1615 
1616  upperPendingActions = lcons(pendingActions, upperPendingActions);
1617 
1618  Assert(list_length(upperPendingActions) ==
1620 
1621  pendingActions = NIL;
1622 
1623  upperPendingNotifies = lcons(pendingNotifies, upperPendingNotifies);
1624 
1625  Assert(list_length(upperPendingNotifies) ==
1627 
1628  pendingNotifies = NIL;
1629 
1630  MemoryContextSwitchTo(old_cxt);
1631 }
1632 
1633 /*
1634  * AtSubCommit_Notify() --- Take care of subtransaction commit.
1635  *
1636  * Reassign all items in the pending lists to the parent transaction.
1637  */
1638 void
1640 {
1641  List *parentPendingActions;
1642  List *parentPendingNotifies;
1643 
1644  parentPendingActions = linitial_node(List, upperPendingActions);
1645  upperPendingActions = list_delete_first(upperPendingActions);
1646 
1647  Assert(list_length(upperPendingActions) ==
1649 
1650  /*
1651  * Mustn't try to eliminate duplicates here --- see queue_listen()
1652  */
1653  pendingActions = list_concat(parentPendingActions, pendingActions);
1654 
1655  parentPendingNotifies = linitial_node(List, upperPendingNotifies);
1656  upperPendingNotifies = list_delete_first(upperPendingNotifies);
1657 
1658  Assert(list_length(upperPendingNotifies) ==
1660 
1661  /*
1662  * We could try to eliminate duplicates here, but it seems not worthwhile.
1663  */
1664  pendingNotifies = list_concat(parentPendingNotifies, pendingNotifies);
1665 }
1666 
1667 /*
1668  * AtSubAbort_Notify() --- Take care of subtransaction abort.
1669  */
1670 void
1672 {
1673  int my_level = GetCurrentTransactionNestLevel();
1674 
1675  /*
1676  * All we have to do is pop the stack --- the actions/notifies made in
1677  * this subxact are no longer interesting, and the space will be freed
1678  * when CurTransactionContext is recycled.
1679  *
1680  * This routine could be called more than once at a given nesting level if
1681  * there is trouble during subxact abort. Avoid dumping core by using
1682  * GetCurrentTransactionNestLevel as the indicator of how far we need to
1683  * prune the list.
1684  */
1685  while (list_length(upperPendingActions) > my_level - 2)
1686  {
1687  pendingActions = linitial_node(List, upperPendingActions);
1688  upperPendingActions = list_delete_first(upperPendingActions);
1689  }
1690 
1691  while (list_length(upperPendingNotifies) > my_level - 2)
1692  {
1693  pendingNotifies = linitial_node(List, upperPendingNotifies);
1694  upperPendingNotifies = list_delete_first(upperPendingNotifies);
1695  }
1696 }
1697 
1698 /*
1699  * HandleNotifyInterrupt
1700  *
1701  * Signal handler portion of interrupt handling. Let the backend know
1702  * that there's a pending notify interrupt. If we're currently reading
1703  * from the client, this will interrupt the read and
1704  * ProcessClientReadInterrupt() will call ProcessNotifyInterrupt().
1705  */
1706 void
1708 {
1709  /*
1710  * Note: this is called by a SIGNAL HANDLER. You must be very wary what
1711  * you do here.
1712  */
1713 
1714  /* signal that work needs to be done */
1715  notifyInterruptPending = true;
1716 
1717  /* make sure the event is processed in due course */
1718  SetLatch(MyLatch);
1719 }
1720 
1721 /*
1722  * ProcessNotifyInterrupt
1723  *
1724  * This is called just after waiting for a frontend command. If a
1725  * interrupt arrives (via HandleNotifyInterrupt()) while reading, the
1726  * read will be interrupted via the process's latch, and this routine
1727  * will get called. If we are truly idle (ie, *not* inside a transaction
1728  * block), process the incoming notifies.
1729  */
1730 void
1732 {
1734  return; /* not really idle */
1735 
1736  while (notifyInterruptPending)
1738 }
1739 
1740 
1741 /*
1742  * Read all pending notifications from the queue, and deliver appropriate
1743  * ones to my frontend. Stop when we reach queue head or an uncommitted
1744  * notification.
1745  */
1746 static void
1748 {
1749  volatile QueuePosition pos;
1750  QueuePosition oldpos;
1751  QueuePosition head;
1752  Snapshot snapshot;
1753  bool advanceTail;
1754 
1755  /* page_buffer must be adequately aligned, so use a union */
1756  union
1757  {
1758  char buf[QUEUE_PAGESIZE];
1759  AsyncQueueEntry align;
1760  } page_buffer;
1761 
1762  /* Fetch current state */
1763  LWLockAcquire(AsyncQueueLock, LW_SHARED);
1764  /* Assert checks that we have a valid state entry */
1766  pos = oldpos = QUEUE_BACKEND_POS(MyBackendId);
1767  head = QUEUE_HEAD;
1768  LWLockRelease(AsyncQueueLock);
1769 
1770  if (QUEUE_POS_EQUAL(pos, head))
1771  {
1772  /* Nothing to do, we have read all notifications already. */
1773  return;
1774  }
1775 
1776  /* Get snapshot we'll use to decide which xacts are still in progress */
1777  snapshot = RegisterSnapshot(GetLatestSnapshot());
1778 
1779  /*----------
1780  * Note that we deliver everything that we see in the queue and that
1781  * matches our _current_ listening state.
1782  * Especially we do not take into account different commit times.
1783  * Consider the following example:
1784  *
1785  * Backend 1: Backend 2:
1786  *
1787  * transaction starts
1788  * NOTIFY foo;
1789  * commit starts
1790  * transaction starts
1791  * LISTEN foo;
1792  * commit starts
1793  * commit to clog
1794  * commit to clog
1795  *
1796  * It could happen that backend 2 sees the notification from backend 1 in
1797  * the queue. Even though the notifying transaction committed before
1798  * the listening transaction, we still deliver the notification.
1799  *
1800  * The idea is that an additional notification does not do any harm, we
1801  * just need to make sure that we do not miss a notification.
1802  *
1803  * It is possible that we fail while trying to send a message to our
1804  * frontend (for example, because of encoding conversion failure).
1805  * If that happens it is critical that we not try to send the same
1806  * message over and over again. Therefore, we place a PG_TRY block
1807  * here that will forcibly advance our backend position before we lose
1808  * control to an error. (We could alternatively retake AsyncQueueLock
1809  * and move the position before handling each individual message, but
1810  * that seems like too much lock traffic.)
1811  *----------
1812  */
1813  PG_TRY();
1814  {
1815  bool reachedStop;
1816 
1817  do
1818  {
1819  int curpage = QUEUE_POS_PAGE(pos);
1820  int curoffset = QUEUE_POS_OFFSET(pos);
1821  int slotno;
1822  int copysize;
1823 
1824  /*
1825  * We copy the data from SLRU into a local buffer, so as to avoid
1826  * holding the AsyncCtlLock while we are examining the entries and
1827  * possibly transmitting them to our frontend. Copy only the part
1828  * of the page we will actually inspect.
1829  */
1830  slotno = SimpleLruReadPage_ReadOnly(AsyncCtl, curpage,
1832  if (curpage == QUEUE_POS_PAGE(head))
1833  {
1834  /* we only want to read as far as head */
1835  copysize = QUEUE_POS_OFFSET(head) - curoffset;
1836  if (copysize < 0)
1837  copysize = 0; /* just for safety */
1838  }
1839  else
1840  {
1841  /* fetch all the rest of the page */
1842  copysize = QUEUE_PAGESIZE - curoffset;
1843  }
1844  memcpy(page_buffer.buf + curoffset,
1845  AsyncCtl->shared->page_buffer[slotno] + curoffset,
1846  copysize);
1847  /* Release lock that we got from SimpleLruReadPage_ReadOnly() */
1848  LWLockRelease(AsyncCtlLock);
1849 
1850  /*
1851  * Process messages up to the stop position, end of page, or an
1852  * uncommitted message.
1853  *
1854  * Our stop position is what we found to be the head's position
1855  * when we entered this function. It might have changed already.
1856  * But if it has, we will receive (or have already received and
1857  * queued) another signal and come here again.
1858  *
1859  * We are not holding AsyncQueueLock here! The queue can only
1860  * extend beyond the head pointer (see above) and we leave our
1861  * backend's pointer where it is so nobody will truncate or
1862  * rewrite pages under us. Especially we don't want to hold a lock
1863  * while sending the notifications to the frontend.
1864  */
1865  reachedStop = asyncQueueProcessPageEntries(&pos, head,
1866  page_buffer.buf,
1867  snapshot);
1868  } while (!reachedStop);
1869  }
1870  PG_CATCH();
1871  {
1872  /* Update shared state */
1873  LWLockAcquire(AsyncQueueLock, LW_SHARED);
1875  advanceTail = QUEUE_POS_EQUAL(oldpos, QUEUE_TAIL);
1876  LWLockRelease(AsyncQueueLock);
1877 
1878  /* If we were the laziest backend, try to advance the tail pointer */
1879  if (advanceTail)
1881 
1882  PG_RE_THROW();
1883  }
1884  PG_END_TRY();
1885 
1886  /* Update shared state */
1887  LWLockAcquire(AsyncQueueLock, LW_SHARED);
1889  advanceTail = QUEUE_POS_EQUAL(oldpos, QUEUE_TAIL);
1890  LWLockRelease(AsyncQueueLock);
1891 
1892  /* If we were the laziest backend, try to advance the tail pointer */
1893  if (advanceTail)
1895 
1896  /* Done with snapshot */
1897  UnregisterSnapshot(snapshot);
1898 }
1899 
1900 /*
1901  * Fetch notifications from the shared queue, beginning at position current,
1902  * and deliver relevant ones to my frontend.
1903  *
1904  * The current page must have been fetched into page_buffer from shared
1905  * memory. (We could access the page right in shared memory, but that
1906  * would imply holding the AsyncCtlLock throughout this routine.)
1907  *
1908  * We stop if we reach the "stop" position, or reach a notification from an
1909  * uncommitted transaction, or reach the end of the page.
1910  *
1911  * The function returns true once we have reached the stop position or an
1912  * uncommitted notification, and false if we have finished with the page.
1913  * In other words: once it returns true there is no need to look further.
1914  * The QueuePosition *current is advanced past all processed messages.
1915  */
1916 static bool
1918  QueuePosition stop,
1919  char *page_buffer,
1920  Snapshot snapshot)
1921 {
1922  bool reachedStop = false;
1923  bool reachedEndOfPage;
1924  AsyncQueueEntry *qe;
1925 
1926  do
1927  {
1928  QueuePosition thisentry = *current;
1929 
1930  if (QUEUE_POS_EQUAL(thisentry, stop))
1931  break;
1932 
1933  qe = (AsyncQueueEntry *) (page_buffer + QUEUE_POS_OFFSET(thisentry));
1934 
1935  /*
1936  * Advance *current over this message, possibly to the next page. As
1937  * noted in the comments for asyncQueueReadAllNotifications, we must
1938  * do this before possibly failing while processing the message.
1939  */
1940  reachedEndOfPage = asyncQueueAdvance(current, qe->length);
1941 
1942  /* Ignore messages destined for other databases */
1943  if (qe->dboid == MyDatabaseId)
1944  {
1945  if (XidInMVCCSnapshot(qe->xid, snapshot))
1946  {
1947  /*
1948  * The source transaction is still in progress, so we can't
1949  * process this message yet. Break out of the loop, but first
1950  * back up *current so we will reprocess the message next
1951  * time. (Note: it is unlikely but not impossible for
1952  * TransactionIdDidCommit to fail, so we can't really avoid
1953  * this advance-then-back-up behavior when dealing with an
1954  * uncommitted message.)
1955  *
1956  * Note that we must test XidInMVCCSnapshot before we test
1957  * TransactionIdDidCommit, else we might return a message from
1958  * a transaction that is not yet visible to snapshots; compare
1959  * the comments at the head of tqual.c.
1960  *
1961  * Also, while our own xact won't be listed in the snapshot,
1962  * we need not check for TransactionIdIsCurrentTransactionId
1963  * because our transaction cannot (yet) have queued any
1964  * messages.
1965  */
1966  *current = thisentry;
1967  reachedStop = true;
1968  break;
1969  }
1970  else if (TransactionIdDidCommit(qe->xid))
1971  {
1972  /* qe->data is the null-terminated channel name */
1973  char *channel = qe->data;
1974 
1975  if (IsListeningOn(channel))
1976  {
1977  /* payload follows channel name */
1978  char *payload = qe->data + strlen(channel) + 1;
1979 
1980  NotifyMyFrontEnd(channel, payload, qe->srcPid);
1981  }
1982  }
1983  else
1984  {
1985  /*
1986  * The source transaction aborted or crashed, so we just
1987  * ignore its notifications.
1988  */
1989  }
1990  }
1991 
1992  /* Loop back if we're not at end of page */
1993  } while (!reachedEndOfPage);
1994 
1995  if (QUEUE_POS_EQUAL(*current, stop))
1996  reachedStop = true;
1997 
1998  return reachedStop;
1999 }
2000 
2001 /*
2002  * Advance the shared queue tail variable to the minimum of all the
2003  * per-backend tail pointers. Truncate pg_notify space if possible.
2004  */
2005 static void
2007 {
2008  QueuePosition min;
2009  int i;
2010  int oldtailpage;
2011  int newtailpage;
2012  int boundary;
2013 
2014  LWLockAcquire(AsyncQueueLock, LW_EXCLUSIVE);
2015  min = QUEUE_HEAD;
2016  for (i = 1; i <= MaxBackends; i++)
2017  {
2018  if (QUEUE_BACKEND_PID(i) != InvalidPid)
2019  min = QUEUE_POS_MIN(min, QUEUE_BACKEND_POS(i));
2020  }
2021  oldtailpage = QUEUE_POS_PAGE(QUEUE_TAIL);
2022  QUEUE_TAIL = min;
2023  LWLockRelease(AsyncQueueLock);
2024 
2025  /*
2026  * We can truncate something if the global tail advanced across an SLRU
2027  * segment boundary.
2028  *
2029  * XXX it might be better to truncate only once every several segments, to
2030  * reduce the number of directory scans.
2031  */
2032  newtailpage = QUEUE_POS_PAGE(min);
2033  boundary = newtailpage - (newtailpage % SLRU_PAGES_PER_SEGMENT);
2034  if (asyncQueuePagePrecedes(oldtailpage, boundary))
2035  {
2036  /*
2037  * SimpleLruTruncate() will ask for AsyncCtlLock but will also release
2038  * the lock again.
2039  */
2040  SimpleLruTruncate(AsyncCtl, newtailpage);
2041  }
2042 }
2043 
2044 /*
2045  * ProcessIncomingNotify
2046  *
2047  * Deal with arriving NOTIFYs from other backends as soon as it's safe to
2048  * do so. This used to be called from the PROCSIG_NOTIFY_INTERRUPT
2049  * signal handler, but isn't anymore.
2050  *
2051  * Scan the queue for arriving notifications and report them to my front
2052  * end.
2053  *
2054  * NOTE: since we are outside any transaction, we must create our own.
2055  */
2056 static void
2058 {
2059  /* We *must* reset the flag */
2060  notifyInterruptPending = false;
2061 
2062  /* Do nothing else if we aren't actively listening */
2063  if (listenChannels == NIL)
2064  return;
2065 
2066  if (Trace_notify)
2067  elog(DEBUG1, "ProcessIncomingNotify");
2068 
2069  set_ps_display("notify interrupt", false);
2070 
2071  /*
2072  * We must run asyncQueueReadAllNotifications inside a transaction, else
2073  * bad things happen if it gets an error.
2074  */
2076 
2078 
2080 
2081  /*
2082  * Must flush the notify messages to ensure frontend gets them promptly.
2083  */
2084  pq_flush();
2085 
2086  set_ps_display("idle", false);
2087 
2088  if (Trace_notify)
2089  elog(DEBUG1, "ProcessIncomingNotify: done");
2090 }
2091 
2092 /*
2093  * Send NOTIFY message to my front end.
2094  */
2095 void
2096 NotifyMyFrontEnd(const char *channel, const char *payload, int32 srcPid)
2097 {
2099  {
2101 
2102  pq_beginmessage(&buf, 'A');
2103  pq_sendint32(&buf, srcPid);
2104  pq_sendstring(&buf, channel);
2106  pq_sendstring(&buf, payload);
2107  pq_endmessage(&buf);
2108 
2109  /*
2110  * NOTE: we do not do pq_flush() here. For a self-notify, it will
2111  * happen at the end of the transaction, and for incoming notifies
2112  * ProcessIncomingNotify will do it after finding all the notifies.
2113  */
2114  }
2115  else
2116  elog(INFO, "NOTIFY for \"%s\" payload \"%s\"", channel, payload);
2117 }
2118 
2119 /* Does pendingNotifies include the given channel/payload? */
2120 static bool
2121 AsyncExistsPendingNotify(const char *channel, const char *payload)
2122 {
2123  ListCell *p;
2124  Notification *n;
2125 
2126  if (pendingNotifies == NIL)
2127  return false;
2128 
2129  if (payload == NULL)
2130  payload = "";
2131 
2132  /*----------
2133  * We need to append new elements to the end of the list in order to keep
2134  * the order. However, on the other hand we'd like to check the list
2135  * backwards in order to make duplicate-elimination a tad faster when the
2136  * same condition is signaled many times in a row. So as a compromise we
2137  * check the tail element first which we can access directly. If this
2138  * doesn't match, we check the whole list.
2139  *
2140  * As we are not checking our parents' lists, we can still get duplicates
2141  * in combination with subtransactions, like in:
2142  *
2143  * begin;
2144  * notify foo '1';
2145  * savepoint foo;
2146  * notify foo '1';
2147  * commit;
2148  *----------
2149  */
2150  n = (Notification *) llast(pendingNotifies);
2151  if (strcmp(n->channel, channel) == 0 &&
2152  strcmp(n->payload, payload) == 0)
2153  return true;
2154 
2155  foreach(p, pendingNotifies)
2156  {
2157  n = (Notification *) lfirst(p);
2158 
2159  if (strcmp(n->channel, channel) == 0 &&
2160  strcmp(n->payload, payload) == 0)
2161  return true;
2162  }
2163 
2164  return false;
2165 }
2166 
2167 /* Clear the pendingActions and pendingNotifies lists. */
2168 static void
2170 {
2171  /*
2172  * We used to have to explicitly deallocate the list members and nodes,
2173  * because they were malloc'd. Now, since we know they are palloc'd in
2174  * CurTransactionContext, we need not do that --- they'll go away
2175  * automatically at transaction exit. We need only reset the list head
2176  * pointers.
2177  */
2178  pendingActions = NIL;
2179  pendingNotifies = NIL;
2180 }
struct QueueBackendStatus QueueBackendStatus
#define NIL
Definition: pg_list.h:69
static void usage(void)
Definition: pg_standby.c:605
#define QUEUE_TAIL
Definition: async.c:258
char data[NAMEDATALEN+NOTIFY_PAYLOAD_MAX_LENGTH]
Definition: async.c:173
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:39
int errhint(const char *fmt,...)
Definition: elog.c:987
static void queue_listen(ListenActionKind action, const char *channel)
Definition: async.c:608
static SlruCtlData AsyncCtlData
Definition: async.c:266
BackendId MyBackendId
Definition: globals.c:73
#define pq_flush()
Definition: libpq.h:39
MemoryContext TopTransactionContext
Definition: mcxt.c:48
#define QUEUE_BACKEND_PID(i)
Definition: async.c:259
int page
Definition: async.c:186
uint32 TransactionId
Definition: c.h:391
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:863
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1353
#define DEBUG3
Definition: elog.h:23
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
void AsyncShmemInit(void)
Definition: async.c:443
int64 TimestampTz
Definition: timestamp.h:39
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
char * pstrdup(const char *in)
Definition: mcxt.c:1076
#define DatabaseRelationId
Definition: pg_database.h:29
void CommitTransactionCommand(void)
Definition: xact.c:2744
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1168
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
#define AsyncCtl
Definition: async.c:268
#define llast(l)
Definition: pg_list.h:131
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static void Exec_UnlistenAllCommit(void)
Definition: async.c:1070
int offset
Definition: async.c:187
void set_ps_display(const char *activity, bool force)
Definition: ps_status.c:326
MemoryContext CurTransactionContext
Definition: mcxt.c:49
static List * listenChannels
Definition: async.c:296
void AtPrepare_Notify(void)
Definition: async.c:750
int errcode(int sqlerrcode)
Definition: elog.c:575
Datum pg_notification_queue_usage(PG_FUNCTION_ARGS)
Definition: async.c:1416
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4465
char * channel
Definition: async.c:343
#define INFO
Definition: elog.h:33
List * list_concat(List *list1, List *list2)
Definition: list.c:321
static double asyncQueueUsage(void)
Definition: async.c:1433
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
void Async_Listen(const char *channel)
Definition: async.c:638
static void ClearPendingActionsAndNotifies(void)
Definition: async.c:2169
static void asyncQueueNotificationToEntry(Notification *n, AsyncQueueEntry *qe)
Definition: async.c:1286
static bool asyncQueueAdvance(volatile QueuePosition *position, int entryLength)
Definition: async.c:1251
#define QUEUE_HEAD
Definition: async.c:257
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:125
static void Exec_UnlistenCommit(const char *channel)
Definition: async.c:1036
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
#define linitial_node(type, l)
Definition: pg_list.h:114
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1649
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:145
void list_free_deep(List *list)
Definition: list.c:1147
static bool IsListeningOn(const char *channel)
Definition: async.c:1169
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
static bool unlistenExitRegistered
Definition: async.c:361
static bool asyncQueueIsFull(void)
Definition: async.c:1220
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:87
static void asyncQueueFillWarning(void)
Definition: async.c:1462
signed int int32
Definition: c.h:246
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
QueuePosition pos
Definition: async.c:221
#define NAMEDATALEN
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
char * payload
Definition: async.c:344
void PreCommit_Notify(void)
Definition: async.c:775
#define NUM_ASYNC_BUFFERS
Definition: async.h:23
#define QUEUE_POS_OFFSET(x)
Definition: async.c:191
int SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId)
Definition: procsignal.c:180
void pfree(void *pointer)
Definition: mcxt.c:949
#define AsyncQueueEntryEmptySize
Definition: async.c:179
#define ERROR
Definition: elog.h:43
static List * pendingActions
Definition: async.c:321
void PreventCommandDuringRecovery(const char *cmdname)
Definition: utility.c:273
void ProcessNotifyInterrupt(void)
Definition: async.c:1731
static bool AsyncExistsPendingNotify(const char *channel, const char *payload)
Definition: async.c:2121
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:375
static void Async_UnlistenOnExit(int code, Datum arg)
Definition: async.c:737
void AtSubCommit_Notify(void)
Definition: async.c:1639
static bool backendHasSentNotifications
Definition: async.c:367
int MaxBackends
Definition: globals.c:126
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:418
static char * buf
Definition: pg_test_fsync.c:67
#define QUEUE_PAGESIZE
Definition: async.c:269
#define SET_QUEUE_POS(x, y, z)
Definition: async.c:193
int errdetail(const char *fmt,...)
Definition: elog.c:873
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:320
#define InvalidTransactionId
Definition: transam.h:31
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:578
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define NOTIFY_PAYLOAD_MAX_LENGTH
Definition: async.c:153
static bool asyncQueuePagePrecedes(int p, int q)
Definition: async.c:403
static AsyncQueueControl * asyncQueueControl
Definition: async.c:255
static void asyncQueueAdvanceTail(void)
Definition: async.c:2006
static void Exec_ListenCommit(const char *channel)
Definition: async.c:1009
#define lnext(lc)
Definition: pg_list.h:105
#define ereport(elevel, rest)
Definition: elog.h:122
#define IsParallelWorker()
Definition: parallel.h:52
MemoryContext TopMemoryContext
Definition: mcxt.c:43
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:905
#define QUEUE_FULL_WARN_INTERVAL
Definition: async.c:270
List * lappend(List *list, void *datum)
Definition: list.c:128
struct AsyncQueueControl AsyncQueueControl
#define WARNING
Definition: elog.h:40
struct QueuePosition QueuePosition
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
void AtSubAbort_Notify(void)
Definition: async.c:1671
static List * upperPendingActions
Definition: async.c:323
ListenActionKind
Definition: async.c:308
static void ProcessIncomingNotify(void)
Definition: async.c:2057
uintptr_t Datum
Definition: postgres.h:372
static void asyncQueueUnregister(void)
Definition: async.c:1188
static bool amRegisteredListener
Definition: async.c:364
void AtAbort_Notify(void)
Definition: async.c:1589
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
int BackendId
Definition: backendid.h:21
#define QUEUEALIGN(len)
Definition: async.c:177
Oid MyDatabaseId
Definition: globals.c:77
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:467
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:871
#define QUEUE_BACKEND_DBOID(i)
Definition: async.c:260
QueuePosition head
Definition: async.c:247
static bool asyncQueueProcessPageEntries(volatile QueuePosition *current, QueuePosition stop, char *page_buffer, Snapshot snapshot)
Definition: async.c:1917
#define InvalidOid
Definition: postgres_ext.h:36
static void asyncQueueReadAllNotifications(void)
Definition: async.c:1747
void AtSubStart_Notify(void)
Definition: async.c:1609
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:754
#define PG_RETURN_VOID()
Definition: fmgr.h:309
Datum pg_notify(PG_FUNCTION_ARGS)
Definition: async.c:509
List * lcons(void *datum, List *list)
Definition: list.c:259
#define PG_CATCH()
Definition: elog.h:293
void SetLatch(volatile Latch *latch)
Definition: latch.c:414
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
static ListCell * asyncQueueAddEntries(ListCell *nextNotify)
Definition: async.c:1323
#define Assert(condition)
Definition: c.h:681
void ProcessCompletedNotifies(void)
Definition: async.c:1101
#define lfirst(lc)
Definition: pg_list.h:106
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1376
static void Exec_ListenPreCommit(void)
Definition: async.c:921
QueueBackendStatus backend[FLEXIBLE_ARRAY_MEMBER]
Definition: async.c:251
void StartTransactionCommand(void)
Definition: xact.c:2673
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
struct AsyncQueueEntry AsyncQueueEntry
Size AsyncShmemSize(void)
Definition: async.c:426
size_t Size
Definition: c.h:350
void Async_UnlistenAll(void)
Definition: async.c:670
static int list_length(const List *l)
Definition: pg_list.h:89
struct Notification Notification
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
#define PG_RE_THROW()
Definition: elog.h:314
bool Trace_notify
Definition: async.c:370
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:379
void Async_Notify(const char *channel, const char *payload)
Definition: async.c:543
Datum pg_listening_channels(PG_FUNCTION_ARGS)
Definition: async.c:690
TransactionId xid
Definition: async.c:171
char * text_to_cstring(const text *t)
Definition: varlena.c:182
#define AccessExclusiveLock
Definition: lockdefs.h:45
void AtCommit_Notify(void)
Definition: async.c:874
void * user_fctx
Definition: funcapi.h:90
void HandleNotifyInterrupt(void)
Definition: async.c:1707
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:298
char channel[FLEXIBLE_ARRAY_MEMBER]
Definition: async.c:318
int i
ListenActionKind action
Definition: async.c:317
#define CStringGetTextDatum(s)
Definition: builtins.h:91
int32 srcPid
Definition: async.c:172
void * arg
static List * upperPendingNotifies
Definition: async.c:349
struct Latch * MyLatch
Definition: globals.c:52
volatile sig_atomic_t notifyInterruptPending
Definition: async.c:358
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define elog
Definition: elog.h:219
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:33
TimestampTz lastQueueFillWarn
Definition: async.c:250
CommandDest whereToSendOutput
Definition: postgres.c:88
#define QUEUE_MAX_PAGE
Definition: async.c:289
#define PG_TRY()
Definition: elog.h:284
QueuePosition tail
Definition: async.c:248
#define QUEUE_BACKEND_POS(i)
Definition: async.c:261
Definition: pg_list.h:45
ProtocolVersion FrontendProtocol
Definition: globals.c:27
int SimpleLruZeroPage(SlruCtl ctl, int pageno)
Definition: slru.c:263
#define PG_END_TRY()
Definition: elog.h:300
#define QUEUE_POS_PAGE(x)
Definition: async.c:190
#define QUEUE_POS_EQUAL(x, y)
Definition: async.c:199
#define offsetof(type, field)
Definition: c.h:549
#define QUEUE_POS_MIN(x, y)
Definition: async.c:203
void Async_Unlisten(const char *channel)
Definition: async.c:652
bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
Definition: tqual.c:1484
List * list_delete_first(List *list)
Definition: list.c:666
void NotifyMyFrontEnd(const char *channel, const char *payload, int32 srcPid)
Definition: async.c:2096
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
static bool SignalBackends(void)
Definition: async.c:1517
#define InvalidPid
Definition: miscadmin.h:31
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id)
Definition: slru.c:165
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
#define QUEUE_POS_MAX(x, y)
Definition: async.c:209
static List * pendingNotifies
Definition: async.c:347