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.

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 HandleNotifyInterrupt (void)
 
void ProcessNotifyInterrupt (bool flush)
 
void AsyncNotifyFreezeXids (TransactionId newFrozenXid)
 

Variables

PGDLLIMPORT bool Trace_notify
 
PGDLLIMPORT int max_notify_queue_pages
 
PGDLLIMPORT volatile sig_atomic_t notifyInterruptPending
 

Function Documentation

◆ Async_Listen()

void Async_Listen ( const char *  channel)

Definition at line 737 of file async.c.

738{
739 if (Trace_notify)
740 elog(DEBUG1, "Async_Listen(%s,%d)", channel, MyProcPid);
741
742 queue_listen(LISTEN_LISTEN, channel);
743}
@ LISTEN_LISTEN
Definition: async.c:334
bool Trace_notify
Definition: async.c:425
static void queue_listen(ListenActionKind action, const char *channel)
Definition: async.c:689
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:226
int MyProcPid
Definition: globals.c:47

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

Referenced by standard_ProcessUtility().

◆ Async_Notify()

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

Definition at line 590 of file async.c.

591{
592 int my_level = GetCurrentTransactionNestLevel();
593 size_t channel_len;
594 size_t payload_len;
595 Notification *n;
596 MemoryContext oldcontext;
597
598 if (IsParallelWorker())
599 elog(ERROR, "cannot send notifications from a parallel worker");
600
601 if (Trace_notify)
602 elog(DEBUG1, "Async_Notify(%s)", channel);
603
604 channel_len = channel ? strlen(channel) : 0;
605 payload_len = payload ? strlen(payload) : 0;
606
607 /* a channel name must be specified */
608 if (channel_len == 0)
610 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
611 errmsg("channel name cannot be empty")));
612
613 /* enforce length limits */
614 if (channel_len >= NAMEDATALEN)
616 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
617 errmsg("channel name too long")));
618
619 if (payload_len >= NOTIFY_PAYLOAD_MAX_LENGTH)
621 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
622 errmsg("payload string too long")));
623
624 /*
625 * We must construct the Notification entry, even if we end up not using
626 * it, in order to compare it cheaply to existing list entries.
627 *
628 * The notification list needs to live until end of transaction, so store
629 * it in the transaction context.
630 */
632
633 n = (Notification *) palloc(offsetof(Notification, data) +
634 channel_len + payload_len + 2);
635 n->channel_len = channel_len;
636 n->payload_len = payload_len;
637 strcpy(n->data, channel);
638 if (payload)
639 strcpy(n->data + channel_len + 1, payload);
640 else
641 n->data[channel_len + 1] = '\0';
642
643 if (pendingNotifies == NULL || my_level > pendingNotifies->nestingLevel)
644 {
645 NotificationList *notifies;
646
647 /*
648 * First notify event in current (sub)xact. Note that we allocate the
649 * NotificationList in TopTransactionContext; the nestingLevel might
650 * get changed later by AtSubCommit_Notify.
651 */
652 notifies = (NotificationList *)
654 sizeof(NotificationList));
655 notifies->nestingLevel = my_level;
656 notifies->events = list_make1(n);
657 /* We certainly don't need a hashtable yet */
658 notifies->hashtab = NULL;
659 notifies->upper = pendingNotifies;
660 pendingNotifies = notifies;
661 }
662 else
663 {
664 /* Now check for duplicates */
666 {
667 /* It's a dup, so forget it */
668 pfree(n);
669 MemoryContextSwitchTo(oldcontext);
670 return;
671 }
672
673 /* Append more events to existing list */
675 }
676
677 MemoryContextSwitchTo(oldcontext);
678}
static bool AsyncExistsPendingNotify(Notification *n)
Definition: async.c:2368
static NotificationList * pendingNotifies
Definition: async.c:404
static void AddEventToPendingNotifies(Notification *n)
Definition: async.c:2409
#define NOTIFY_PAYLOAD_MAX_LENGTH
Definition: async.c:163
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
#define IsParallelWorker()
Definition: parallel.h:60
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1229
MemoryContext TopTransactionContext
Definition: mcxt.c:171
void pfree(void *pointer)
Definition: mcxt.c:1594
void * palloc(Size size)
Definition: mcxt.c:1365
MemoryContext CurTransactionContext
Definition: mcxt.c:172
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define NAMEDATALEN
const void * data
#define list_make1(x1)
Definition: pg_list.h:212
int nestingLevel
Definition: async.c:391
HTAB * hashtab
Definition: async.c:393
List * events
Definition: async.c:392
struct NotificationList * upper
Definition: async.c:394
uint16 payload_len
Definition: async.c:384
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: async.c:386
uint16 channel_len
Definition: async.c:383
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:930

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

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

◆ Async_Unlisten()

void Async_Unlisten ( const char *  channel)

Definition at line 751 of file async.c.

752{
753 if (Trace_notify)
754 elog(DEBUG1, "Async_Unlisten(%s,%d)", channel, MyProcPid);
755
756 /* If we couldn't possibly be listening, no need to queue anything */
758 return;
759
761}
static ActionList * pendingActions
Definition: async.c:352
@ LISTEN_UNLISTEN
Definition: async.c:335
static bool unlistenExitRegistered
Definition: async.c:416

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

Referenced by standard_ProcessUtility().

◆ Async_UnlistenAll()

void Async_UnlistenAll ( void  )

Definition at line 769 of file async.c.

770{
771 if (Trace_notify)
772 elog(DEBUG1, "Async_UnlistenAll(%d)", MyProcPid);
773
774 /* If we couldn't possibly be listening, no need to queue anything */
776 return;
777
779}
@ LISTEN_UNLISTEN_ALL
Definition: async.c:336

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

Referenced by DiscardAll(), and standard_ProcessUtility().

◆ AsyncNotifyFreezeXids()

void AsyncNotifyFreezeXids ( TransactionId  newFrozenXid)

Definition at line 2196 of file async.c.

2197{
2198 QueuePosition pos;
2199 QueuePosition head;
2200 int64 curpage = -1;
2201 int slotno = -1;
2202 char *page_buffer = NULL;
2203 bool page_dirty = false;
2204
2205 /*
2206 * Acquire locks in the correct order to avoid deadlocks. As per the
2207 * locking protocol: NotifyQueueTailLock, then NotifyQueueLock, then SLRU
2208 * bank locks.
2209 *
2210 * We only need SHARED mode since we're just reading the head/tail
2211 * positions, not modifying them.
2212 */
2213 LWLockAcquire(NotifyQueueTailLock, LW_SHARED);
2214 LWLockAcquire(NotifyQueueLock, LW_SHARED);
2215
2216 pos = QUEUE_TAIL;
2217 head = QUEUE_HEAD;
2218
2219 /* Release NotifyQueueLock early, we only needed to read the positions */
2220 LWLockRelease(NotifyQueueLock);
2221
2222 /*
2223 * Scan the queue from tail to head, freezing XIDs as needed. We hold
2224 * NotifyQueueTailLock throughout to ensure the tail doesn't move while
2225 * we're working.
2226 */
2227 while (!QUEUE_POS_EQUAL(pos, head))
2228 {
2229 AsyncQueueEntry *qe;
2230 TransactionId xid;
2231 int64 pageno = QUEUE_POS_PAGE(pos);
2232 int offset = QUEUE_POS_OFFSET(pos);
2233
2234 /* If we need a different page, release old lock and get new one */
2235 if (pageno != curpage)
2236 {
2237 LWLock *lock;
2238
2239 /* Release previous page if any */
2240 if (slotno >= 0)
2241 {
2242 if (page_dirty)
2243 {
2244 NotifyCtl->shared->page_dirty[slotno] = true;
2245 page_dirty = false;
2246 }
2248 }
2249
2250 lock = SimpleLruGetBankLock(NotifyCtl, pageno);
2252 slotno = SimpleLruReadPage(NotifyCtl, pageno, true,
2254 page_buffer = NotifyCtl->shared->page_buffer[slotno];
2255 curpage = pageno;
2256 }
2257
2258 qe = (AsyncQueueEntry *) (page_buffer + offset);
2259 xid = qe->xid;
2260
2261 if (TransactionIdIsNormal(xid) &&
2262 TransactionIdPrecedes(xid, newFrozenXid))
2263 {
2264 if (TransactionIdDidCommit(xid))
2265 {
2267 page_dirty = true;
2268 }
2269 else
2270 {
2272 page_dirty = true;
2273 }
2274 }
2275
2276 /* Advance to next entry */
2277 asyncQueueAdvance(&pos, qe->length);
2278 }
2279
2280 /* Release final page lock if we acquired one */
2281 if (slotno >= 0)
2282 {
2283 if (page_dirty)
2284 NotifyCtl->shared->page_dirty[slotno] = true;
2286 }
2287
2288 LWLockRelease(NotifyQueueTailLock);
2289}
#define QUEUE_POS_OFFSET(x)
Definition: async.c:201
static bool asyncQueueAdvance(volatile QueuePosition *position, int entryLength)
Definition: async.c:1286
#define QUEUE_TAIL
Definition: async.c:297
#define QUEUE_POS_PAGE(x)
Definition: async.c:200
#define NotifyCtl
Definition: async.c:310
#define QUEUE_HEAD
Definition: async.c:296
#define QUEUE_POS_EQUAL(x, y)
Definition: async.c:209
int64_t int64
Definition: c.h:540
uint32 TransactionId
Definition: c.h:662
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_SHARED
Definition: lwlock.h:113
@ LW_EXCLUSIVE
Definition: lwlock.h:112
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, TransactionId xid)
Definition: slru.c:527
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
Definition: slru.h:160
TransactionId xid
Definition: async.c:181
Definition: lwlock.h:42
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:126
#define FrozenTransactionId
Definition: transam.h:33
#define InvalidTransactionId
Definition: transam.h:31
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.h:263

References asyncQueueAdvance(), FrozenTransactionId, InvalidTransactionId, AsyncQueueEntry::length, LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), NotifyCtl, QUEUE_HEAD, QUEUE_POS_EQUAL, QUEUE_POS_OFFSET, QUEUE_POS_PAGE, QUEUE_TAIL, SimpleLruGetBankLock(), SimpleLruReadPage(), TransactionIdDidCommit(), TransactionIdIsNormal, TransactionIdPrecedes(), and AsyncQueueEntry::xid.

Referenced by vac_truncate_clog().

◆ AsyncShmemInit()

void AsyncShmemInit ( void  )

Definition at line 501 of file async.c.

502{
503 bool found;
504 Size size;
505
506 /*
507 * Create or attach to the AsyncQueueControl structure.
508 */
509 size = mul_size(MaxBackends, sizeof(QueueBackendStatus));
510 size = add_size(size, offsetof(AsyncQueueControl, backend));
511
513 ShmemInitStruct("Async Queue Control", size, &found);
514
515 if (!found)
516 {
517 /* First time through, so initialize it */
520 QUEUE_STOP_PAGE = 0;
523 for (int i = 0; i < MaxBackends; i++)
524 {
529 }
530 }
531
532 /*
533 * Set up SLRU management of the pg_notify data. Note that long segment
534 * names are used in order to avoid wraparound.
535 */
536 NotifyCtl->PagePrecedes = asyncQueuePagePrecedes;
538 "pg_notify", LWTRANCHE_NOTIFY_BUFFER, LWTRANCHE_NOTIFY_SLRU,
539 SYNC_HANDLER_NONE, true);
540
541 if (!found)
542 {
543 /*
544 * During start or reboot, clean out the pg_notify directory.
545 */
547 }
548}
#define QUEUE_FIRST_LISTENER
Definition: async.c:299
#define QUEUE_BACKEND_POS(i)
Definition: async.c:303
#define SET_QUEUE_POS(x, y, z)
Definition: async.c:203
static AsyncQueueControl * asyncQueueControl
Definition: async.c:294
static bool asyncQueuePagePrecedes(int64 p, int64 q)
Definition: async.c:475
#define QUEUE_BACKEND_PID(i)
Definition: async.c:300
#define QUEUE_NEXT_LISTENER(i)
Definition: async.c:302
#define QUEUE_BACKEND_DBOID(i)
Definition: async.c:301
#define QUEUE_STOP_PAGE
Definition: async.c:298
size_t Size
Definition: c.h:615
int MaxBackends
Definition: globals.c:146
int notify_buffers
Definition: globals.c:164
int i
Definition: isn.c:77
#define InvalidPid
Definition: miscadmin.h:32
#define InvalidOid
Definition: postgres_ext.h:37
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
Size add_size(Size s1, Size s2)
Definition: shmem.c:494
Size mul_size(Size s1, Size s2)
Definition: shmem.c:511
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:388
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, const char *subdir, int buffer_tranche_id, int bank_tranche_id, SyncRequestHandler sync_handler, bool long_segment_names)
Definition: slru.c:252
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1816
bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition: slru.c:1769
TimestampTz lastQueueFillWarn
Definition: async.c:290
@ SYNC_HANDLER_NONE
Definition: sync.h:42

References add_size(), asyncQueueControl, asyncQueuePagePrecedes(), i, INVALID_PROC_NUMBER, InvalidOid, InvalidPid, AsyncQueueControl::lastQueueFillWarn, MaxBackends, mul_size(), notify_buffers, NotifyCtl, QUEUE_BACKEND_DBOID, QUEUE_BACKEND_PID, QUEUE_BACKEND_POS, QUEUE_FIRST_LISTENER, QUEUE_HEAD, QUEUE_NEXT_LISTENER, QUEUE_STOP_PAGE, QUEUE_TAIL, SET_QUEUE_POS, ShmemInitStruct(), SimpleLruInit(), SlruScanDirCbDeleteAll(), SlruScanDirectory(), and SYNC_HANDLER_NONE.

Referenced by CreateOrAttachShmemStructs().

◆ AsyncShmemSize()

Size AsyncShmemSize ( void  )

Definition at line 484 of file async.c.

485{
486 Size size;
487
488 /* This had better match AsyncShmemInit */
489 size = mul_size(MaxBackends, sizeof(QueueBackendStatus));
490 size = add_size(size, offsetof(AsyncQueueControl, backend));
491
493
494 return size;
495}
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:198

References add_size(), MaxBackends, mul_size(), notify_buffers, and SimpleLruShmemSize().

Referenced by CalculateShmemSize().

◆ AtAbort_Notify()

void AtAbort_Notify ( void  )

Definition at line 1671 of file async.c.

1672{
1673 /*
1674 * If we LISTEN but then roll back the transaction after PreCommit_Notify,
1675 * we have registered as a listener but have not made any entry in
1676 * listenChannels. In that case, deregister again.
1677 */
1680
1681 /* And clean up */
1683}
static void ClearPendingActionsAndNotifies(void)
Definition: async.c:2498
static List * listenChannels
Definition: async.c:320
static bool amRegisteredListener
Definition: async.c:419
static void asyncQueueUnregister(void)
Definition: async.c:1230
#define NIL
Definition: pg_list.h:68

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

Referenced by AbortTransaction().

◆ AtCommit_Notify()

void AtCommit_Notify ( void  )

Definition at line 967 of file async.c.

968{
969 ListCell *p;
970
971 /*
972 * Allow transactions that have not executed LISTEN/UNLISTEN/NOTIFY to
973 * return as soon as possible
974 */
976 return;
977
978 if (Trace_notify)
979 elog(DEBUG1, "AtCommit_Notify");
980
981 /* Perform any pending listen/unlisten actions */
982 if (pendingActions != NULL)
983 {
984 foreach(p, pendingActions->actions)
985 {
986 ListenAction *actrec = (ListenAction *) lfirst(p);
987
988 switch (actrec->action)
989 {
990 case LISTEN_LISTEN:
991 Exec_ListenCommit(actrec->channel);
992 break;
993 case LISTEN_UNLISTEN:
995 break;
998 break;
999 }
1000 }
1001 }
1002
1003 /* If no longer listening to anything, get out of listener array */
1006
1007 /*
1008 * Send signals to listening backends. We need do this only if there are
1009 * pending notifies, which were previously added to the shared queue by
1010 * PreCommit_Notify().
1011 */
1012 if (pendingNotifies != NULL)
1014
1015 /*
1016 * If it's time to try to advance the global tail pointer, do that.
1017 *
1018 * (It might seem odd to do this in the sender, when more than likely the
1019 * listeners won't yet have read the messages we just sent. However,
1020 * there's less contention if only the sender does it, and there is little
1021 * need for urgency in advancing the global tail. So this typically will
1022 * be clearing out messages that were sent some time ago.)
1023 */
1024 if (tryAdvanceTail)
1025 {
1026 tryAdvanceTail = false;
1028 }
1029
1030 /* And clean up */
1032}
static void SignalBackends(void)
Definition: async.c:1581
static void Exec_ListenCommit(const char *channel)
Definition: async.c:1135
static bool tryAdvanceTail
Definition: async.c:422
static void Exec_UnlistenCommit(const char *channel)
Definition: async.c:1162
static void asyncQueueAdvanceTail(void)
Definition: async.c:2114
static void Exec_UnlistenAllCommit(void)
Definition: async.c:1193
#define lfirst(lc)
Definition: pg_list.h:172
List * actions
Definition: async.c:348
char channel[FLEXIBLE_ARRAY_MEMBER]
Definition: async.c:342
ListenActionKind action
Definition: async.c:341

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

Referenced by CommitTransaction().

◆ AtPrepare_Notify()

void AtPrepare_Notify ( void  )

Definition at line 835 of file async.c.

836{
837 /* It's not allowed to have any pending LISTEN/UNLISTEN/NOTIFY actions */
840 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
841 errmsg("cannot PREPARE a transaction that has executed LISTEN, UNLISTEN, or NOTIFY")));
842}

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

Referenced by PrepareTransaction().

◆ AtSubAbort_Notify()

void AtSubAbort_Notify ( void  )

Definition at line 1761 of file async.c.

1762{
1763 int my_level = GetCurrentTransactionNestLevel();
1764
1765 /*
1766 * All we have to do is pop the stack --- the actions/notifies made in
1767 * this subxact are no longer interesting, and the space will be freed
1768 * when CurTransactionContext is recycled. We still have to free the
1769 * ActionList and NotificationList objects themselves, though, because
1770 * those are allocated in TopTransactionContext.
1771 *
1772 * Note that there might be no entries at all, or no entries for the
1773 * current subtransaction level, either because none were ever created, or
1774 * because we reentered this routine due to trouble during subxact abort.
1775 */
1776 while (pendingActions != NULL &&
1777 pendingActions->nestingLevel >= my_level)
1778 {
1779 ActionList *childPendingActions = pendingActions;
1780
1782 pfree(childPendingActions);
1783 }
1784
1785 while (pendingNotifies != NULL &&
1786 pendingNotifies->nestingLevel >= my_level)
1787 {
1788 NotificationList *childPendingNotifies = pendingNotifies;
1789
1791 pfree(childPendingNotifies);
1792 }
1793}
int nestingLevel
Definition: async.c:347
struct ActionList * upper
Definition: async.c:349

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

Referenced by AbortSubTransaction().

◆ AtSubCommit_Notify()

void AtSubCommit_Notify ( void  )

Definition at line 1691 of file async.c.

1692{
1693 int my_level = GetCurrentTransactionNestLevel();
1694
1695 /* If there are actions at our nesting level, we must reparent them. */
1696 if (pendingActions != NULL &&
1697 pendingActions->nestingLevel >= my_level)
1698 {
1699 if (pendingActions->upper == NULL ||
1700 pendingActions->upper->nestingLevel < my_level - 1)
1701 {
1702 /* nothing to merge; give the whole thing to the parent */
1704 }
1705 else
1706 {
1707 ActionList *childPendingActions = pendingActions;
1708
1710
1711 /*
1712 * Mustn't try to eliminate duplicates here --- see queue_listen()
1713 */
1716 childPendingActions->actions);
1717 pfree(childPendingActions);
1718 }
1719 }
1720
1721 /* If there are notifies at our nesting level, we must reparent them. */
1722 if (pendingNotifies != NULL &&
1723 pendingNotifies->nestingLevel >= my_level)
1724 {
1725 Assert(pendingNotifies->nestingLevel == my_level);
1726
1727 if (pendingNotifies->upper == NULL ||
1728 pendingNotifies->upper->nestingLevel < my_level - 1)
1729 {
1730 /* nothing to merge; give the whole thing to the parent */
1732 }
1733 else
1734 {
1735 /*
1736 * Formerly, we didn't bother to eliminate duplicates here, but
1737 * now we must, else we fall foul of "Assert(!found)", either here
1738 * or during a later attempt to build the parent-level hashtable.
1739 */
1740 NotificationList *childPendingNotifies = pendingNotifies;
1741 ListCell *l;
1742
1744 /* Insert all the subxact's events into parent, except for dups */
1745 foreach(l, childPendingNotifies->events)
1746 {
1747 Notification *childn = (Notification *) lfirst(l);
1748
1749 if (!AsyncExistsPendingNotify(childn))
1751 }
1752 pfree(childPendingNotifies);
1753 }
1754 }
1755}
Assert(PointerIsAligned(start, uint64))
List * list_concat(List *list1, const List *list2)
Definition: list.c:561

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().

◆ HandleNotifyInterrupt()

void HandleNotifyInterrupt ( void  )

Definition at line 1804 of file async.c.

1805{
1806 /*
1807 * Note: this is called by a SIGNAL HANDLER. You must be very wary what
1808 * you do here.
1809 */
1810
1811 /* signal that work needs to be done */
1813
1814 /* make sure the event is processed in due course */
1816}
volatile sig_atomic_t notifyInterruptPending
Definition: async.c:413
struct Latch * MyLatch
Definition: globals.c:63
void SetLatch(Latch *latch)
Definition: latch.c:290

References MyLatch, notifyInterruptPending, and SetLatch().

Referenced by procsignal_sigusr1_handler().

◆ NotifyMyFrontEnd()

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

Definition at line 2344 of file async.c.

2345{
2347 {
2349
2351 pq_sendint32(&buf, srcPid);
2352 pq_sendstring(&buf, channel);
2353 pq_sendstring(&buf, payload);
2355
2356 /*
2357 * NOTE: we do not do pq_flush() here. Some level of caller will
2358 * handle it later, allowing this message to be combined into a packet
2359 * with other ones.
2360 */
2361 }
2362 else
2363 elog(INFO, "NOTIFY for \"%s\" payload \"%s\"", channel, payload);
2364}
@ DestRemote
Definition: dest.h:89
#define INFO
Definition: elog.h:34
static char * buf
Definition: pg_test_fsync.c:72
CommandDest whereToSendOutput
Definition: postgres.c:92
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:195
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:296
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:144
#define PqMsg_NotificationResponse
Definition: protocol.h:41

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

Referenced by asyncQueueProcessPageEntries(), and ProcessParallelMessage().

◆ PreCommit_Notify()

void PreCommit_Notify ( void  )

Definition at line 860 of file async.c.

861{
862 ListCell *p;
863
865 return; /* no relevant statements in this xact */
866
867 if (Trace_notify)
868 elog(DEBUG1, "PreCommit_Notify");
869
870 /* Preflight for any pending listen/unlisten actions */
871 if (pendingActions != NULL)
872 {
873 foreach(p, pendingActions->actions)
874 {
875 ListenAction *actrec = (ListenAction *) lfirst(p);
876
877 switch (actrec->action)
878 {
879 case LISTEN_LISTEN:
881 break;
882 case LISTEN_UNLISTEN:
883 /* there is no Exec_UnlistenPreCommit() */
884 break;
886 /* there is no Exec_UnlistenAllPreCommit() */
887 break;
888 }
889 }
890 }
891
892 /* Queue any pending notifies (must happen after the above) */
893 if (pendingNotifies)
894 {
895 ListCell *nextNotify;
896
897 /*
898 * Make sure that we have an XID assigned to the current transaction.
899 * GetCurrentTransactionId is cheap if we already have an XID, but not
900 * so cheap if we don't, and we'd prefer not to do that work while
901 * holding NotifyQueueLock.
902 */
904
905 /*
906 * Serialize writers by acquiring a special lock that we hold till
907 * after commit. This ensures that queue entries appear in commit
908 * order, and in particular that there are never uncommitted queue
909 * entries ahead of committed ones, so an uncommitted transaction
910 * can't block delivery of deliverable notifications.
911 *
912 * We use a heavyweight lock so that it'll automatically be released
913 * after either commit or abort. This also allows deadlocks to be
914 * detected, though really a deadlock shouldn't be possible here.
915 *
916 * The lock is on "database 0", which is pretty ugly but it doesn't
917 * seem worth inventing a special locktag category just for this.
918 * (Historical note: before PG 9.0, a similar lock on "database 0" was
919 * used by the flatfiles mechanism.)
920 */
921 LockSharedObject(DatabaseRelationId, InvalidOid, 0,
923
924 /* Now push the notifications into the queue */
925 nextNotify = list_head(pendingNotifies->events);
926 while (nextNotify != NULL)
927 {
928 /*
929 * Add the pending notifications to the queue. We acquire and
930 * release NotifyQueueLock once per page, which might be overkill
931 * but it does allow readers to get in while we're doing this.
932 *
933 * A full queue is very uncommon and should really not happen,
934 * given that we have so much space available in the SLRU pages.
935 * Nevertheless we need to deal with this possibility. Note that
936 * when we get here we are in the process of committing our
937 * transaction, but we have not yet committed to clog, so at this
938 * point in time we can still roll the transaction back.
939 */
940 LWLockAcquire(NotifyQueueLock, LW_EXCLUSIVE);
942 if (asyncQueueIsFull())
944 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
945 errmsg("too many notifications in the NOTIFY queue")));
946 nextNotify = asyncQueueAddEntries(nextNotify);
947 LWLockRelease(NotifyQueueLock);
948 }
949
950 /* Note that we don't clear pendingNotifies; AtCommit_Notify will. */
951 }
952}
static void Exec_ListenPreCommit(void)
Definition: async.c:1040
static ListCell * asyncQueueAddEntries(ListCell *nextNotify)
Definition: async.c:1355
static void asyncQueueFillWarning(void)
Definition: async.c:1527
static bool asyncQueueIsFull(void)
Definition: async.c:1271
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1088
#define AccessExclusiveLock
Definition: lockdefs.h:43
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:455

References AccessExclusiveLock, ListenAction::action, ActionList::actions, asyncQueueAddEntries(), asyncQueueFillWarning(), asyncQueueIsFull(), 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(), pendingActions, pendingNotifies, and Trace_notify.

Referenced by CommitTransaction().

◆ ProcessNotifyInterrupt()

void ProcessNotifyInterrupt ( bool  flush)

Definition at line 1834 of file async.c.

1835{
1837 return; /* not really idle */
1838
1839 /* Loop in case another signal arrives while sending messages */
1841 ProcessIncomingNotify(flush);
1842}
static void ProcessIncomingNotify(bool flush)
Definition: async.c:2303
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:5007

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

Referenced by PostgresMain(), and ProcessClientReadInterrupt().

Variable Documentation

◆ max_notify_queue_pages

PGDLLIMPORT int max_notify_queue_pages
extern

Definition at line 428 of file async.c.

Referenced by asyncQueueIsFull(), and asyncQueueUsage().

◆ notifyInterruptPending

PGDLLIMPORT volatile sig_atomic_t notifyInterruptPending
extern

◆ Trace_notify