Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Typedefs | Enumerations | Functions | Variables

async.c File Reference

#include "postgres.h"
#include <limits.h>
#include <unistd.h>
#include <signal.h>
#include "access/slru.h"
#include "access/transam.h"
#include "access/xact.h"
#include "catalog/pg_database.h"
#include "commands/async.h"
#include "funcapi.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/procsignal.h"
#include "storage/sinval.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
#include "utils/timestamp.h"
Include dependency graph for async.c:

Go to the source code of this file.

Data Structures

struct  AsyncQueueEntry
struct  QueuePosition
struct  QueueBackendStatus
struct  AsyncQueueControl
struct  ListenAction
struct  Notification

Defines

#define NOTIFY_PAYLOAD_MAX_LENGTH   (BLCKSZ - NAMEDATALEN - 128)
#define QUEUEALIGN(len)   INTALIGN(len)
#define AsyncQueueEntryEmptySize   (offsetof(AsyncQueueEntry, data) + 2)
#define QUEUE_POS_PAGE(x)   ((x).page)
#define QUEUE_POS_OFFSET(x)   ((x).offset)
#define SET_QUEUE_POS(x, y, z)
#define QUEUE_POS_EQUAL(x, y)   ((x).page == (y).page && (x).offset == (y).offset)
#define QUEUE_POS_MIN(x, y)
#define InvalidPid   (-1)
#define QUEUE_HEAD   (asyncQueueControl->head)
#define QUEUE_TAIL   (asyncQueueControl->tail)
#define QUEUE_BACKEND_PID(i)   (asyncQueueControl->backend[i].pid)
#define QUEUE_BACKEND_POS(i)   (asyncQueueControl->backend[i].pos)
#define AsyncCtl   (&AsyncCtlData)
#define QUEUE_PAGESIZE   BLCKSZ
#define QUEUE_FULL_WARN_INTERVAL   5000
#define QUEUE_MAX_PAGE   (SLRU_PAGES_PER_SEGMENT * 0x10000 - 1)

Typedefs

typedef struct AsyncQueueEntry AsyncQueueEntry
typedef struct QueuePosition QueuePosition
typedef struct QueueBackendStatus QueueBackendStatus
typedef struct AsyncQueueControl AsyncQueueControl
typedef struct Notification Notification

Enumerations

enum  ListenActionKind { LISTEN_LISTEN, LISTEN_UNLISTEN, LISTEN_UNLISTEN_ALL }

Functions

static bool asyncQueuePagePrecedes (int p, int q)
static void queue_listen (ListenActionKind action, const char *channel)
static void Async_UnlistenOnExit (int code, Datum arg)
static void Exec_ListenPreCommit (void)
static void Exec_ListenCommit (const char *channel)
static void Exec_UnlistenCommit (const char *channel)
static void Exec_UnlistenAllCommit (void)
static bool IsListeningOn (const char *channel)
static void asyncQueueUnregister (void)
static bool asyncQueueIsFull (void)
static bool asyncQueueAdvance (QueuePosition *position, int entryLength)
static void asyncQueueNotificationToEntry (Notification *n, AsyncQueueEntry *qe)
static ListCellasyncQueueAddEntries (ListCell *nextNotify)
static void asyncQueueFillWarning (void)
static bool SignalBackends (void)
static void asyncQueueReadAllNotifications (void)
static bool asyncQueueProcessPageEntries (QueuePosition *current, QueuePosition stop, char *page_buffer)
static void asyncQueueAdvanceTail (void)
static void ProcessIncomingNotify (void)
static void NotifyMyFrontEnd (const char *channel, const char *payload, int32 srcPid)
static bool AsyncExistsPendingNotify (const char *channel, const char *payload)
static void ClearPendingActionsAndNotifies (void)
Size AsyncShmemSize (void)
void AsyncShmemInit (void)
Datum pg_notify (PG_FUNCTION_ARGS)
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)
Datum pg_listening_channels (PG_FUNCTION_ARGS)
void AtPrepare_Notify (void)
void PreCommit_Notify (void)
void AtCommit_Notify (void)
void ProcessCompletedNotifies (void)
void AtAbort_Notify (void)
void AtSubStart_Notify (void)
void AtSubCommit_Notify (void)
void AtSubAbort_Notify (void)
void HandleNotifyInterrupt (void)
void EnableNotifyInterrupt (void)
bool DisableNotifyInterrupt (void)

Variables

static AsyncQueueControlasyncQueueControl
static SlruCtlData AsyncCtlData
static ListlistenChannels = NIL
static ListpendingActions = NIL
static ListupperPendingActions = NIL
static ListpendingNotifies = NIL
static ListupperPendingNotifies = NIL
static volatile sig_atomic_t notifyInterruptEnabled = 0
static volatile sig_atomic_t notifyInterruptOccurred = 0
static bool unlistenExitRegistered = false
static bool amRegisteredListener = false
static bool backendHasSentNotifications = false
bool Trace_notify = false

Define Documentation

#define AsyncCtl   (&AsyncCtlData)
#define AsyncQueueEntryEmptySize   (offsetof(AsyncQueueEntry, data) + 2)

Definition at line 172 of file async.c.

Referenced by asyncQueueAdvance(), and asyncQueueNotificationToEntry().

#define InvalidPid   (-1)

Definition at line 210 of file async.c.

Referenced by asyncQueueAdvanceTail(), asyncQueueFillWarning(), and SignalBackends().

#define NOTIFY_PAYLOAD_MAX_LENGTH   (BLCKSZ - NAMEDATALEN - 128)

Definition at line 146 of file async.c.

Referenced by Async_Notify(), and asyncQueueNotificationToEntry().

#define QUEUE_BACKEND_PID (   i  )     (asyncQueueControl->backend[i].pid)
#define QUEUE_BACKEND_POS (   i  )     (asyncQueueControl->backend[i].pos)
#define QUEUE_FULL_WARN_INTERVAL   5000

Definition at line 256 of file async.c.

Referenced by asyncQueueFillWarning().

#define QUEUE_HEAD   (asyncQueueControl->head)
#define QUEUE_MAX_PAGE   (SLRU_PAGES_PER_SEGMENT * 0x10000 - 1)
#define QUEUE_PAGESIZE   BLCKSZ
#define QUEUE_POS_EQUAL (   x,
  y 
)    ((x).page == (y).page && (x).offset == (y).offset)
#define QUEUE_POS_MIN (   x,
  y 
)
Value:
(asyncQueuePagePrecedes((x).page, (y).page) ? (x) : \
     (x).page != (y).page ? (y) : \
     (x).offset < (y).offset ? (x) : (y))

Definition at line 196 of file async.c.

Referenced by asyncQueueAdvanceTail(), and asyncQueueFillWarning().

#define QUEUE_POS_OFFSET (   x  )     ((x).offset)
#define QUEUE_POS_PAGE (   x  )     ((x).page)
#define QUEUE_TAIL   (asyncQueueControl->tail)
#define QUEUEALIGN (   len  )     INTALIGN(len)

Definition at line 170 of file async.c.

Referenced by asyncQueueAdvance(), and asyncQueueNotificationToEntry().

#define SET_QUEUE_POS (   x,
  y,
  z 
)
Value:
do { \
        (x).page = (y); \
        (x).offset = (z); \
    } while (0)

Definition at line 186 of file async.c.

Referenced by asyncQueueAdvance(), and AsyncShmemInit().


Typedef Documentation

typedef struct Notification Notification
typedef struct QueuePosition QueuePosition

Enumeration Type Documentation

Enumerator:
LISTEN_LISTEN 
LISTEN_UNLISTEN 
LISTEN_UNLISTEN_ALL 

Definition at line 294 of file async.c.

{
    LISTEN_LISTEN,
    LISTEN_UNLISTEN,
    LISTEN_UNLISTEN_ALL
} ListenActionKind;


Function Documentation

void Async_Listen ( const char *  channel  ) 

Definition at line 625 of file async.c.

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

Referenced by standard_ProcessUtility().

{
    if (Trace_notify)
        elog(DEBUG1, "Async_Listen(%s,%d)", channel, MyProcPid);

    queue_listen(LISTEN_LISTEN, channel);
}

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

Definition at line 534 of file async.c.

References AsyncExistsPendingNotify(), Notification::channel, CurTransactionContext, DEBUG1, elog, ereport, errcode(), errmsg(), ERROR, lappend(), MemoryContextSwitchTo(), NAMEDATALEN, NOTIFY_PAYLOAD_MAX_LENGTH, palloc(), Notification::payload, pstrdup(), and Trace_notify.

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

{
    Notification *n;
    MemoryContext oldcontext;

    if (Trace_notify)
        elog(DEBUG1, "Async_Notify(%s)", channel);

    /* a channel name must be specified */
    if (!channel || !strlen(channel))
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("channel name cannot be empty")));

    if (strlen(channel) >= NAMEDATALEN)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("channel name too long")));

    if (payload)
    {
        if (strlen(payload) >= NOTIFY_PAYLOAD_MAX_LENGTH)
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("payload string too long")));
    }

    /* no point in making duplicate entries in the list ... */
    if (AsyncExistsPendingNotify(channel, payload))
        return;

    /*
     * The notification list needs to live until end of transaction, so store
     * it in the transaction context.
     */
    oldcontext = MemoryContextSwitchTo(CurTransactionContext);

    n = (Notification *) palloc(sizeof(Notification));
    n->channel = pstrdup(channel);
    if (payload)
        n->payload = pstrdup(payload);
    else
        n->payload = "";

    /*
     * We want to preserve the order so we need to append every notification.
     * See comments at AsyncExistsPendingNotify().
     */
    pendingNotifies = lappend(pendingNotifies, n);

    MemoryContextSwitchTo(oldcontext);
}

void Async_Unlisten ( const char *  channel  ) 

Definition at line 639 of file async.c.

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

Referenced by standard_ProcessUtility().

{
    if (Trace_notify)
        elog(DEBUG1, "Async_Unlisten(%s,%d)", channel, MyProcPid);

    /* If we couldn't possibly be listening, no need to queue anything */
    if (pendingActions == NIL && !unlistenExitRegistered)
        return;

    queue_listen(LISTEN_UNLISTEN, channel);
}

void Async_UnlistenAll ( void   ) 

Definition at line 657 of file async.c.

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

Referenced by DiscardAll(), and standard_ProcessUtility().

{
    if (Trace_notify)
        elog(DEBUG1, "Async_UnlistenAll(%d)", MyProcPid);

    /* If we couldn't possibly be listening, no need to queue anything */
    if (pendingActions == NIL && !unlistenExitRegistered)
        return;

    queue_listen(LISTEN_UNLISTEN_ALL, "");
}

static void Async_UnlistenOnExit ( int  code,
Datum  arg 
) [static]
static bool AsyncExistsPendingNotify ( const char *  channel,
const char *  payload 
) [static]

Definition at line 2150 of file async.c.

References Notification::channel, lfirst, llast, NIL, NULL, and Notification::payload.

Referenced by Async_Notify().

{
    ListCell   *p;
    Notification *n;

    if (pendingNotifies == NIL)
        return false;

    if (payload == NULL)
        payload = "";

    /*----------
     * We need to append new elements to the end of the list in order to keep
     * the order. However, on the other hand we'd like to check the list
     * backwards in order to make duplicate-elimination a tad faster when the
     * same condition is signaled many times in a row. So as a compromise we
     * check the tail element first which we can access directly. If this
     * doesn't match, we check the whole list.
     *
     * As we are not checking our parents' lists, we can still get duplicates
     * in combination with subtransactions, like in:
     *
     * begin;
     * notify foo '1';
     * savepoint foo;
     * notify foo '1';
     * commit;
     *----------
     */
    n = (Notification *) llast(pendingNotifies);
    if (strcmp(n->channel, channel) == 0 &&
        strcmp(n->payload, payload) == 0)
        return true;

    foreach(p, pendingNotifies)
    {
        n = (Notification *) lfirst(p);

        if (strcmp(n->channel, channel) == 0 &&
            strcmp(n->payload, payload) == 0)
            return true;
    }

    return false;
}

static ListCell * asyncQueueAddEntries ( ListCell nextNotify  )  [static]

Definition at line 1278 of file async.c.

References AsyncCtl, AsyncCtlLock, asyncQueueAdvance(), asyncQueueNotificationToEntry(), AsyncQueueEntry::data, AsyncQueueEntry::dboid, InvalidTransactionId, AsyncQueueEntry::length, lfirst, lnext, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), NULL, QUEUE_HEAD, QUEUE_PAGESIZE, QUEUE_POS_OFFSET, QUEUE_POS_PAGE, SimpleLruReadPage(), and SimpleLruZeroPage().

Referenced by PreCommit_Notify().

{
    AsyncQueueEntry qe;
    QueuePosition queue_head;
    int         pageno;
    int         offset;
    int         slotno;

    /* We hold both AsyncQueueLock and AsyncCtlLock during this operation */
    LWLockAcquire(AsyncCtlLock, LW_EXCLUSIVE);

    /*
     * We work with a local copy of QUEUE_HEAD, which we write back to shared
     * memory upon exiting.  The reason for this is that if we have to advance
     * to a new page, SimpleLruZeroPage might fail (out of disk space, for
     * instance), and we must not advance QUEUE_HEAD if it does.  (Otherwise,
     * subsequent insertions would try to put entries into a page that slru.c
     * thinks doesn't exist yet.)  So, use a local position variable.  Note
     * that if we do fail, any already-inserted queue entries are forgotten;
     * this is okay, since they'd be useless anyway after our transaction
     * rolls back.
     */
    queue_head = QUEUE_HEAD;

    /* Fetch the current page */
    pageno = QUEUE_POS_PAGE(queue_head);
    slotno = SimpleLruReadPage(AsyncCtl, pageno, true, InvalidTransactionId);
    /* Note we mark the page dirty before writing in it */
    AsyncCtl->shared->page_dirty[slotno] = true;

    while (nextNotify != NULL)
    {
        Notification *n = (Notification *) lfirst(nextNotify);

        /* Construct a valid queue entry in local variable qe */
        asyncQueueNotificationToEntry(n, &qe);

        offset = QUEUE_POS_OFFSET(queue_head);

        /* Check whether the entry really fits on the current page */
        if (offset + qe.length <= QUEUE_PAGESIZE)
        {
            /* OK, so advance nextNotify past this item */
            nextNotify = lnext(nextNotify);
        }
        else
        {
            /*
             * Write a dummy entry to fill up the page. Actually readers will
             * only check dboid and since it won't match any reader's database
             * OID, they will ignore this entry and move on.
             */
            qe.length = QUEUE_PAGESIZE - offset;
            qe.dboid = InvalidOid;
            qe.data[0] = '\0';  /* empty channel */
            qe.data[1] = '\0';  /* empty payload */
        }

        /* Now copy qe into the shared buffer page */
        memcpy(AsyncCtl->shared->page_buffer[slotno] + offset,
               &qe,
               qe.length);

        /* Advance queue_head appropriately, and detect if page is full */
        if (asyncQueueAdvance(&(queue_head), qe.length))
        {
            /*
             * Page is full, so we're done here, but first fill the next page
             * with zeroes.  The reason to do this is to ensure that slru.c's
             * idea of the head page is always the same as ours, which avoids
             * boundary problems in SimpleLruTruncate.  The test in
             * asyncQueueIsFull() ensured that there is room to create this
             * page without overrunning the queue.
             */
            slotno = SimpleLruZeroPage(AsyncCtl, QUEUE_POS_PAGE(queue_head));
            /* And exit the loop */
            break;
        }
    }

    /* Success, so update the global QUEUE_HEAD */
    QUEUE_HEAD = queue_head;

    LWLockRelease(AsyncCtlLock);

    return nextNotify;
}

static bool asyncQueueAdvance ( QueuePosition position,
int  entryLength 
) [static]

Definition at line 1206 of file async.c.

References Assert, AsyncQueueEntryEmptySize, QUEUE_MAX_PAGE, QUEUE_PAGESIZE, QUEUE_POS_OFFSET, QUEUE_POS_PAGE, QUEUEALIGN, and SET_QUEUE_POS.

Referenced by asyncQueueAddEntries(), and asyncQueueProcessPageEntries().

{
    int         pageno = QUEUE_POS_PAGE(*position);
    int         offset = QUEUE_POS_OFFSET(*position);
    bool        pageJump = false;

    /*
     * Move to the next writing position: First jump over what we have just
     * written or read.
     */
    offset += entryLength;
    Assert(offset <= QUEUE_PAGESIZE);

    /*
     * In a second step check if another entry can possibly be written to the
     * page. If so, stay here, we have reached the next position. If not, then
     * we need to move on to the next page.
     */
    if (offset + QUEUEALIGN(AsyncQueueEntryEmptySize) > QUEUE_PAGESIZE)
    {
        pageno++;
        if (pageno > QUEUE_MAX_PAGE)
            pageno = 0;         /* wrap around */
        offset = 0;
        pageJump = true;
    }

    SET_QUEUE_POS(*position, pageno, offset);
    return pageJump;
}

static void asyncQueueAdvanceTail ( void   )  [static]

Definition at line 2028 of file async.c.

References AsyncCtl, AsyncQueueLock, asyncQueuePagePrecedes(), i, InvalidPid, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MaxBackends, QUEUE_BACKEND_PID, QUEUE_BACKEND_POS, QUEUE_POS_MIN, QUEUE_POS_PAGE, QUEUE_TAIL, and SimpleLruTruncate().

Referenced by asyncQueueReadAllNotifications(), asyncQueueUnregister(), and ProcessCompletedNotifies().

{
    QueuePosition min;
    int         i;
    int         oldtailpage;
    int         newtailpage;
    int         boundary;

    LWLockAcquire(AsyncQueueLock, LW_EXCLUSIVE);
    min = QUEUE_HEAD;
    for (i = 1; i <= MaxBackends; i++)
    {
        if (QUEUE_BACKEND_PID(i) != InvalidPid)
            min = QUEUE_POS_MIN(min, QUEUE_BACKEND_POS(i));
    }
    oldtailpage = QUEUE_POS_PAGE(QUEUE_TAIL);
    QUEUE_TAIL = min;
    LWLockRelease(AsyncQueueLock);

    /*
     * We can truncate something if the global tail advanced across an SLRU
     * segment boundary.
     *
     * XXX it might be better to truncate only once every several segments, to
     * reduce the number of directory scans.
     */
    newtailpage = QUEUE_POS_PAGE(min);
    boundary = newtailpage - (newtailpage % SLRU_PAGES_PER_SEGMENT);
    if (asyncQueuePagePrecedes(oldtailpage, boundary))
    {
        /*
         * SimpleLruTruncate() will ask for AsyncCtlLock but will also release
         * the lock again.
         */
        SimpleLruTruncate(AsyncCtl, newtailpage);
    }
}

static void asyncQueueFillWarning ( void   )  [static]

Definition at line 1375 of file async.c.

References ereport, errdetail(), errhint(), errmsg(), GetCurrentTimestamp(), i, InvalidPid, AsyncQueueControl::lastQueueFillWarn, MaxBackends, QUEUE_BACKEND_PID, QUEUE_BACKEND_POS, QUEUE_FULL_WARN_INTERVAL, QUEUE_HEAD, QUEUE_MAX_PAGE, QUEUE_POS_EQUAL, QUEUE_POS_MIN, QUEUE_POS_PAGE, QUEUE_TAIL, TimestampDifferenceExceeds(), and WARNING.

Referenced by PreCommit_Notify().

{
    int         headPage = QUEUE_POS_PAGE(QUEUE_HEAD);
    int         tailPage = QUEUE_POS_PAGE(QUEUE_TAIL);
    int         occupied;
    double      fillDegree;
    TimestampTz t;

    occupied = headPage - tailPage;

    if (occupied == 0)
        return;                 /* fast exit for common case */

    if (occupied < 0)
    {
        /* head has wrapped around, tail not yet */
        occupied += QUEUE_MAX_PAGE + 1;
    }

    fillDegree = (double) occupied / (double) ((QUEUE_MAX_PAGE + 1) / 2);

    if (fillDegree < 0.5)
        return;

    t = GetCurrentTimestamp();

    if (TimestampDifferenceExceeds(asyncQueueControl->lastQueueFillWarn,
                                   t, QUEUE_FULL_WARN_INTERVAL))
    {
        QueuePosition min = QUEUE_HEAD;
        int32       minPid = InvalidPid;
        int         i;

        for (i = 1; i <= MaxBackends; i++)
        {
            if (QUEUE_BACKEND_PID(i) != InvalidPid)
            {
                min = QUEUE_POS_MIN(min, QUEUE_BACKEND_POS(i));
                if (QUEUE_POS_EQUAL(min, QUEUE_BACKEND_POS(i)))
                    minPid = QUEUE_BACKEND_PID(i);
            }
        }

        ereport(WARNING,
                (errmsg("NOTIFY queue is %.0f%% full", fillDegree * 100),
                 (minPid != InvalidPid ?
                  errdetail("The server process with PID %d is among those with the oldest transactions.", minPid)
                  : 0),
                 (minPid != InvalidPid ?
                  errhint("The NOTIFY queue cannot be emptied until that process ends its current transaction.")
                  : 0)));

        asyncQueueControl->lastQueueFillWarn = t;
    }
}

static bool asyncQueueIsFull ( void   )  [static]

Definition at line 1175 of file async.c.

References asyncQueuePagePrecedes(), QUEUE_HEAD, QUEUE_MAX_PAGE, QUEUE_POS_PAGE, and QUEUE_TAIL.

Referenced by PreCommit_Notify().

{
    int         nexthead;
    int         boundary;

    /*
     * The queue is full if creating a new head page would create a page that
     * logically precedes the current global tail pointer, ie, the head
     * pointer would wrap around compared to the tail.  We cannot create such
     * a head page for fear of confusing slru.c.  For safety we round the tail
     * pointer back to a segment boundary (compare the truncation logic in
     * asyncQueueAdvanceTail).
     *
     * Note that this test is *not* dependent on how much space there is on
     * the current head page.  This is necessary because asyncQueueAddEntries
     * might try to create the next head page in any case.
     */
    nexthead = QUEUE_POS_PAGE(QUEUE_HEAD) + 1;
    if (nexthead > QUEUE_MAX_PAGE)
        nexthead = 0;           /* wrap around */
    boundary = QUEUE_POS_PAGE(QUEUE_TAIL);
    boundary -= boundary % SLRU_PAGES_PER_SEGMENT;
    return asyncQueuePagePrecedes(nexthead, boundary);
}

static void asyncQueueNotificationToEntry ( Notification n,
AsyncQueueEntry qe 
) [static]

Definition at line 1241 of file async.c.

References Assert, AsyncQueueEntryEmptySize, Notification::channel, AsyncQueueEntry::data, AsyncQueueEntry::dboid, GetCurrentTransactionId(), AsyncQueueEntry::length, MyDatabaseId, MyProcPid, NAMEDATALEN, NOTIFY_PAYLOAD_MAX_LENGTH, Notification::payload, QUEUEALIGN, AsyncQueueEntry::srcPid, and AsyncQueueEntry::xid.

Referenced by asyncQueueAddEntries().

{
    size_t      channellen = strlen(n->channel);
    size_t      payloadlen = strlen(n->payload);
    int         entryLength;

    Assert(channellen < NAMEDATALEN);
    Assert(payloadlen < NOTIFY_PAYLOAD_MAX_LENGTH);

    /* The terminators are already included in AsyncQueueEntryEmptySize */
    entryLength = AsyncQueueEntryEmptySize + payloadlen + channellen;
    entryLength = QUEUEALIGN(entryLength);
    qe->length = entryLength;
    qe->dboid = MyDatabaseId;
    qe->xid = GetCurrentTransactionId();
    qe->srcPid = MyProcPid;
    memcpy(qe->data, n->channel, channellen + 1);
    memcpy(qe->data + channellen + 1, n->payload, payloadlen + 1);
}

static bool asyncQueuePagePrecedes ( int  p,
int  q 
) [static]

Definition at line 394 of file async.c.

References Assert, and QUEUE_MAX_PAGE.

Referenced by asyncQueueAdvanceTail(), and asyncQueueIsFull().

{
    int         diff;

    /*
     * We have to compare modulo (QUEUE_MAX_PAGE+1)/2.  Both inputs should be
     * in the range 0..QUEUE_MAX_PAGE.
     */
    Assert(p >= 0 && p <= QUEUE_MAX_PAGE);
    Assert(q >= 0 && q <= QUEUE_MAX_PAGE);

    diff = p - q;
    if (diff >= ((QUEUE_MAX_PAGE + 1) / 2))
        diff -= QUEUE_MAX_PAGE + 1;
    else if (diff < -((QUEUE_MAX_PAGE + 1) / 2))
        diff += QUEUE_MAX_PAGE + 1;
    return diff < 0;
}

static bool asyncQueueProcessPageEntries ( QueuePosition current,
QueuePosition  stop,
char *  page_buffer 
) [static]

Definition at line 1950 of file async.c.

References asyncQueueAdvance(), AsyncQueueEntry::data, AsyncQueueEntry::dboid, IsListeningOn(), AsyncQueueEntry::length, MyDatabaseId, NotifyMyFrontEnd(), QUEUE_POS_EQUAL, QUEUE_POS_OFFSET, AsyncQueueEntry::srcPid, TransactionIdDidAbort(), TransactionIdDidCommit(), and AsyncQueueEntry::xid.

Referenced by asyncQueueReadAllNotifications().

{
    bool        reachedStop = false;
    bool        reachedEndOfPage;
    AsyncQueueEntry *qe;

    do
    {
        QueuePosition thisentry = *current;

        if (QUEUE_POS_EQUAL(thisentry, stop))
            break;

        qe = (AsyncQueueEntry *) (page_buffer + QUEUE_POS_OFFSET(thisentry));

        /*
         * Advance *current over this message, possibly to the next page. As
         * noted in the comments for asyncQueueReadAllNotifications, we must
         * do this before possibly failing while processing the message.
         */
        reachedEndOfPage = asyncQueueAdvance(current, qe->length);

        /* Ignore messages destined for other databases */
        if (qe->dboid == MyDatabaseId)
        {
            if (TransactionIdDidCommit(qe->xid))
            {
                /* qe->data is the null-terminated channel name */
                char       *channel = qe->data;

                if (IsListeningOn(channel))
                {
                    /* payload follows channel name */
                    char       *payload = qe->data + strlen(channel) + 1;

                    NotifyMyFrontEnd(channel, payload, qe->srcPid);
                }
            }
            else if (TransactionIdDidAbort(qe->xid))
            {
                /*
                 * If the source transaction aborted, we just ignore its
                 * notifications.
                 */
            }
            else
            {
                /*
                 * The transaction has neither committed nor aborted so far,
                 * so we can't process its message yet.  Break out of the
                 * loop, but first back up *current so we will reprocess the
                 * message next time.  (Note: it is unlikely but not
                 * impossible for TransactionIdDidCommit to fail, so we can't
                 * really avoid this advance-then-back-up behavior when
                 * dealing with an uncommitted message.)
                 */
                *current = thisentry;
                reachedStop = true;
                break;
            }
        }

        /* Loop back if we're not at end of page */
    } while (!reachedEndOfPage);

    if (QUEUE_POS_EQUAL(*current, stop))
        reachedStop = true;

    return reachedStop;
}

static void asyncQueueReadAllNotifications ( void   )  [static]

Definition at line 1788 of file async.c.

References Assert, AsyncCtl, AsyncCtlLock, asyncQueueAdvanceTail(), AsyncQueueLock, asyncQueueProcessPageEntries(), buf, InvalidTransactionId, LW_SHARED, LWLockAcquire(), LWLockRelease(), MyBackendId, MyProcPid, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, QUEUE_BACKEND_PID, QUEUE_BACKEND_POS, QUEUE_PAGESIZE, QUEUE_POS_EQUAL, QUEUE_POS_OFFSET, QUEUE_POS_PAGE, QUEUE_TAIL, and SimpleLruReadPage_ReadOnly().

Referenced by Exec_ListenPreCommit(), ProcessCompletedNotifies(), and ProcessIncomingNotify().

{
    QueuePosition pos;
    QueuePosition oldpos;
    QueuePosition head;
    bool        advanceTail;

    /* page_buffer must be adequately aligned, so use a union */
    union
    {
        char        buf[QUEUE_PAGESIZE];
        AsyncQueueEntry align;
    }           page_buffer;

    /* Fetch current state */
    LWLockAcquire(AsyncQueueLock, LW_SHARED);
    /* Assert checks that we have a valid state entry */
    Assert(MyProcPid == QUEUE_BACKEND_PID(MyBackendId));
    pos = oldpos = QUEUE_BACKEND_POS(MyBackendId);
    head = QUEUE_HEAD;
    LWLockRelease(AsyncQueueLock);

    if (QUEUE_POS_EQUAL(pos, head))
    {
        /* Nothing to do, we have read all notifications already. */
        return;
    }

    /*----------
     * Note that we deliver everything that we see in the queue and that
     * matches our _current_ listening state.
     * Especially we do not take into account different commit times.
     * Consider the following example:
     *
     * Backend 1:                    Backend 2:
     *
     * transaction starts
     * NOTIFY foo;
     * commit starts
     *                               transaction starts
     *                               LISTEN foo;
     *                               commit starts
     * commit to clog
     *                               commit to clog
     *
     * It could happen that backend 2 sees the notification from backend 1 in
     * the queue.  Even though the notifying transaction committed before
     * the listening transaction, we still deliver the notification.
     *
     * The idea is that an additional notification does not do any harm, we
     * just need to make sure that we do not miss a notification.
     *
     * It is possible that we fail while trying to send a message to our
     * frontend (for example, because of encoding conversion failure).
     * If that happens it is critical that we not try to send the same
     * message over and over again.  Therefore, we place a PG_TRY block
     * here that will forcibly advance our backend position before we lose
     * control to an error.  (We could alternatively retake AsyncQueueLock
     * and move the position before handling each individual message, but
     * that seems like too much lock traffic.)
     *----------
     */
    PG_TRY();
    {
        bool        reachedStop;

        do
        {
            int         curpage = QUEUE_POS_PAGE(pos);
            int         curoffset = QUEUE_POS_OFFSET(pos);
            int         slotno;
            int         copysize;

            /*
             * We copy the data from SLRU into a local buffer, so as to avoid
             * holding the AsyncCtlLock while we are examining the entries and
             * possibly transmitting them to our frontend.  Copy only the part
             * of the page we will actually inspect.
             */
            slotno = SimpleLruReadPage_ReadOnly(AsyncCtl, curpage,
                                                InvalidTransactionId);
            if (curpage == QUEUE_POS_PAGE(head))
            {
                /* we only want to read as far as head */
                copysize = QUEUE_POS_OFFSET(head) - curoffset;
                if (copysize < 0)
                    copysize = 0;       /* just for safety */
            }
            else
            {
                /* fetch all the rest of the page */
                copysize = QUEUE_PAGESIZE - curoffset;
            }
            memcpy(page_buffer.buf + curoffset,
                   AsyncCtl->shared->page_buffer[slotno] + curoffset,
                   copysize);
            /* Release lock that we got from SimpleLruReadPage_ReadOnly() */
            LWLockRelease(AsyncCtlLock);

            /*
             * Process messages up to the stop position, end of page, or an
             * uncommitted message.
             *
             * Our stop position is what we found to be the head's position
             * when we entered this function. It might have changed already.
             * But if it has, we will receive (or have already received and
             * queued) another signal and come here again.
             *
             * We are not holding AsyncQueueLock here! The queue can only
             * extend beyond the head pointer (see above) and we leave our
             * backend's pointer where it is so nobody will truncate or
             * rewrite pages under us. Especially we don't want to hold a lock
             * while sending the notifications to the frontend.
             */
            reachedStop = asyncQueueProcessPageEntries(&pos, head,
                                                       page_buffer.buf);
        } while (!reachedStop);
    }
    PG_CATCH();
    {
        /* Update shared state */
        LWLockAcquire(AsyncQueueLock, LW_SHARED);
        QUEUE_BACKEND_POS(MyBackendId) = pos;
        advanceTail = QUEUE_POS_EQUAL(oldpos, QUEUE_TAIL);
        LWLockRelease(AsyncQueueLock);

        /* If we were the laziest backend, try to advance the tail pointer */
        if (advanceTail)
            asyncQueueAdvanceTail();

        PG_RE_THROW();
    }
    PG_END_TRY();

    /* Update shared state */
    LWLockAcquire(AsyncQueueLock, LW_SHARED);
    QUEUE_BACKEND_POS(MyBackendId) = pos;
    advanceTail = QUEUE_POS_EQUAL(oldpos, QUEUE_TAIL);
    LWLockRelease(AsyncQueueLock);

    /* If we were the laziest backend, try to advance the tail pointer */
    if (advanceTail)
        asyncQueueAdvanceTail();
}

static void asyncQueueUnregister ( void   )  [static]

Definition at line 1144 of file async.c.

References amRegisteredListener, Assert, asyncQueueAdvanceTail(), AsyncQueueLock, LW_SHARED, LWLockAcquire(), LWLockRelease(), MyBackendId, MyProcPid, NIL, QUEUE_BACKEND_PID, QUEUE_BACKEND_POS, and QUEUE_POS_EQUAL.

Referenced by Async_UnlistenOnExit(), AtAbort_Notify(), and AtCommit_Notify().

{
    bool        advanceTail;

    Assert(listenChannels == NIL);      /* else caller error */

    if (!amRegisteredListener)          /* nothing to do */
        return;

    LWLockAcquire(AsyncQueueLock, LW_SHARED);
    /* check if entry is valid and oldest ... */
    advanceTail = (MyProcPid == QUEUE_BACKEND_PID(MyBackendId)) &&
        QUEUE_POS_EQUAL(QUEUE_BACKEND_POS(MyBackendId), QUEUE_TAIL);
    /* ... then mark it invalid */
    QUEUE_BACKEND_PID(MyBackendId) = InvalidPid;
    LWLockRelease(AsyncQueueLock);

    /* mark ourselves as no longer listed in the global array */
    amRegisteredListener = false;

    /* If we were the laziest backend, try to advance the tail pointer */
    if (advanceTail)
        asyncQueueAdvanceTail();
}

void AsyncShmemInit ( void   ) 

Definition at line 434 of file async.c.

References add_size(), AsyncCtl, AsyncCtlLock, i, AsyncQueueControl::lastQueueFillWarn, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MaxBackends, mul_size(), NULL, NUM_ASYNC_BUFFERS, QUEUE_BACKEND_PID, QUEUE_BACKEND_POS, QUEUE_HEAD, QUEUE_POS_PAGE, QUEUE_TAIL, SET_QUEUE_POS, ShmemInitStruct(), SimpleLruInit(), SimpleLruWritePage(), SimpleLruZeroPage(), SlruScanDirCbDeleteAll(), and SlruScanDirectory().

Referenced by CreateSharedMemoryAndSemaphores().

{
    bool        found;
    int         slotno;
    Size        size;

    /*
     * Create or attach to the AsyncQueueControl structure.
     *
     * The used entries in the backend[] array run from 1 to MaxBackends.
     * sizeof(AsyncQueueControl) already includes space for the unused zero'th
     * entry, but we need to add on space for the used entries.
     */
    size = mul_size(MaxBackends, sizeof(QueueBackendStatus));
    size = add_size(size, sizeof(AsyncQueueControl));

    asyncQueueControl = (AsyncQueueControl *)
        ShmemInitStruct("Async Queue Control", size, &found);

    if (!found)
    {
        /* First time through, so initialize it */
        int         i;

        SET_QUEUE_POS(QUEUE_HEAD, 0, 0);
        SET_QUEUE_POS(QUEUE_TAIL, 0, 0);
        asyncQueueControl->lastQueueFillWarn = 0;
        /* zero'th entry won't be used, but let's initialize it anyway */
        for (i = 0; i <= MaxBackends; i++)
        {
            QUEUE_BACKEND_PID(i) = InvalidPid;
            SET_QUEUE_POS(QUEUE_BACKEND_POS(i), 0, 0);
        }
    }

    /*
     * Set up SLRU management of the pg_notify data.
     */
    AsyncCtl->PagePrecedes = asyncQueuePagePrecedes;
    SimpleLruInit(AsyncCtl, "Async Ctl", NUM_ASYNC_BUFFERS, 0,
                  AsyncCtlLock, "pg_notify");
    /* Override default assumption that writes should be fsync'd */
    AsyncCtl->do_fsync = false;

    if (!found)
    {
        /*
         * During start or reboot, clean out the pg_notify directory.
         */
        (void) SlruScanDirectory(AsyncCtl, SlruScanDirCbDeleteAll, NULL);

        /* Now initialize page zero to empty */
        LWLockAcquire(AsyncCtlLock, LW_EXCLUSIVE);
        slotno = SimpleLruZeroPage(AsyncCtl, QUEUE_POS_PAGE(QUEUE_HEAD));
        /* This write is just to verify that pg_notify/ is writable */
        SimpleLruWritePage(AsyncCtl, slotno);
        LWLockRelease(AsyncCtlLock);
    }
}

Size AsyncShmemSize ( void   ) 

Definition at line 417 of file async.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

{
    Size        size;

    /* This had better match AsyncShmemInit */
    size = mul_size(MaxBackends, sizeof(QueueBackendStatus));
    size = add_size(size, sizeof(AsyncQueueControl));

    size = add_size(size, SimpleLruShmemSize(NUM_ASYNC_BUFFERS, 0));

    return size;
}

void AtAbort_Notify ( void   ) 

Definition at line 1517 of file async.c.

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

Referenced by AbortTransaction().

{
    /*
     * If we LISTEN but then roll back the transaction after PreCommit_Notify,
     * we have registered as a listener but have not made any entry in
     * listenChannels.  In that case, deregister again.
     */
    if (amRegisteredListener && listenChannels == NIL)
        asyncQueueUnregister();

    /* And clean up */
    ClearPendingActionsAndNotifies();
}

void AtCommit_Notify ( void   ) 

Definition at line 861 of file async.c.

References ListenAction::action, 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().

{
    ListCell   *p;

    /*
     * Allow transactions that have not executed LISTEN/UNLISTEN/NOTIFY to
     * return as soon as possible
     */
    if (!pendingActions && !pendingNotifies)
        return;

    if (Trace_notify)
        elog(DEBUG1, "AtCommit_Notify");

    /* Perform any pending listen/unlisten actions */
    foreach(p, pendingActions)
    {
        ListenAction *actrec = (ListenAction *) lfirst(p);

        switch (actrec->action)
        {
            case LISTEN_LISTEN:
                Exec_ListenCommit(actrec->channel);
                break;
            case LISTEN_UNLISTEN:
                Exec_UnlistenCommit(actrec->channel);
                break;
            case LISTEN_UNLISTEN_ALL:
                Exec_UnlistenAllCommit();
                break;
        }
    }

    /* If no longer listening to anything, get out of listener array */
    if (amRegisteredListener && listenChannels == NIL)
        asyncQueueUnregister();

    /* And clean up */
    ClearPendingActionsAndNotifies();
}

void AtPrepare_Notify ( void   ) 

Definition at line 737 of file async.c.

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

Referenced by PrepareTransaction().

{
    /* It's not allowed to have any pending LISTEN/UNLISTEN/NOTIFY actions */
    if (pendingActions || pendingNotifies)
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("cannot PREPARE a transaction that has executed LISTEN, UNLISTEN, or NOTIFY")));
}

void AtSubAbort_Notify ( void   ) 

Definition at line 1599 of file async.c.

References GetCurrentTransactionNestLevel(), linitial, list_delete_first(), and list_length().

Referenced by AbortSubTransaction().

{
    int         my_level = GetCurrentTransactionNestLevel();

    /*
     * All we have to do is pop the stack --- the actions/notifies made in
     * this subxact are no longer interesting, and the space will be freed
     * when CurTransactionContext is recycled.
     *
     * This routine could be called more than once at a given nesting level if
     * there is trouble during subxact abort.  Avoid dumping core by using
     * GetCurrentTransactionNestLevel as the indicator of how far we need to
     * prune the list.
     */
    while (list_length(upperPendingActions) > my_level - 2)
    {
        pendingActions = (List *) linitial(upperPendingActions);
        upperPendingActions = list_delete_first(upperPendingActions);
    }

    while (list_length(upperPendingNotifies) > my_level - 2)
    {
        pendingNotifies = (List *) linitial(upperPendingNotifies);
        upperPendingNotifies = list_delete_first(upperPendingNotifies);
    }
}

void AtSubCommit_Notify ( void   ) 

Definition at line 1567 of file async.c.

References Assert, GetCurrentTransactionNestLevel(), linitial, list_concat(), list_delete_first(), and list_length().

Referenced by CommitSubTransaction().

{
    List       *parentPendingActions;
    List       *parentPendingNotifies;

    parentPendingActions = (List *) linitial(upperPendingActions);
    upperPendingActions = list_delete_first(upperPendingActions);

    Assert(list_length(upperPendingActions) ==
           GetCurrentTransactionNestLevel() - 2);

    /*
     * Mustn't try to eliminate duplicates here --- see queue_listen()
     */
    pendingActions = list_concat(parentPendingActions, pendingActions);

    parentPendingNotifies = (List *) linitial(upperPendingNotifies);
    upperPendingNotifies = list_delete_first(upperPendingNotifies);

    Assert(list_length(upperPendingNotifies) ==
           GetCurrentTransactionNestLevel() - 2);

    /*
     * We could try to eliminate duplicates here, but it seems not worthwhile.
     */
    pendingNotifies = list_concat(parentPendingNotifies, pendingNotifies);
}

void AtSubStart_Notify ( void   ) 
static void ClearPendingActionsAndNotifies ( void   )  [static]

Definition at line 2198 of file async.c.

Referenced by AtAbort_Notify(), and AtCommit_Notify().

{
    /*
     * We used to have to explicitly deallocate the list members and nodes,
     * because they were malloc'd.  Now, since we know they are palloc'd in
     * CurTransactionContext, we need not do that --- they'll go away
     * automatically at transaction exit.  We need only reset the list head
     * pointers.
     */
    pendingActions = NIL;
    pendingNotifies = NIL;
}

bool DisableNotifyInterrupt ( void   ) 
void EnableNotifyInterrupt ( void   ) 

Definition at line 1716 of file async.c.

References DEBUG1, elog, IsTransactionOrTransactionBlock(), notifyInterruptEnabled, notifyInterruptOccurred, ProcessIncomingNotify(), and Trace_notify.

Referenced by prepare_for_client_read(), and ProcessCatchupEvent().

{
    if (IsTransactionOrTransactionBlock())
        return;                 /* not really idle */

    /*
     * This code is tricky because we are communicating with a signal handler
     * that could interrupt us at any point.  If we just checked
     * notifyInterruptOccurred and then set notifyInterruptEnabled, we could
     * fail to respond promptly to a signal that happens in between those two
     * steps.  (A very small time window, perhaps, but Murphy's Law says you
     * can hit it...)  Instead, we first set the enable flag, then test the
     * occurred flag.  If we see an unserviced interrupt has occurred, we
     * re-clear the enable flag before going off to do the service work. (That
     * prevents re-entrant invocation of ProcessIncomingNotify() if another
     * interrupt occurs.) If an interrupt comes in between the setting and
     * clearing of notifyInterruptEnabled, then it will have done the service
     * work and left notifyInterruptOccurred zero, so we have to check again
     * after clearing enable.  The whole thing has to be in a loop in case
     * another interrupt occurs while we're servicing the first. Once we get
     * out of the loop, enable is set and we know there is no unserviced
     * interrupt.
     *
     * NB: an overenthusiastic optimizing compiler could easily break this
     * code. Hopefully, they all understand what "volatile" means these days.
     */
    for (;;)
    {
        notifyInterruptEnabled = 1;
        if (!notifyInterruptOccurred)
            break;
        notifyInterruptEnabled = 0;
        if (notifyInterruptOccurred)
        {
            if (Trace_notify)
                elog(DEBUG1, "EnableNotifyInterrupt: perform async notify");

            ProcessIncomingNotify();

            if (Trace_notify)
                elog(DEBUG1, "EnableNotifyInterrupt: done");
        }
    }
}

static void Exec_ListenCommit ( const char *  channel  )  [static]

Definition at line 965 of file async.c.

References IsListeningOn(), lappend(), MemoryContextSwitchTo(), pstrdup(), and TopMemoryContext.

Referenced by AtCommit_Notify().

{
    MemoryContext oldcontext;

    /* Do nothing if we are already listening on this channel */
    if (IsListeningOn(channel))
        return;

    /*
     * Add the new channel name to listenChannels.
     *
     * XXX It is theoretically possible to get an out-of-memory failure here,
     * which would be bad because we already committed.  For the moment it
     * doesn't seem worth trying to guard against that, but maybe improve this
     * later.
     */
    oldcontext = MemoryContextSwitchTo(TopMemoryContext);
    listenChannels = lappend(listenChannels, pstrdup(channel));
    MemoryContextSwitchTo(oldcontext);
}

static void Exec_ListenPreCommit ( void   )  [static]

Definition at line 908 of file async.c.

References amRegisteredListener, Async_UnlistenOnExit(), AsyncQueueLock, asyncQueueReadAllNotifications(), DEBUG1, elog, LW_SHARED, LWLockAcquire(), LWLockRelease(), MyBackendId, MyProcPid, on_shmem_exit(), QUEUE_BACKEND_PID, QUEUE_BACKEND_POS, Trace_notify, and unlistenExitRegistered.

Referenced by PreCommit_Notify().

{
    /*
     * Nothing to do if we are already listening to something, nor if we
     * already ran this routine in this transaction.
     */
    if (amRegisteredListener)
        return;

    if (Trace_notify)
        elog(DEBUG1, "Exec_ListenPreCommit(%d)", MyProcPid);

    /*
     * Before registering, make sure we will unlisten before dying. (Note:
     * this action does not get undone if we abort later.)
     */
    if (!unlistenExitRegistered)
    {
        on_shmem_exit(Async_UnlistenOnExit, 0);
        unlistenExitRegistered = true;
    }

    /*
     * This is our first LISTEN, so establish our pointer.
     *
     * We set our pointer to the global tail pointer and then move it forward
     * over already-committed notifications.  This ensures we cannot miss any
     * not-yet-committed notifications.  We might get a few more but that
     * doesn't hurt.
     */
    LWLockAcquire(AsyncQueueLock, LW_SHARED);
    QUEUE_BACKEND_POS(MyBackendId) = QUEUE_TAIL;
    QUEUE_BACKEND_PID(MyBackendId) = MyProcPid;
    LWLockRelease(AsyncQueueLock);

    /* Now we are listed in the global array, so remember we're listening */
    amRegisteredListener = true;

    /*
     * Try to move our pointer forward as far as possible. This will skip over
     * already-committed notifications. Still, we could get notifications that
     * have already committed before we started to LISTEN.
     *
     * Note that we are not yet listening on anything, so we won't deliver any
     * notification to the frontend.
     *
     * This will also advance the global tail pointer if possible.
     */
    asyncQueueReadAllNotifications();
}

static void Exec_UnlistenAllCommit ( void   )  [static]

Definition at line 1026 of file async.c.

References DEBUG1, elog, list_free_deep(), MyProcPid, and Trace_notify.

Referenced by Async_UnlistenOnExit(), and AtCommit_Notify().

{
    if (Trace_notify)
        elog(DEBUG1, "Exec_UnlistenAllCommit(%d)", MyProcPid);

    list_free_deep(listenChannels);
    listenChannels = NIL;
}

static void Exec_UnlistenCommit ( const char *  channel  )  [static]

Definition at line 992 of file async.c.

References DEBUG1, elog, lfirst, list_delete_cell(), MyProcPid, pfree(), and Trace_notify.

Referenced by AtCommit_Notify().

{
    ListCell   *q;
    ListCell   *prev;

    if (Trace_notify)
        elog(DEBUG1, "Exec_UnlistenCommit(%s,%d)", channel, MyProcPid);

    prev = NULL;
    foreach(q, listenChannels)
    {
        char       *lchan = (char *) lfirst(q);

        if (strcmp(lchan, channel) == 0)
        {
            listenChannels = list_delete_cell(listenChannels, q, prev);
            pfree(lchan);
            break;
        }
        prev = q;
    }

    /*
     * We do not complain about unlistening something not being listened;
     * should we?
     */
}

void HandleNotifyInterrupt ( void   ) 

Definition at line 1636 of file async.c.

References CHECK_FOR_INTERRUPTS, DEBUG1, elog, ImmediateInterruptOK, notifyInterruptEnabled, notifyInterruptOccurred, proc_exit_inprogress, ProcessIncomingNotify(), and Trace_notify.

Referenced by procsignal_sigusr1_handler().

{
    /*
     * Note: this is called by a SIGNAL HANDLER. You must be very wary what
     * you do here. Some helpful soul had this routine sprinkled with
     * TPRINTFs, which would likely lead to corruption of stdio buffers if
     * they were ever turned on.
     */

    /* Don't joggle the elbow of proc_exit */
    if (proc_exit_inprogress)
        return;

    if (notifyInterruptEnabled)
    {
        bool        save_ImmediateInterruptOK = ImmediateInterruptOK;

        /*
         * We may be called while ImmediateInterruptOK is true; turn it off
         * while messing with the NOTIFY state.  (We would have to save and
         * restore it anyway, because PGSemaphore operations inside
         * ProcessIncomingNotify() might reset it.)
         */
        ImmediateInterruptOK = false;

        /*
         * I'm not sure whether some flavors of Unix might allow another
         * SIGUSR1 occurrence to recursively interrupt this routine. To cope
         * with the possibility, we do the same sort of dance that
         * EnableNotifyInterrupt must do --- see that routine for comments.
         */
        notifyInterruptEnabled = 0;     /* disable any recursive signal */
        notifyInterruptOccurred = 1;    /* do at least one iteration */
        for (;;)
        {
            notifyInterruptEnabled = 1;
            if (!notifyInterruptOccurred)
                break;
            notifyInterruptEnabled = 0;
            if (notifyInterruptOccurred)
            {
                /* Here, it is finally safe to do stuff. */
                if (Trace_notify)
                    elog(DEBUG1, "HandleNotifyInterrupt: perform async notify");

                ProcessIncomingNotify();

                if (Trace_notify)
                    elog(DEBUG1, "HandleNotifyInterrupt: done");
            }
        }

        /*
         * Restore ImmediateInterruptOK, and check for interrupts if needed.
         */
        ImmediateInterruptOK = save_ImmediateInterruptOK;
        if (save_ImmediateInterruptOK)
            CHECK_FOR_INTERRUPTS();
    }
    else
    {
        /*
         * In this path it is NOT SAFE to do much of anything, except this:
         */
        notifyInterruptOccurred = 1;
    }
}

static bool IsListeningOn ( const char *  channel  )  [static]

Definition at line 1125 of file async.c.

References lfirst.

Referenced by asyncQueueProcessPageEntries(), and Exec_ListenCommit().

{
    ListCell   *p;

    foreach(p, listenChannels)
    {
        char       *lchan = (char *) lfirst(p);

        if (strcmp(lchan, channel) == 0)
            return true;
    }
    return false;
}

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

Definition at line 2125 of file async.c.

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

Referenced by asyncQueueProcessPageEntries().

{
    if (whereToSendOutput == DestRemote)
    {
        StringInfoData buf;

        pq_beginmessage(&buf, 'A');
        pq_sendint(&buf, srcPid, sizeof(int32));
        pq_sendstring(&buf, channel);
        if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
            pq_sendstring(&buf, payload);
        pq_endmessage(&buf);

        /*
         * NOTE: we do not do pq_flush() here.  For a self-notify, it will
         * happen at the end of the transaction, and for incoming notifies
         * ProcessIncomingNotify will do it after finding all the notifies.
         */
    }
    else
        elog(INFO, "NOTIFY for \"%s\" payload \"%s\"", channel, payload);
}

Datum pg_listening_channels ( PG_FUNCTION_ARGS   ) 

Definition at line 677 of file async.c.

References CStringGetTextDatum, lfirst, list_head(), lnext, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, NULL, palloc(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, and FuncCallContext::user_fctx.

{
    FuncCallContext *funcctx;
    ListCell  **lcp;

    /* stuff done only on the first call of the function */
    if (SRF_IS_FIRSTCALL())
    {
        MemoryContext oldcontext;

        /* create a function context for cross-call persistence */
        funcctx = SRF_FIRSTCALL_INIT();

        /* switch to memory context appropriate for multiple function calls */
        oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

        /* allocate memory for user context */
        lcp = (ListCell **) palloc(sizeof(ListCell *));
        *lcp = list_head(listenChannels);
        funcctx->user_fctx = (void *) lcp;

        MemoryContextSwitchTo(oldcontext);
    }

    /* stuff done on every call of the function */
    funcctx = SRF_PERCALL_SETUP();
    lcp = (ListCell **) funcctx->user_fctx;

    while (*lcp != NULL)
    {
        char       *channel = (char *) lfirst(*lcp);

        *lcp = lnext(*lcp);
        SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(channel));
    }

    SRF_RETURN_DONE(funcctx);
}

Datum pg_notify ( PG_FUNCTION_ARGS   ) 

Definition at line 500 of file async.c.

References Async_Notify(), PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, PreventCommandDuringRecovery(), and text_to_cstring().

{
    const char *channel;
    const char *payload;

    if (PG_ARGISNULL(0))
        channel = "";
    else
        channel = text_to_cstring(PG_GETARG_TEXT_PP(0));

    if (PG_ARGISNULL(1))
        payload = "";
    else
        payload = text_to_cstring(PG_GETARG_TEXT_PP(1));

    /* For NOTIFY as a statement, this is checked in ProcessUtility */
    PreventCommandDuringRecovery("NOTIFY");

    Async_Notify(channel, payload);

    PG_RETURN_VOID();
}

void PreCommit_Notify ( void   ) 

Definition at line 762 of file async.c.

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

Referenced by CommitTransaction().

{
    ListCell   *p;

    if (pendingActions == NIL && pendingNotifies == NIL)
        return;                 /* no relevant statements in this xact */

    if (Trace_notify)
        elog(DEBUG1, "PreCommit_Notify");

    /* Preflight for any pending listen/unlisten actions */
    foreach(p, pendingActions)
    {
        ListenAction *actrec = (ListenAction *) lfirst(p);

        switch (actrec->action)
        {
            case LISTEN_LISTEN:
                Exec_ListenPreCommit();
                break;
            case LISTEN_UNLISTEN:
                /* there is no Exec_UnlistenPreCommit() */
                break;
            case LISTEN_UNLISTEN_ALL:
                /* there is no Exec_UnlistenAllPreCommit() */
                break;
        }
    }

    /* Queue any pending notifies */
    if (pendingNotifies)
    {
        ListCell   *nextNotify;

        /*
         * Make sure that we have an XID assigned to the current transaction.
         * GetCurrentTransactionId is cheap if we already have an XID, but not
         * so cheap if we don't, and we'd prefer not to do that work while
         * holding AsyncQueueLock.
         */
        (void) GetCurrentTransactionId();

        /*
         * Serialize writers by acquiring a special lock that we hold till
         * after commit.  This ensures that queue entries appear in commit
         * order, and in particular that there are never uncommitted queue
         * entries ahead of committed ones, so an uncommitted transaction
         * can't block delivery of deliverable notifications.
         *
         * We use a heavyweight lock so that it'll automatically be released
         * after either commit or abort.  This also allows deadlocks to be
         * detected, though really a deadlock shouldn't be possible here.
         *
         * The lock is on "database 0", which is pretty ugly but it doesn't
         * seem worth inventing a special locktag category just for this.
         * (Historical note: before PG 9.0, a similar lock on "database 0" was
         * used by the flatfiles mechanism.)
         */
        LockSharedObject(DatabaseRelationId, InvalidOid, 0,
                         AccessExclusiveLock);

        /* Now push the notifications into the queue */
        backendHasSentNotifications = true;

        nextNotify = list_head(pendingNotifies);
        while (nextNotify != NULL)
        {
            /*
             * Add the pending notifications to the queue.  We acquire and
             * release AsyncQueueLock once per page, which might be overkill
             * but it does allow readers to get in while we're doing this.
             *
             * A full queue is very uncommon and should really not happen,
             * given that we have so much space available in the SLRU pages.
             * Nevertheless we need to deal with this possibility. Note that
             * when we get here we are in the process of committing our
             * transaction, but we have not yet committed to clog, so at this
             * point in time we can still roll the transaction back.
             */
            LWLockAcquire(AsyncQueueLock, LW_EXCLUSIVE);
            asyncQueueFillWarning();
            if (asyncQueueIsFull())
                ereport(ERROR,
                        (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                      errmsg("too many notifications in the NOTIFY queue")));
            nextNotify = asyncQueueAddEntries(nextNotify);
            LWLockRelease(AsyncQueueLock);
        }
    }
}

void ProcessCompletedNotifies ( void   ) 

Definition at line 1057 of file async.c.

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

Referenced by PostgresMain().

{
    MemoryContext caller_context;
    bool        signalled;

    /* Nothing to do if we didn't send any notifications */
    if (!backendHasSentNotifications)
        return;

    /*
     * We reset the flag immediately; otherwise, if any sort of error occurs
     * below, we'd be locked up in an infinite loop, because control will come
     * right back here after error cleanup.
     */
    backendHasSentNotifications = false;

    /*
     * We must preserve the caller's memory context (probably MessageContext)
     * across the transaction we do here.
     */
    caller_context = CurrentMemoryContext;

    if (Trace_notify)
        elog(DEBUG1, "ProcessCompletedNotifies");

    /*
     * We must run asyncQueueReadAllNotifications inside a transaction, else
     * bad things happen if it gets an error.
     */
    StartTransactionCommand();

    /* Send signals to other backends */
    signalled = SignalBackends();

    if (listenChannels != NIL)
    {
        /* Read the queue ourselves, and send relevant stuff to the frontend */
        asyncQueueReadAllNotifications();
    }
    else if (!signalled)
    {
        /*
         * If we found no other listening backends, and we aren't listening
         * ourselves, then we must execute asyncQueueAdvanceTail to flush the
         * queue, because ain't nobody else gonna do it.  This prevents queue
         * overflow when we're sending useless notifies to nobody. (A new
         * listener could have joined since we looked, but if so this is
         * harmless.)
         */
        asyncQueueAdvanceTail();
    }

    CommitTransactionCommand();

    MemoryContextSwitchTo(caller_context);

    /* We don't need pq_flush() here since postgres.c will do one shortly */
}

static void ProcessIncomingNotify ( void   )  [static]

Definition at line 2078 of file async.c.

References asyncQueueReadAllNotifications(), CommitTransactionCommand(), DEBUG1, DisableCatchupInterrupt(), elog, EnableCatchupInterrupt(), NIL, notifyInterruptOccurred, pq_flush(), set_ps_display(), StartTransactionCommand(), and Trace_notify.

Referenced by EnableNotifyInterrupt(), and HandleNotifyInterrupt().

{
    bool        catchup_enabled;

    /* We *must* reset the flag */
    notifyInterruptOccurred = 0;

    /* Do nothing else if we aren't actively listening */
    if (listenChannels == NIL)
        return;

    /* Must prevent catchup interrupt while I am running */
    catchup_enabled = DisableCatchupInterrupt();

    if (Trace_notify)
        elog(DEBUG1, "ProcessIncomingNotify");

    set_ps_display("notify interrupt", false);

    /*
     * We must run asyncQueueReadAllNotifications inside a transaction, else
     * bad things happen if it gets an error.
     */
    StartTransactionCommand();

    asyncQueueReadAllNotifications();

    CommitTransactionCommand();

    /*
     * Must flush the notify messages to ensure frontend gets them promptly.
     */
    pq_flush();

    set_ps_display("idle", false);

    if (Trace_notify)
        elog(DEBUG1, "ProcessIncomingNotify: done");

    if (catchup_enabled)
        EnableCatchupInterrupt();
}

static void queue_listen ( ListenActionKind  action,
const char *  channel 
) [static]

Definition at line 596 of file async.c.

References ListenAction::action, ListenAction::channel, CurTransactionContext, lappend(), MemoryContextSwitchTo(), and palloc().

Referenced by Async_Listen(), Async_Unlisten(), and Async_UnlistenAll().

{
    MemoryContext oldcontext;
    ListenAction *actrec;

    /*
     * Unlike Async_Notify, we don't try to collapse out duplicates. It would
     * be too complicated to ensure we get the right interactions of
     * conflicting LISTEN/UNLISTEN/UNLISTEN_ALL, and it's unlikely that there
     * would be any performance benefit anyway in sane applications.
     */
    oldcontext = MemoryContextSwitchTo(CurTransactionContext);

    /* space for terminating null is included in sizeof(ListenAction) */
    actrec = (ListenAction *) palloc(sizeof(ListenAction) + strlen(channel));
    actrec->action = action;
    strcpy(actrec->channel, channel);

    pendingActions = lappend(pendingActions, actrec);

    MemoryContextSwitchTo(oldcontext);
}

static bool SignalBackends ( void   )  [static]

Definition at line 1445 of file async.c.

References AsyncQueueLock, DEBUG3, elog, i, InvalidPid, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MaxBackends, MyProcPid, palloc(), pfree(), PROCSIG_NOTIFY_INTERRUPT, QUEUE_BACKEND_PID, QUEUE_BACKEND_POS, QUEUE_HEAD, QUEUE_POS_EQUAL, and SendProcSignal().

Referenced by ProcessCompletedNotifies().

{
    bool        signalled = false;
    int32      *pids;
    BackendId  *ids;
    int         count;
    int         i;
    int32       pid;

    /*
     * Identify all backends that are listening and not already up-to-date. We
     * don't want to send signals while holding the AsyncQueueLock, so we just
     * build a list of target PIDs.
     *
     * XXX in principle these pallocs could fail, which would be bad. Maybe
     * preallocate the arrays?  But in practice this is only run in trivial
     * transactions, so there should surely be space available.
     */
    pids = (int32 *) palloc(MaxBackends * sizeof(int32));
    ids = (BackendId *) palloc(MaxBackends * sizeof(BackendId));
    count = 0;

    LWLockAcquire(AsyncQueueLock, LW_EXCLUSIVE);
    for (i = 1; i <= MaxBackends; i++)
    {
        pid = QUEUE_BACKEND_PID(i);
        if (pid != InvalidPid && pid != MyProcPid)
        {
            QueuePosition pos = QUEUE_BACKEND_POS(i);

            if (!QUEUE_POS_EQUAL(pos, QUEUE_HEAD))
            {
                pids[count] = pid;
                ids[count] = i;
                count++;
            }
        }
    }
    LWLockRelease(AsyncQueueLock);

    /* Now send signals */
    for (i = 0; i < count; i++)
    {
        pid = pids[i];

        /*
         * Note: assuming things aren't broken, a signal failure here could
         * only occur if the target backend exited since we released
         * AsyncQueueLock; which is unlikely but certainly possible. So we
         * just log a low-level debug message if it happens.
         */
        if (SendProcSignal(pid, PROCSIG_NOTIFY_INTERRUPT, ids[i]) < 0)
            elog(DEBUG3, "could not signal backend with PID %d: %m", pid);
        else
            signalled = true;
    }

    pfree(pids);
    pfree(ids);

    return signalled;
}


Variable Documentation

bool amRegisteredListener = false [static]

Definition at line 252 of file async.c.

Definition at line 242 of file async.c.

Definition at line 357 of file async.c.

Referenced by PreCommit_Notify(), and ProcessCompletedNotifies().

List* listenChannels = NIL [static]

Definition at line 282 of file async.c.

volatile sig_atomic_t notifyInterruptEnabled = 0 [static]

Definition at line 347 of file async.c.

Referenced by DisableNotifyInterrupt(), EnableNotifyInterrupt(), and HandleNotifyInterrupt().

volatile sig_atomic_t notifyInterruptOccurred = 0 [static]

Definition at line 348 of file async.c.

Referenced by EnableNotifyInterrupt(), HandleNotifyInterrupt(), and ProcessIncomingNotify().

List* pendingActions = NIL [static]

Definition at line 307 of file async.c.

List* pendingNotifies = NIL [static]

Definition at line 333 of file async.c.

bool Trace_notify = false
bool unlistenExitRegistered = false [static]

Definition at line 351 of file async.c.

Referenced by Async_Unlisten(), Async_UnlistenAll(), and Exec_ListenPreCommit().

List* upperPendingActions = NIL [static]

Definition at line 309 of file async.c.

List* upperPendingNotifies = NIL [static]

Definition at line 335 of file async.c.