1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-27 23:21:58 +03:00

Add new function pg_notification_queue_usage.

This tells you what fraction of NOTIFY's queue is currently filled.

Brendan Jurd, reviewed by Merlin Moncure and Gurjeet Singh.  A few
further tweaks by me.
This commit is contained in:
Robert Haas
2015-07-17 09:12:03 -04:00
parent 43d89a23d5
commit a04bb65f70
10 changed files with 115 additions and 19 deletions

View File

@ -371,6 +371,7 @@ static bool asyncQueueIsFull(void);
static bool asyncQueueAdvance(volatile QueuePosition *position, int entryLength);
static void asyncQueueNotificationToEntry(Notification *n, AsyncQueueEntry *qe);
static ListCell *asyncQueueAddEntries(ListCell *nextNotify);
static double asyncQueueUsage(void);
static void asyncQueueFillWarning(void);
static bool SignalBackends(void);
static void asyncQueueReadAllNotifications(void);
@ -1361,6 +1362,48 @@ asyncQueueAddEntries(ListCell *nextNotify)
return nextNotify;
}
/*
* SQL function to return the fraction of the notification queue currently
* occupied.
*/
Datum
pg_notification_queue_usage(PG_FUNCTION_ARGS)
{
double usage;
LWLockAcquire(AsyncQueueLock, LW_SHARED);
usage = asyncQueueUsage();
LWLockRelease(AsyncQueueLock);
PG_RETURN_FLOAT8(usage);
}
/*
* Return the fraction of the queue that is currently occupied.
*
* The caller must hold AysncQueueLock in (at least) shared mode.
*/
static double
asyncQueueUsage(void)
{
int headPage = QUEUE_POS_PAGE(QUEUE_HEAD);
int tailPage = QUEUE_POS_PAGE(QUEUE_TAIL);
int occupied;
occupied = headPage - tailPage;
if (occupied == 0)
return (double) 0; /* fast exit for common case */
if (occupied < 0)
{
/* head has wrapped around, tail not yet */
occupied += QUEUE_MAX_PAGE + 1;
}
return (double) occupied / (double) ((QUEUE_MAX_PAGE + 1) / 2);
}
/*
* Check whether the queue is at least half full, and emit a warning if so.
*
@ -1372,25 +1415,10 @@ asyncQueueAddEntries(ListCell *nextNotify)
static void
asyncQueueFillWarning(void)
{
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);
fillDegree = asyncQueueUsage();
if (fillDegree < 0.5)
return;