PostgreSQL Source Code  git master
async.h File Reference
#include <signal.h>
Include dependency graph for async.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define NUM_NOTIFY_BUFFERS   8
 

Functions

Size AsyncShmemSize (void)
 
void AsyncShmemInit (void)
 
void NotifyMyFrontEnd (const char *channel, const char *payload, int32 srcPid)
 
void Async_Notify (const char *channel, const char *payload)
 
void Async_Listen (const char *channel)
 
void Async_Unlisten (const char *channel)
 
void Async_UnlistenAll (void)
 
void PreCommit_Notify (void)
 
void AtCommit_Notify (void)
 
void AtAbort_Notify (void)
 
void AtSubCommit_Notify (void)
 
void AtSubAbort_Notify (void)
 
void AtPrepare_Notify (void)
 
void ProcessCompletedNotifies (void)
 
void HandleNotifyInterrupt (void)
 
void ProcessNotifyInterrupt (void)
 

Variables

bool Trace_notify
 
volatile sig_atomic_t notifyInterruptPending
 

Macro Definition Documentation

◆ NUM_NOTIFY_BUFFERS

#define NUM_NOTIFY_BUFFERS   8

Definition at line 21 of file async.h.

Referenced by AsyncShmemInit(), and AsyncShmemSize().

Function Documentation

◆ Async_Listen()

void Async_Listen ( const char *  channel)

Definition at line 756 of file async.c.

References DEBUG1, elog, LISTEN_LISTEN, MyProcPid, queue_listen(), and Trace_notify.

Referenced by standard_ProcessUtility().

757 {
758  if (Trace_notify)
759  elog(DEBUG1, "Async_Listen(%s,%d)", channel, MyProcPid);
760 
761  queue_listen(LISTEN_LISTEN, channel);
762 }
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:40
static void queue_listen(ListenActionKind action, const char *channel)
Definition: async.c:708
bool Trace_notify
Definition: async.c:433
#define elog(elevel,...)
Definition: elog.h:214

◆ Async_Notify()

void Async_Notify ( const char *  channel,
const char *  payload 
)

Definition at line 609 of file async.c.

References AddEventToPendingNotifies(), AsyncExistsPendingNotify(), Notification::channel_len, CurTransactionContext, AsyncQueueEntry::data, Notification::data, DEBUG1, elog, ereport, errcode(), errmsg(), ERROR, NotificationList::events, GetCurrentTransactionNestLevel(), NotificationList::hashtab, IsParallelWorker, list_make1, MemoryContextAlloc(), MemoryContextSwitchTo(), NAMEDATALEN, NotificationList::nestingLevel, NOTIFY_PAYLOAD_MAX_LENGTH, offsetof, palloc(), Notification::payload_len, pendingNotifies, pfree(), TopTransactionContext, Trace_notify, and NotificationList::upper.

Referenced by pg_notify(), standard_ProcessUtility(), and triggered_change_notification().

610 {
611  int my_level = GetCurrentTransactionNestLevel();
612  size_t channel_len;
613  size_t payload_len;
614  Notification *n;
615  MemoryContext oldcontext;
616 
617  if (IsParallelWorker())
618  elog(ERROR, "cannot send notifications from a parallel worker");
619 
620  if (Trace_notify)
621  elog(DEBUG1, "Async_Notify(%s)", channel);
622 
623  channel_len = channel ? strlen(channel) : 0;
624  payload_len = payload ? strlen(payload) : 0;
625 
626  /* a channel name must be specified */
627  if (channel_len == 0)
628  ereport(ERROR,
629  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
630  errmsg("channel name cannot be empty")));
631 
632  /* enforce length limits */
633  if (channel_len >= NAMEDATALEN)
634  ereport(ERROR,
635  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
636  errmsg("channel name too long")));
637 
638  if (payload_len >= NOTIFY_PAYLOAD_MAX_LENGTH)
639  ereport(ERROR,
640  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
641  errmsg("payload string too long")));
642 
643  /*
644  * We must construct the Notification entry, even if we end up not using
645  * it, in order to compare it cheaply to existing list entries.
646  *
647  * The notification list needs to live until end of transaction, so store
648  * it in the transaction context.
649  */
651 
652  n = (Notification *) palloc(offsetof(Notification, data) +
653  channel_len + payload_len + 2);
654  n->channel_len = channel_len;
655  n->payload_len = payload_len;
656  strcpy(n->data, channel);
657  if (payload)
658  strcpy(n->data + channel_len + 1, payload);
659  else
660  n->data[channel_len + 1] = '\0';
661 
662  if (pendingNotifies == NULL || my_level > pendingNotifies->nestingLevel)
663  {
664  NotificationList *notifies;
665 
666  /*
667  * First notify event in current (sub)xact. Note that we allocate the
668  * NotificationList in TopTransactionContext; the nestingLevel might
669  * get changed later by AtSubCommit_Notify.
670  */
671  notifies = (NotificationList *)
673  sizeof(NotificationList));
674  notifies->nestingLevel = my_level;
675  notifies->events = list_make1(n);
676  /* We certainly don't need a hashtable yet */
677  notifies->hashtab = NULL;
678  notifies->upper = pendingNotifies;
679  pendingNotifies = notifies;
680  }
681  else
682  {
683  /* Now check for duplicates */
685  {
686  /* It's a dup, so forget it */
687  pfree(n);
688  MemoryContextSwitchTo(oldcontext);
689  return;
690  }
691 
692  /* Append more events to existing list */
694  }
695 
696  MemoryContextSwitchTo(oldcontext);
697 }
#define DEBUG1
Definition: elog.h:25
List * events
Definition: async.c:397
MemoryContext TopTransactionContext
Definition: mcxt.c:49
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: async.c:391
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
int errcode(int sqlerrcode)
Definition: elog.c:610
static bool AsyncExistsPendingNotify(Notification *n)
Definition: async.c:2299
int nestingLevel
Definition: async.c:396
#define list_make1(x1)
Definition: pg_list.h:227
#define NAMEDATALEN
void pfree(void *pointer)
Definition: mcxt.c:1057
static NotificationList * pendingNotifies
Definition: async.c:409
#define ERROR
Definition: elog.h:43
static void AddEventToPendingNotifies(Notification *n)
Definition: async.c:2340
#define NOTIFY_PAYLOAD_MAX_LENGTH
Definition: async.c:156
struct NotificationList * upper
Definition: async.c:399
#define IsParallelWorker()
Definition: parallel.h:61
#define ereport(elevel,...)
Definition: elog.h:144
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:857
HTAB * hashtab
Definition: async.c:398
uint16 channel_len
Definition: async.c:388
uint16 payload_len
Definition: async.c:389
bool Trace_notify
Definition: async.c:433
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:824
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
#define elog(elevel,...)
Definition: elog.h:214
#define offsetof(type, field)
Definition: c.h:668

◆ Async_Unlisten()

void Async_Unlisten ( const char *  channel)

Definition at line 770 of file async.c.

References DEBUG1, elog, LISTEN_UNLISTEN, MyProcPid, queue_listen(), Trace_notify, and unlistenExitRegistered.

Referenced by standard_ProcessUtility().

771 {
772  if (Trace_notify)
773  elog(DEBUG1, "Async_Unlisten(%s,%d)", channel, MyProcPid);
774 
775  /* If we couldn't possibly be listening, no need to queue anything */
776  if (pendingActions == NULL && !unlistenExitRegistered)
777  return;
778 
779  queue_listen(LISTEN_UNLISTEN, channel);
780 }
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:40
static void queue_listen(ListenActionKind action, const char *channel)
Definition: async.c:708
static ActionList * pendingActions
Definition: async.c:357
static bool unlistenExitRegistered
Definition: async.c:421
bool Trace_notify
Definition: async.c:433
#define elog(elevel,...)
Definition: elog.h:214

◆ Async_UnlistenAll()

void Async_UnlistenAll ( void  )

Definition at line 788 of file async.c.

References DEBUG1, elog, LISTEN_UNLISTEN_ALL, MyProcPid, queue_listen(), Trace_notify, and unlistenExitRegistered.

Referenced by DiscardAll(), and standard_ProcessUtility().

789 {
790  if (Trace_notify)
791  elog(DEBUG1, "Async_UnlistenAll(%d)", MyProcPid);
792 
793  /* If we couldn't possibly be listening, no need to queue anything */
794  if (pendingActions == NULL && !unlistenExitRegistered)
795  return;
796 
798 }
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:40
static void queue_listen(ListenActionKind action, const char *channel)
Definition: async.c:708
static ActionList * pendingActions
Definition: async.c:357
static bool unlistenExitRegistered
Definition: async.c:421
bool Trace_notify
Definition: async.c:433
#define elog(elevel,...)
Definition: elog.h:214

◆ AsyncShmemInit()

void AsyncShmemInit ( void  )

Definition at line 518 of file async.c.

References add_size(), asyncQueuePagePrecedes(), i, InvalidBackendId, InvalidOid, InvalidPid, AsyncQueueControl::lastQueueFillWarn, LWTRANCHE_NOTIFY_BUFFER, MaxBackends, mul_size(), NotifyCtl, NUM_NOTIFY_BUFFERS, offsetof, QUEUE_BACKEND_DBOID, QUEUE_BACKEND_PID, QUEUE_BACKEND_POS, QUEUE_FIRST_LISTENER, QUEUE_HEAD, QUEUE_NEXT_LISTENER, QUEUE_TAIL, SET_QUEUE_POS, ShmemInitStruct(), SimpleLruInit(), SlruScanDirCbDeleteAll(), SlruScanDirectory(), and SYNC_HANDLER_NONE.

Referenced by CreateSharedMemoryAndSemaphores().

519 {
520  bool found;
521  Size size;
522 
523  /*
524  * Create or attach to the AsyncQueueControl structure.
525  *
526  * The used entries in the backend[] array run from 1 to MaxBackends; the
527  * zero'th entry is unused but must be allocated.
528  */
529  size = mul_size(MaxBackends + 1, sizeof(QueueBackendStatus));
530  size = add_size(size, offsetof(AsyncQueueControl, backend));
531 
533  ShmemInitStruct("Async Queue Control", size, &found);
534 
535  if (!found)
536  {
537  /* First time through, so initialize it */
538  SET_QUEUE_POS(QUEUE_HEAD, 0, 0);
539  SET_QUEUE_POS(QUEUE_TAIL, 0, 0);
542  /* zero'th entry won't be used, but let's initialize it anyway */
543  for (int i = 0; i <= MaxBackends; i++)
544  {
549  }
550  }
551 
552  /*
553  * Set up SLRU management of the pg_notify data.
554  */
555  NotifyCtl->PagePrecedes = asyncQueuePagePrecedes;
557  NotifySLRULock, "pg_notify", LWTRANCHE_NOTIFY_BUFFER,
559 
560  if (!found)
561  {
562  /*
563  * During start or reboot, clean out the pg_notify directory.
564  */
566  }
567 }
#define QUEUE_TAIL
Definition: async.c:288
#define QUEUE_BACKEND_PID(i)
Definition: async.c:290
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage, void *data)
Definition: slru.c:1426
#define QUEUE_HEAD
Definition: async.c:287
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id, SyncRequestHandler sync_handler)
Definition: slru.c:186
#define NotifyCtl
Definition: async.c:300
#define NUM_NOTIFY_BUFFERS
Definition: async.h:21
#define QUEUE_FIRST_LISTENER
Definition: async.c:289
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:392
int MaxBackends
Definition: globals.c:136
#define SET_QUEUE_POS(x, y, z)
Definition: async.c:196
static bool asyncQueuePagePrecedes(int p, int q)
Definition: async.c:492
static AsyncQueueControl * asyncQueueControl
Definition: async.c:285
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
#define InvalidBackendId
Definition: backendid.h:23
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
#define QUEUE_BACKEND_DBOID(i)
Definition: async.c:291
#define InvalidOid
Definition: postgres_ext.h:36
#define QUEUE_NEXT_LISTENER(i)
Definition: async.c:292
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1449
size_t Size
Definition: c.h:473
int i
TimestampTz lastQueueFillWarn
Definition: async.c:280
#define QUEUE_BACKEND_POS(i)
Definition: async.c:293
#define offsetof(type, field)
Definition: c.h:668
#define InvalidPid
Definition: miscadmin.h:32

◆ AsyncShmemSize()

Size AsyncShmemSize ( void  )

Definition at line 501 of file async.c.

References add_size(), MaxBackends, mul_size(), NUM_NOTIFY_BUFFERS, offsetof, and SimpleLruShmemSize().

Referenced by CreateSharedMemoryAndSemaphores().

502 {
503  Size size;
504 
505  /* This had better match AsyncShmemInit */
506  size = mul_size(MaxBackends + 1, sizeof(QueueBackendStatus));
507  size = add_size(size, offsetof(AsyncQueueControl, backend));
508 
510 
511  return size;
512 }
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:155
#define NUM_NOTIFY_BUFFERS
Definition: async.h:21
int MaxBackends
Definition: globals.c:136
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
size_t Size
Definition: c.h:473
#define offsetof(type, field)
Definition: c.h:668

◆ AtAbort_Notify()

void AtAbort_Notify ( void  )

Definition at line 1742 of file async.c.

References amRegisteredListener, asyncQueueUnregister(), ClearPendingActionsAndNotifies(), and NIL.

Referenced by AbortTransaction().

1743 {
1744  /*
1745  * If we LISTEN but then roll back the transaction after PreCommit_Notify,
1746  * we have registered as a listener but have not made any entry in
1747  * listenChannels. In that case, deregister again.
1748  */
1751 
1752  /* And clean up */
1754 }
#define NIL
Definition: pg_list.h:65
static List * listenChannels
Definition: async.c:325
static void ClearPendingActionsAndNotifies(void)
Definition: async.c:2434
static void asyncQueueUnregister(void)
Definition: async.c:1300
static bool amRegisteredListener
Definition: async.c:424

◆ AtCommit_Notify()

void AtCommit_Notify ( void  )

Definition at line 981 of file async.c.

References ListenAction::action, ActionList::actions, amRegisteredListener, asyncQueueUnregister(), ListenAction::channel, ClearPendingActionsAndNotifies(), DEBUG1, elog, Exec_ListenCommit(), Exec_UnlistenAllCommit(), Exec_UnlistenCommit(), lfirst, LISTEN_LISTEN, LISTEN_UNLISTEN, LISTEN_UNLISTEN_ALL, NIL, and Trace_notify.

Referenced by CommitTransaction().

982 {
983  ListCell *p;
984 
985  /*
986  * Allow transactions that have not executed LISTEN/UNLISTEN/NOTIFY to
987  * return as soon as possible
988  */
990  return;
991 
992  if (Trace_notify)
993  elog(DEBUG1, "AtCommit_Notify");
994 
995  /* Perform any pending listen/unlisten actions */
996  if (pendingActions != NULL)
997  {
998  foreach(p, pendingActions->actions)
999  {
1000  ListenAction *actrec = (ListenAction *) lfirst(p);
1001 
1002  switch (actrec->action)
1003  {
1004  case LISTEN_LISTEN:
1005  Exec_ListenCommit(actrec->channel);
1006  break;
1007  case LISTEN_UNLISTEN:
1008  Exec_UnlistenCommit(actrec->channel);
1009  break;
1010  case LISTEN_UNLISTEN_ALL:
1012  break;
1013  }
1014  }
1015  }
1016 
1017  /* If no longer listening to anything, get out of listener array */
1020 
1021  /* And clean up */
1023 }
#define NIL
Definition: pg_list.h:65
#define DEBUG1
Definition: elog.h:25
static ActionList * pendingActions
Definition: async.c:357
static void Exec_UnlistenAllCommit(void)
Definition: async.c:1184
static List * listenChannels
Definition: async.c:325
static void ClearPendingActionsAndNotifies(void)
Definition: async.c:2434
static void Exec_UnlistenCommit(const char *channel)
Definition: async.c:1153
static NotificationList * pendingNotifies
Definition: async.c:409
List * actions
Definition: async.c:353
static void Exec_ListenCommit(const char *channel)
Definition: async.c:1126
static void asyncQueueUnregister(void)
Definition: async.c:1300
static bool amRegisteredListener
Definition: async.c:424
#define lfirst(lc)
Definition: pg_list.h:190
bool Trace_notify
Definition: async.c:433
#define elog(elevel,...)
Definition: elog.h:214
char channel[FLEXIBLE_ARRAY_MEMBER]
Definition: async.c:347
ListenActionKind action
Definition: async.c:346

◆ AtPrepare_Notify()

void AtPrepare_Notify ( void  )

Definition at line 854 of file async.c.

References ereport, errcode(), errmsg(), and ERROR.

Referenced by PrepareTransaction().

855 {
856  /* It's not allowed to have any pending LISTEN/UNLISTEN/NOTIFY actions */
858  ereport(ERROR,
859  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
860  errmsg("cannot PREPARE a transaction that has executed LISTEN, UNLISTEN, or NOTIFY")));
861 }
static ActionList * pendingActions
Definition: async.c:357
int errcode(int sqlerrcode)
Definition: elog.c:610
static NotificationList * pendingNotifies
Definition: async.c:409
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ AtSubAbort_Notify()

void AtSubAbort_Notify ( void  )

Definition at line 1832 of file async.c.

References GetCurrentTransactionNestLevel(), ActionList::nestingLevel, NotificationList::nestingLevel, pendingActions, pendingNotifies, pfree(), ActionList::upper, and NotificationList::upper.

Referenced by AbortSubTransaction().

1833 {
1834  int my_level = GetCurrentTransactionNestLevel();
1835 
1836  /*
1837  * All we have to do is pop the stack --- the actions/notifies made in
1838  * this subxact are no longer interesting, and the space will be freed
1839  * when CurTransactionContext is recycled. We still have to free the
1840  * ActionList and NotificationList objects themselves, though, because
1841  * those are allocated in TopTransactionContext.
1842  *
1843  * Note that there might be no entries at all, or no entries for the
1844  * current subtransaction level, either because none were ever created, or
1845  * because we reentered this routine due to trouble during subxact abort.
1846  */
1847  while (pendingActions != NULL &&
1848  pendingActions->nestingLevel >= my_level)
1849  {
1850  ActionList *childPendingActions = pendingActions;
1851 
1853  pfree(childPendingActions);
1854  }
1855 
1856  while (pendingNotifies != NULL &&
1857  pendingNotifies->nestingLevel >= my_level)
1858  {
1859  NotificationList *childPendingNotifies = pendingNotifies;
1860 
1862  pfree(childPendingNotifies);
1863  }
1864 }
static ActionList * pendingActions
Definition: async.c:357
int nestingLevel
Definition: async.c:396
void pfree(void *pointer)
Definition: mcxt.c:1057
static NotificationList * pendingNotifies
Definition: async.c:409
struct NotificationList * upper
Definition: async.c:399
int nestingLevel
Definition: async.c:352
struct ActionList * upper
Definition: async.c:354
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:857

◆ AtSubCommit_Notify()

void AtSubCommit_Notify ( void  )

Definition at line 1762 of file async.c.

References ActionList::actions, AddEventToPendingNotifies(), Assert, AsyncExistsPendingNotify(), NotificationList::events, GetCurrentTransactionNestLevel(), lfirst, list_concat(), ActionList::nestingLevel, NotificationList::nestingLevel, pendingActions, pendingNotifies, pfree(), ActionList::upper, and NotificationList::upper.

Referenced by CommitSubTransaction().

1763 {
1764  int my_level = GetCurrentTransactionNestLevel();
1765 
1766  /* If there are actions at our nesting level, we must reparent them. */
1767  if (pendingActions != NULL &&
1768  pendingActions->nestingLevel >= my_level)
1769  {
1770  if (pendingActions->upper == NULL ||
1771  pendingActions->upper->nestingLevel < my_level - 1)
1772  {
1773  /* nothing to merge; give the whole thing to the parent */
1775  }
1776  else
1777  {
1778  ActionList *childPendingActions = pendingActions;
1779 
1781 
1782  /*
1783  * Mustn't try to eliminate duplicates here --- see queue_listen()
1784  */
1787  childPendingActions->actions);
1788  pfree(childPendingActions);
1789  }
1790  }
1791 
1792  /* If there are notifies at our nesting level, we must reparent them. */
1793  if (pendingNotifies != NULL &&
1794  pendingNotifies->nestingLevel >= my_level)
1795  {
1796  Assert(pendingNotifies->nestingLevel == my_level);
1797 
1798  if (pendingNotifies->upper == NULL ||
1799  pendingNotifies->upper->nestingLevel < my_level - 1)
1800  {
1801  /* nothing to merge; give the whole thing to the parent */
1803  }
1804  else
1805  {
1806  /*
1807  * Formerly, we didn't bother to eliminate duplicates here, but
1808  * now we must, else we fall foul of "Assert(!found)", either here
1809  * or during a later attempt to build the parent-level hashtable.
1810  */
1811  NotificationList *childPendingNotifies = pendingNotifies;
1812  ListCell *l;
1813 
1815  /* Insert all the subxact's events into parent, except for dups */
1816  foreach(l, childPendingNotifies->events)
1817  {
1818  Notification *childn = (Notification *) lfirst(l);
1819 
1820  if (!AsyncExistsPendingNotify(childn))
1821  AddEventToPendingNotifies(childn);
1822  }
1823  pfree(childPendingNotifies);
1824  }
1825  }
1826 }
List * events
Definition: async.c:397
static ActionList * pendingActions
Definition: async.c:357
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
static bool AsyncExistsPendingNotify(Notification *n)
Definition: async.c:2299
int nestingLevel
Definition: async.c:396
void pfree(void *pointer)
Definition: mcxt.c:1057
static NotificationList * pendingNotifies
Definition: async.c:409
List * actions
Definition: async.c:353
static void AddEventToPendingNotifies(Notification *n)
Definition: async.c:2340
struct NotificationList * upper
Definition: async.c:399
int nestingLevel
Definition: async.c:352
struct ActionList * upper
Definition: async.c:354
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:857
#define Assert(condition)
Definition: c.h:745
#define lfirst(lc)
Definition: pg_list.h:190

◆ HandleNotifyInterrupt()

void HandleNotifyInterrupt ( void  )

Definition at line 1875 of file async.c.

References MyLatch, notifyInterruptPending, and SetLatch().

Referenced by procsignal_sigusr1_handler().

1876 {
1877  /*
1878  * Note: this is called by a SIGNAL HANDLER. You must be very wary what
1879  * you do here.
1880  */
1881 
1882  /* signal that work needs to be done */
1883  notifyInterruptPending = true;
1884 
1885  /* make sure the event is processed in due course */
1886  SetLatch(MyLatch);
1887 }
void SetLatch(Latch *latch)
Definition: latch.c:505
struct Latch * MyLatch
Definition: globals.c:54
volatile sig_atomic_t notifyInterruptPending
Definition: async.c:418

◆ NotifyMyFrontEnd()

void NotifyMyFrontEnd ( const char *  channel,
const char *  payload,
int32  srcPid 
)

Definition at line 2274 of file async.c.

References buf, DestRemote, elog, FrontendProtocol, INFO, PG_PROTOCOL_MAJOR, pq_beginmessage(), pq_endmessage(), pq_sendint32(), pq_sendstring(), and whereToSendOutput.

Referenced by asyncQueueProcessPageEntries(), and HandleParallelMessage().

2275 {
2277  {
2279 
2280  pq_beginmessage(&buf, 'A');
2281  pq_sendint32(&buf, srcPid);
2282  pq_sendstring(&buf, channel);
2284  pq_sendstring(&buf, payload);
2285  pq_endmessage(&buf);
2286 
2287  /*
2288  * NOTE: we do not do pq_flush() here. For a self-notify, it will
2289  * happen at the end of the transaction, and for incoming notifies
2290  * ProcessIncomingNotify will do it after finding all the notifies.
2291  */
2292  }
2293  else
2294  elog(INFO, "NOTIFY for \"%s\" payload \"%s\"", channel, payload);
2295 }
#define INFO
Definition: elog.h:33
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:87
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static char * buf
Definition: pg_test_fsync.c:67
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:298
#define elog(elevel,...)
Definition: elog.h:214
CommandDest whereToSendOutput
Definition: postgres.c:91
ProtocolVersion FrontendProtocol
Definition: globals.c:28

◆ PreCommit_Notify()

void PreCommit_Notify ( void  )

Definition at line 879 of file async.c.

References AccessExclusiveLock, ListenAction::action, ActionList::actions, asyncQueueAddEntries(), asyncQueueFillWarning(), asyncQueueIsFull(), backendHasSentNotifications, DEBUG1, elog, ereport, errcode(), errmsg(), ERROR, NotificationList::events, Exec_ListenPreCommit(), GetCurrentTransactionId(), InvalidOid, lfirst, list_head(), LISTEN_LISTEN, LISTEN_UNLISTEN, LISTEN_UNLISTEN_ALL, LockSharedObject(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), and Trace_notify.

Referenced by CommitTransaction().

880 {
881  ListCell *p;
882 
884  return; /* no relevant statements in this xact */
885 
886  if (Trace_notify)
887  elog(DEBUG1, "PreCommit_Notify");
888 
889  /* Preflight for any pending listen/unlisten actions */
890  if (pendingActions != NULL)
891  {
892  foreach(p, pendingActions->actions)
893  {
894  ListenAction *actrec = (ListenAction *) lfirst(p);
895 
896  switch (actrec->action)
897  {
898  case LISTEN_LISTEN:
900  break;
901  case LISTEN_UNLISTEN:
902  /* there is no Exec_UnlistenPreCommit() */
903  break;
904  case LISTEN_UNLISTEN_ALL:
905  /* there is no Exec_UnlistenAllPreCommit() */
906  break;
907  }
908  }
909  }
910 
911  /* Queue any pending notifies (must happen after the above) */
912  if (pendingNotifies)
913  {
914  ListCell *nextNotify;
915 
916  /*
917  * Make sure that we have an XID assigned to the current transaction.
918  * GetCurrentTransactionId is cheap if we already have an XID, but not
919  * so cheap if we don't, and we'd prefer not to do that work while
920  * holding NotifyQueueLock.
921  */
922  (void) GetCurrentTransactionId();
923 
924  /*
925  * Serialize writers by acquiring a special lock that we hold till
926  * after commit. This ensures that queue entries appear in commit
927  * order, and in particular that there are never uncommitted queue
928  * entries ahead of committed ones, so an uncommitted transaction
929  * can't block delivery of deliverable notifications.
930  *
931  * We use a heavyweight lock so that it'll automatically be released
932  * after either commit or abort. This also allows deadlocks to be
933  * detected, though really a deadlock shouldn't be possible here.
934  *
935  * The lock is on "database 0", which is pretty ugly but it doesn't
936  * seem worth inventing a special locktag category just for this.
937  * (Historical note: before PG 9.0, a similar lock on "database 0" was
938  * used by the flatfiles mechanism.)
939  */
940  LockSharedObject(DatabaseRelationId, InvalidOid, 0,
942 
943  /* Now push the notifications into the queue */
945 
946  nextNotify = list_head(pendingNotifies->events);
947  while (nextNotify != NULL)
948  {
949  /*
950  * Add the pending notifications to the queue. We acquire and
951  * release NotifyQueueLock once per page, which might be overkill
952  * but it does allow readers to get in while we're doing this.
953  *
954  * A full queue is very uncommon and should really not happen,
955  * given that we have so much space available in the SLRU pages.
956  * Nevertheless we need to deal with this possibility. Note that
957  * when we get here we are in the process of committing our
958  * transaction, but we have not yet committed to clog, so at this
959  * point in time we can still roll the transaction back.
960  */
961  LWLockAcquire(NotifyQueueLock, LW_EXCLUSIVE);
963  if (asyncQueueIsFull())
964  ereport(ERROR,
965  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
966  errmsg("too many notifications in the NOTIFY queue")));
967  nextNotify = asyncQueueAddEntries(nextNotify);
968  LWLockRelease(NotifyQueueLock);
969  }
970  }
971 }
#define DEBUG1
Definition: elog.h:25
List * events
Definition: async.c:397
static ActionList * pendingActions
Definition: async.c:357
int errcode(int sqlerrcode)
Definition: elog.c:610
static bool asyncQueueIsFull(void)
Definition: async.c:1341
static void asyncQueueFillWarning(void)
Definition: async.c:1606
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
static NotificationList * pendingNotifies
Definition: async.c:409
#define ERROR
Definition: elog.h:43
static bool backendHasSentNotifications
Definition: async.c:427
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:438
List * actions
Definition: async.c:353
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1017
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
static ListCell * asyncQueueAddEntries(ListCell *nextNotify)
Definition: async.c:1443
#define lfirst(lc)
Definition: pg_list.h:190
static void Exec_ListenPreCommit(void)
Definition: async.c:1031
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
bool Trace_notify
Definition: async.c:433
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
ListenActionKind action
Definition: async.c:346

◆ ProcessCompletedNotifies()

void ProcessCompletedNotifies ( void  )

Definition at line 1217 of file async.c.

References asyncQueueAdvanceTail(), asyncQueueReadAllNotifications(), backendHasSentNotifications, backendTryAdvanceTail, CommitTransactionCommand(), CurrentMemoryContext, DEBUG1, elog, MemoryContextSwitchTo(), NIL, SignalBackends(), StartTransactionCommand(), and Trace_notify.

Referenced by PostgresMain().

1218 {
1219  MemoryContext caller_context;
1220 
1221  /* Nothing to do if we didn't send any notifications */
1223  return;
1224 
1225  /*
1226  * We reset the flag immediately; otherwise, if any sort of error occurs
1227  * below, we'd be locked up in an infinite loop, because control will come
1228  * right back here after error cleanup.
1229  */
1231 
1232  /*
1233  * We must preserve the caller's memory context (probably MessageContext)
1234  * across the transaction we do here.
1235  */
1236  caller_context = CurrentMemoryContext;
1237 
1238  if (Trace_notify)
1239  elog(DEBUG1, "ProcessCompletedNotifies");
1240 
1241  /*
1242  * We must run asyncQueueReadAllNotifications inside a transaction, else
1243  * bad things happen if it gets an error.
1244  */
1246 
1247  /* Send signals to other backends */
1248  SignalBackends();
1249 
1250  if (listenChannels != NIL)
1251  {
1252  /* Read the queue ourselves, and send relevant stuff to the frontend */
1254  }
1255 
1256  /*
1257  * If it's time to try to advance the global tail pointer, do that.
1258  */
1260  {
1261  backendTryAdvanceTail = false;
1263  }
1264 
1266 
1267  MemoryContextSwitchTo(caller_context);
1268 
1269  /* We don't need pq_flush() here since postgres.c will do one shortly */
1270 }
#define NIL
Definition: pg_list.h:65
#define DEBUG1
Definition: elog.h:25
void CommitTransactionCommand(void)
Definition: xact.c:2947
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static List * listenChannels
Definition: async.c:325
static bool backendHasSentNotifications
Definition: async.c:427
static bool backendTryAdvanceTail
Definition: async.c:430
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void asyncQueueAdvanceTail(void)
Definition: async.c:2171
static void SignalBackends(void)
Definition: async.c:1659
static void asyncQueueReadAllNotifications(void)
Definition: async.c:1917
void StartTransactionCommand(void)
Definition: xact.c:2846
bool Trace_notify
Definition: async.c:433
#define elog(elevel,...)
Definition: elog.h:214

◆ ProcessNotifyInterrupt()

void ProcessNotifyInterrupt ( void  )

Definition at line 1901 of file async.c.

References IsTransactionOrTransactionBlock(), notifyInterruptPending, and ProcessIncomingNotify().

Referenced by PostgresMain(), and ProcessClientReadInterrupt().

1902 {
1904  return; /* not really idle */
1905 
1906  while (notifyInterruptPending)
1908 }
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4702
static void ProcessIncomingNotify(void)
Definition: async.c:2235
volatile sig_atomic_t notifyInterruptPending
Definition: async.c:418

Variable Documentation

◆ notifyInterruptPending

volatile sig_atomic_t notifyInterruptPending

◆ Trace_notify