PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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

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)
extern

Definition at line 1043 of file async.c.

1044{
1045 if (Trace_notify)
1046 elog(DEBUG1, "Async_Listen(%s,%d)", channel, MyProcPid);
1047
1048 queue_listen(LISTEN_LISTEN, channel);
1049}
@ LISTEN_LISTEN
Definition async.c:440
bool Trace_notify
Definition async.c:581
static void queue_listen(ListenActionKind action, const char *channel)
Definition async.c:996
#define DEBUG1
Definition elog.h:30
#define elog(elevel,...)
Definition elog.h:227
int MyProcPid
Definition globals.c:49

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 
)
extern

Definition at line 894 of file async.c.

895{
896 int my_level = GetCurrentTransactionNestLevel();
897 size_t channel_len;
898 size_t payload_len;
899 Notification *n;
900 MemoryContext oldcontext;
901
902 if (IsParallelWorker())
903 elog(ERROR, "cannot send notifications from a parallel worker");
904
905 if (Trace_notify)
906 elog(DEBUG1, "Async_Notify(%s)", channel);
907
908 channel_len = channel ? strlen(channel) : 0;
909 payload_len = payload ? strlen(payload) : 0;
910
911 /* a channel name must be specified */
912 if (channel_len == 0)
915 errmsg("channel name cannot be empty")));
916
917 /* enforce length limits */
918 if (channel_len >= NAMEDATALEN)
921 errmsg("channel name too long")));
922
923 if (payload_len >= NOTIFY_PAYLOAD_MAX_LENGTH)
926 errmsg("payload string too long")));
927
928 /*
929 * We must construct the Notification entry, even if we end up not using
930 * it, in order to compare it cheaply to existing list entries.
931 *
932 * The notification list needs to live until end of transaction, so store
933 * it in the transaction context.
934 */
936
938 channel_len + payload_len + 2);
939 n->channel_len = channel_len;
940 n->payload_len = payload_len;
941 strcpy(n->data, channel);
942 if (payload)
943 strcpy(n->data + channel_len + 1, payload);
944 else
945 n->data[channel_len + 1] = '\0';
946
947 if (pendingNotifies == NULL || my_level > pendingNotifies->nestingLevel)
948 {
950
951 /*
952 * First notify event in current (sub)xact. Note that we allocate the
953 * NotificationList in TopTransactionContext; the nestingLevel might
954 * get changed later by AtSubCommit_Notify.
955 */
958 sizeof(NotificationList));
959 notifies->nestingLevel = my_level;
960 notifies->events = list_make1(n);
961 /* We certainly don't need a hashtable yet */
962 notifies->hashtab = NULL;
963 /* We won't build uniqueChannelNames/Hash till later, either */
964 notifies->uniqueChannelNames = NIL;
965 notifies->uniqueChannelHash = NULL;
966 notifies->upper = pendingNotifies;
968 }
969 else
970 {
971 /* Now check for duplicates */
973 {
974 /* It's a dup, so forget it */
975 pfree(n);
976 MemoryContextSwitchTo(oldcontext);
977 return;
978 }
979
980 /* Append more events to existing list */
982 }
983
984 MemoryContextSwitchTo(oldcontext);
985}
static bool AsyncExistsPendingNotify(Notification *n)
Definition async.c:3121
static NotificationList * pendingNotifies
Definition async.c:534
static void AddEventToPendingNotifies(Notification *n)
Definition async.c:3162
#define NOTIFY_PAYLOAD_MAX_LENGTH
Definition async.c:201
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:151
#define IsParallelWorker()
Definition parallel.h:62
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
MemoryContext TopTransactionContext
Definition mcxt.c:171
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc(Size size)
Definition mcxt.c:1387
MemoryContext CurTransactionContext
Definition mcxt.c:172
static char * errmsg
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
#define NAMEDATALEN
const void * data
#define NIL
Definition pg_list.h:68
#define list_make1(x1)
Definition pg_list.h:244
static int fb(int x)
uint16 payload_len
Definition async.c:512
char data[FLEXIBLE_ARRAY_MEMBER]
Definition async.c:514
uint16 channel_len
Definition async.c:511
int GetCurrentTransactionNestLevel(void)
Definition xact.c:931

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

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

◆ Async_Unlisten()

void Async_Unlisten ( const char channel)
extern

Definition at line 1057 of file async.c.

1058{
1059 if (Trace_notify)
1060 elog(DEBUG1, "Async_Unlisten(%s,%d)", channel, MyProcPid);
1061
1062 /* If we couldn't possibly be listening, no need to queue anything */
1064 return;
1065
1066 queue_listen(LISTEN_UNLISTEN, channel);
1067}
static ActionList * pendingActions
Definition async.c:458
@ LISTEN_UNLISTEN
Definition async.c:441
static bool unlistenExitRegistered
Definition async.c:555

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

Referenced by standard_ProcessUtility().

◆ Async_UnlistenAll()

void Async_UnlistenAll ( void  )
extern

Definition at line 1075 of file async.c.

1076{
1077 if (Trace_notify)
1078 elog(DEBUG1, "Async_UnlistenAll(%d)", MyProcPid);
1079
1080 /* If we couldn't possibly be listening, no need to queue anything */
1082 return;
1083
1085}
@ LISTEN_UNLISTEN_ALL
Definition async.c:442

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

Referenced by DiscardAll(), and standard_ProcessUtility().

◆ AsyncNotifyFreezeXids()

void AsyncNotifyFreezeXids ( TransactionId  newFrozenXid)
extern

Definition at line 2950 of file async.c.

2951{
2952 QueuePosition pos;
2953 QueuePosition head;
2954 int64 curpage = -1;
2955 int slotno = -1;
2956 char *page_buffer = NULL;
2957 bool page_dirty = false;
2958
2959 /*
2960 * Acquire locks in the correct order to avoid deadlocks. As per the
2961 * locking protocol: NotifyQueueTailLock, then NotifyQueueLock, then SLRU
2962 * bank locks.
2963 *
2964 * We only need SHARED mode since we're just reading the head/tail
2965 * positions, not modifying them.
2966 */
2969
2970 pos = QUEUE_TAIL;
2971 head = QUEUE_HEAD;
2972
2973 /* Release NotifyQueueLock early, we only needed to read the positions */
2975
2976 /*
2977 * Scan the queue from tail to head, freezing XIDs as needed. We hold
2978 * NotifyQueueTailLock throughout to ensure the tail doesn't move while
2979 * we're working.
2980 */
2981 while (!QUEUE_POS_EQUAL(pos, head))
2982 {
2984 TransactionId xid;
2985 int64 pageno = QUEUE_POS_PAGE(pos);
2986 int offset = QUEUE_POS_OFFSET(pos);
2987
2988 /* If we need a different page, release old lock and get new one */
2989 if (pageno != curpage)
2990 {
2991 LWLock *lock;
2992
2993 /* Release previous page if any */
2994 if (slotno >= 0)
2995 {
2996 if (page_dirty)
2997 {
2998 NotifyCtl->shared->page_dirty[slotno] = true;
2999 page_dirty = false;
3000 }
3002 }
3003
3004 lock = SimpleLruGetBankLock(NotifyCtl, pageno);
3006 slotno = SimpleLruReadPage(NotifyCtl, pageno, true, &pos);
3007 page_buffer = NotifyCtl->shared->page_buffer[slotno];
3008 curpage = pageno;
3009 }
3010
3011 qe = (AsyncQueueEntry *) (page_buffer + offset);
3012 xid = qe->xid;
3013
3014 if (TransactionIdIsNormal(xid) &&
3016 {
3017 if (TransactionIdDidCommit(xid))
3018 {
3019 qe->xid = FrozenTransactionId;
3020 page_dirty = true;
3021 }
3022 else
3023 {
3024 qe->xid = InvalidTransactionId;
3025 page_dirty = true;
3026 }
3027 }
3028
3029 /* Advance to next entry */
3030 asyncQueueAdvance(&pos, qe->length);
3031 }
3032
3033 /* Release final page lock if we acquired one */
3034 if (slotno >= 0)
3035 {
3036 if (page_dirty)
3037 NotifyCtl->shared->page_dirty[slotno] = true;
3039 }
3040
3042}
#define QUEUE_POS_OFFSET(x)
Definition async.c:239
static bool asyncQueueAdvance(volatile QueuePosition *position, int entryLength)
Definition async.c:1974
#define QUEUE_TAIL
Definition async.c:359
#define QUEUE_POS_PAGE(x)
Definition async.c:238
#define NotifyCtl
Definition async.c:378
#define QUEUE_HEAD
Definition async.c:358
#define QUEUE_POS_EQUAL(x, y)
Definition async.c:247
int64_t int64
Definition c.h:621
uint32 TransactionId
Definition c.h:736
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_SHARED
Definition lwlock.h:105
@ LW_EXCLUSIVE
Definition lwlock.h:104
int SimpleLruReadPage(SlruDesc *ctl, int64 pageno, bool write_ok, const void *opaque_data)
Definition slru.c:550
static LWLock * SimpleLruGetBankLock(SlruDesc *ctl, int64 pageno)
Definition slru.h:207
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(), fb(), FrozenTransactionId, InvalidTransactionId, LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), NotifyCtl, QUEUE_HEAD, QUEUE_POS_EQUAL, QUEUE_POS_OFFSET, QUEUE_POS_PAGE, QUEUE_TAIL, SimpleLruGetBankLock(), SimpleLruReadPage(), TransactionIdDidCommit(), TransactionIdIsNormal, and TransactionIdPrecedes().

Referenced by vac_truncate_clog().

◆ AtAbort_Notify()

void AtAbort_Notify ( void  )
extern

Definition at line 2418 of file async.c.

2419{
2420 /* Revert staged listen/unlisten changes */
2422
2423 /* If we're no longer listening on anything, unregister */
2426
2427 /* And clean up */
2429}
static void ApplyPendingListenActions(bool isCommit)
Definition async.c:1721
static void ClearPendingActionsAndNotifies(void)
Definition async.c:3276
static bool amRegisteredListener
Definition async.c:558
static void asyncQueueUnregister(void)
Definition async.c:1916
#define LocalChannelTableIsEmpty()
Definition async.c:425

References amRegisteredListener, ApplyPendingListenActions(), asyncQueueUnregister(), ClearPendingActionsAndNotifies(), and LocalChannelTableIsEmpty.

Referenced by AbortTransaction().

◆ AtCommit_Notify()

void AtCommit_Notify ( void  )
extern

Definition at line 1378 of file async.c.

1379{
1380 /*
1381 * Allow transactions that have not executed LISTEN/UNLISTEN/NOTIFY to
1382 * return as soon as possible
1383 */
1385 return;
1386
1387 if (Trace_notify)
1388 elog(DEBUG1, "AtCommit_Notify");
1389
1390 /* Apply staged listen/unlisten changes */
1392
1393 /* If no longer listening to anything, get out of listener array */
1396
1397 /*
1398 * Send signals to listening backends. We need do this only if there are
1399 * pending notifies, which were previously added to the shared queue by
1400 * PreCommit_Notify().
1401 */
1402 if (pendingNotifies != NULL)
1404
1405 /*
1406 * If it's time to try to advance the global tail pointer, do that.
1407 *
1408 * (It might seem odd to do this in the sender, when more than likely the
1409 * listeners won't yet have read the messages we just sent. However,
1410 * there's less contention if only the sender does it, and there is little
1411 * need for urgency in advancing the global tail. So this typically will
1412 * be clearing out messages that were sent some time ago.)
1413 */
1414 if (tryAdvanceTail)
1415 {
1416 tryAdvanceTail = false;
1418 }
1419
1420 /* And clean up */
1422}
static void SignalBackends(void)
Definition async.c:2266
static bool tryAdvanceTail
Definition async.c:578
static void asyncQueueAdvanceTail(void)
Definition async.c:2868

References amRegisteredListener, ApplyPendingListenActions(), asyncQueueAdvanceTail(), asyncQueueUnregister(), ClearPendingActionsAndNotifies(), DEBUG1, elog, fb(), LocalChannelTableIsEmpty, pendingActions, pendingNotifies, SignalBackends(), Trace_notify, and tryAdvanceTail.

Referenced by CommitTransaction().

◆ AtPrepare_Notify()

void AtPrepare_Notify ( void  )
extern

Definition at line 1160 of file async.c.

1161{
1162 /* It's not allowed to have any pending LISTEN/UNLISTEN/NOTIFY actions */
1164 ereport(ERROR,
1166 errmsg("cannot PREPARE a transaction that has executed LISTEN, UNLISTEN, or NOTIFY")));
1167}

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

Referenced by PrepareTransaction().

◆ AtSubAbort_Notify()

void AtSubAbort_Notify ( void  )
extern

Definition at line 2507 of file async.c.

2508{
2509 int my_level = GetCurrentTransactionNestLevel();
2510
2511 /*
2512 * All we have to do is pop the stack --- the actions/notifies made in
2513 * this subxact are no longer interesting, and the space will be freed
2514 * when CurTransactionContext is recycled. We still have to free the
2515 * ActionList and NotificationList objects themselves, though, because
2516 * those are allocated in TopTransactionContext.
2517 *
2518 * Note that there might be no entries at all, or no entries for the
2519 * current subtransaction level, either because none were ever created, or
2520 * because we reentered this routine due to trouble during subxact abort.
2521 */
2522 while (pendingActions != NULL &&
2523 pendingActions->nestingLevel >= my_level)
2524 {
2526
2529 }
2530
2531 while (pendingNotifies != NULL &&
2532 pendingNotifies->nestingLevel >= my_level)
2533 {
2535
2538 }
2539}
int nestingLevel
Definition async.c:453
struct ActionList * upper
Definition async.c:455
struct NotificationList * upper
Definition async.c:524

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

Referenced by AbortSubTransaction().

◆ AtSubCommit_Notify()

void AtSubCommit_Notify ( void  )
extern

Definition at line 2437 of file async.c.

2438{
2439 int my_level = GetCurrentTransactionNestLevel();
2440
2441 /* If there are actions at our nesting level, we must reparent them. */
2442 if (pendingActions != NULL &&
2443 pendingActions->nestingLevel >= my_level)
2444 {
2445 if (pendingActions->upper == NULL ||
2446 pendingActions->upper->nestingLevel < my_level - 1)
2447 {
2448 /* nothing to merge; give the whole thing to the parent */
2450 }
2451 else
2452 {
2454
2456
2457 /*
2458 * Mustn't try to eliminate duplicates here --- see queue_listen()
2459 */
2462 childPendingActions->actions);
2464 }
2465 }
2466
2467 /* If there are notifies at our nesting level, we must reparent them. */
2468 if (pendingNotifies != NULL &&
2469 pendingNotifies->nestingLevel >= my_level)
2470 {
2471 Assert(pendingNotifies->nestingLevel == my_level);
2472
2473 if (pendingNotifies->upper == NULL ||
2474 pendingNotifies->upper->nestingLevel < my_level - 1)
2475 {
2476 /* nothing to merge; give the whole thing to the parent */
2478 }
2479 else
2480 {
2481 /*
2482 * Formerly, we didn't bother to eliminate duplicates here, but
2483 * now we must, else we fall foul of "Assert(!found)", either here
2484 * or during a later attempt to build the parent-level hashtable.
2485 */
2487 ListCell *l;
2488
2490 /* Insert all the subxact's events into parent, except for dups */
2491 foreach(l, childPendingNotifies->events)
2492 {
2494
2497 }
2499 }
2500 }
2501}
#define Assert(condition)
Definition c.h:943
List * list_concat(List *list1, const List *list2)
Definition list.c:561
#define lfirst(lc)
Definition pg_list.h:172
List * actions
Definition async.c:454

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

Referenced by CommitSubTransaction().

◆ HandleNotifyInterrupt()

void HandleNotifyInterrupt ( void  )
extern

Definition at line 2550 of file async.c.

2551{
2552 /*
2553 * Note: this is called by a SIGNAL HANDLER. You must be very wary what
2554 * you do here.
2555 */
2556
2557 /* signal that work needs to be done */
2559
2560 /* latch will be set by procsignal_sigusr1_handler */
2561}
volatile sig_atomic_t notifyInterruptPending
Definition async.c:552

References notifyInterruptPending.

Referenced by procsignal_sigusr1_handler().

◆ NotifyMyFrontEnd()

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

Definition at line 3097 of file async.c.

3098{
3100 {
3102
3104 pq_sendint32(&buf, srcPid);
3105 pq_sendstring(&buf, channel);
3106 pq_sendstring(&buf, payload);
3108
3109 /*
3110 * NOTE: we do not do pq_flush() here. Some level of caller will
3111 * handle it later, allowing this message to be combined into a packet
3112 * with other ones.
3113 */
3114 }
3115 else
3116 elog(INFO, "NOTIFY for \"%s\" payload \"%s\"", channel, payload);
3117}
@ DestRemote
Definition dest.h:89
#define INFO
Definition elog.h:34
static char buf[DEFAULT_XLOG_SEG_SIZE]
CommandDest whereToSendOutput
Definition postgres.c:97
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  )
extern

Definition at line 1185 of file async.c.

1186{
1187 ListCell *p;
1188
1190 return; /* no relevant statements in this xact */
1191
1192 if (Trace_notify)
1193 elog(DEBUG1, "PreCommit_Notify");
1194
1195 /* Preflight for any pending listen/unlisten actions */
1197
1198 if (pendingActions != NULL)
1199 {
1200 /* Ensure we have a local channel table */
1202 /* Create pendingListenActions hash table for this transaction */
1204
1205 /* Stage all the actions this transaction wants to perform */
1206 foreach(p, pendingActions->actions)
1207 {
1209
1210 switch (actrec->action)
1211 {
1212 case LISTEN_LISTEN:
1215 break;
1216 case LISTEN_UNLISTEN:
1218 break;
1221 break;
1222 }
1223 }
1224 }
1225
1226 /* Queue any pending notifies (must happen after the above) */
1227 if (pendingNotifies)
1228 {
1230 bool firstIteration = true;
1231
1232 /*
1233 * Build list of unique channel names being notified for use by
1234 * SignalBackends().
1235 *
1236 * If uniqueChannelHash is available, use it to efficiently get the
1237 * unique channels. Otherwise, fall back to the O(N^2) approach.
1238 */
1241 {
1242 HASH_SEQ_STATUS status;
1244
1246 while ((channelEntry = (ChannelName *) hash_seq_search(&status)) != NULL)
1249 channelEntry->channel);
1250 }
1251 else
1252 {
1253 /* O(N^2) approach is better for small number of notifications */
1255 {
1256 char *channel = n->data;
1257 bool found = false;
1258
1259 /* Name present in list? */
1261 {
1262 if (strcmp(oldchan, channel) == 0)
1263 {
1264 found = true;
1265 break;
1266 }
1267 }
1268 /* Add if not already in list */
1269 if (!found)
1272 channel);
1273 }
1274 }
1275
1276 /* Preallocate workspace that will be needed by SignalBackends() */
1277 if (signalPids == NULL)
1279 MaxBackends * sizeof(int32));
1280
1281 if (signalProcnos == NULL)
1283 MaxBackends * sizeof(ProcNumber));
1284
1285 /*
1286 * Make sure that we have an XID assigned to the current transaction.
1287 * GetCurrentTransactionId is cheap if we already have an XID, but not
1288 * so cheap if we don't, and we'd prefer not to do that work while
1289 * holding NotifyQueueLock.
1290 */
1292
1293 /*
1294 * Serialize writers by acquiring a special lock that we hold till
1295 * after commit. This ensures that queue entries appear in commit
1296 * order, and in particular that there are never uncommitted queue
1297 * entries ahead of committed ones, so an uncommitted transaction
1298 * can't block delivery of deliverable notifications.
1299 *
1300 * We use a heavyweight lock so that it'll automatically be released
1301 * after either commit or abort. This also allows deadlocks to be
1302 * detected, though really a deadlock shouldn't be possible here.
1303 *
1304 * The lock is on "database 0", which is pretty ugly but it doesn't
1305 * seem worth inventing a special locktag category just for this.
1306 * (Historical note: before PG 9.0, a similar lock on "database 0" was
1307 * used by the flatfiles mechanism.)
1308 */
1311
1312 /*
1313 * For the direct advancement optimization in SignalBackends(), we
1314 * need to ensure that no other backend can insert queue entries
1315 * between queueHeadBeforeWrite and queueHeadAfterWrite. The
1316 * heavyweight lock above provides this guarantee, since it serializes
1317 * all writers.
1318 *
1319 * Note: if the heavyweight lock were ever removed for scalability
1320 * reasons, we could achieve the same guarantee by holding
1321 * NotifyQueueLock in EXCLUSIVE mode across all our insertions, rather
1322 * than releasing and reacquiring it for each page as we do below.
1323 */
1324
1325 /* Initialize values to a safe default in case list is empty */
1328
1329 /* Now push the notifications into the queue */
1331 while (nextNotify != NULL)
1332 {
1333 /*
1334 * Add the pending notifications to the queue. We acquire and
1335 * release NotifyQueueLock once per page, which might be overkill
1336 * but it does allow readers to get in while we're doing this.
1337 *
1338 * A full queue is very uncommon and should really not happen,
1339 * given that we have so much space available in the SLRU pages.
1340 * Nevertheless we need to deal with this possibility. Note that
1341 * when we get here we are in the process of committing our
1342 * transaction, but we have not yet committed to clog, so at this
1343 * point in time we can still roll the transaction back.
1344 */
1346 if (firstIteration)
1347 {
1349 firstIteration = false;
1350 }
1352 if (asyncQueueIsFull())
1353 ereport(ERROR,
1355 errmsg("too many notifications in the NOTIFY queue")));
1359 }
1360
1361 /* Note that we don't clear pendingNotifies; AtCommit_Notify will. */
1362 }
1363}
static void PrepareTableEntriesForListen(const char *channel)
Definition async.c:1531
static void BecomeRegisteredListener(void)
Definition async.c:1430
static int32 * signalPids
Definition async.c:574
static void PrepareTableEntriesForUnlisten(const char *channel)
Definition async.c:1634
#define SET_QUEUE_POS(x, y, z)
Definition async.c:241
static ProcNumber * signalProcnos
Definition async.c:575
static QueuePosition queueHeadAfterWrite
Definition async.c:567
static ListCell * asyncQueueAddEntries(ListCell *nextNotify)
Definition async.c:2043
static void PrepareTableEntriesForUnlistenAll(void)
Definition async.c:1664
static void asyncQueueFillWarning(void)
Definition async.c:2214
static void initGlobalChannelTable(void)
Definition async.c:699
static bool asyncQueueIsFull(void)
Definition async.c:1959
static void initLocalChannelTable(void)
Definition async.c:750
static void initPendingListenActions(void)
Definition async.c:776
static QueuePosition queueHeadBeforeWrite
Definition async.c:566
int32_t int32
Definition c.h:620
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1352
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1317
int MaxBackends
Definition globals.c:149
List * lappend(List *list, void *datum)
Definition list.c:339
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition lmgr.c:1088
#define AccessExclusiveLock
Definition lockdefs.h:43
MemoryContext TopMemoryContext
Definition mcxt.c:166
#define foreach_ptr(type, var, lst)
Definition pg_list.h:501
static ListCell * list_head(const List *l)
Definition pg_list.h:128
#define InvalidOid
int ProcNumber
Definition procnumber.h:24
List * uniqueChannelNames
Definition async.c:522
HTAB * uniqueChannelHash
Definition async.c:523
List * events
Definition async.c:520
TransactionId GetCurrentTransactionId(void)
Definition xact.c:456

References AccessExclusiveLock, ActionList::actions, asyncQueueAddEntries(), asyncQueueFillWarning(), asyncQueueIsFull(), BecomeRegisteredListener(), DEBUG1, elog, ereport, errcode(), errmsg, ERROR, NotificationList::events, fb(), foreach_ptr, GetCurrentTransactionId(), hash_seq_init(), hash_seq_search(), initGlobalChannelTable(), initLocalChannelTable(), initPendingListenActions(), InvalidOid, lappend(), lfirst, list_head(), LISTEN_LISTEN, LISTEN_UNLISTEN, LISTEN_UNLISTEN_ALL, LockSharedObject(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MaxBackends, MemoryContextAlloc(), NIL, pendingActions, pendingNotifies, PrepareTableEntriesForListen(), PrepareTableEntriesForUnlisten(), PrepareTableEntriesForUnlistenAll(), QUEUE_HEAD, queueHeadAfterWrite, queueHeadBeforeWrite, SET_QUEUE_POS, signalPids, signalProcnos, TopMemoryContext, Trace_notify, NotificationList::uniqueChannelHash, and NotificationList::uniqueChannelNames.

Referenced by CommitTransaction().

◆ ProcessNotifyInterrupt()

void ProcessNotifyInterrupt ( bool  flush)
extern

Definition at line 2579 of file async.c.

2580{
2582 return; /* not really idle */
2583
2584 /* Loop in case another signal arrives while sending messages */
2586 ProcessIncomingNotify(flush);
2587}
static void ProcessIncomingNotify(bool flush)
Definition async.c:3056
bool IsTransactionOrTransactionBlock(void)
Definition xact.c:5040

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 584 of file async.c.

Referenced by asyncQueueIsFull(), and asyncQueueUsage().

◆ notifyInterruptPending

◆ Trace_notify