Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit03f9b63

Browse files
committed
Improve LISTEN startup time when there are many unread notifications.
If some existing listener is far behind, incoming new listener sessionswould start from that session's read pointer and then need to advance overmany already-committed notification messages, which they have no interestin. This was expensive in itself and also thrashed the pg_notify SLRUbuffers a lot more than necessary. We can improve matters considerablyin typical scenarios, without much added cost, by starting from thefurthest-ahead read pointer, not the furthest-behind one. We do have toconsider only sessions in our own database when doing this, which requiresan extra field in the data structure, but that's a pretty small cost.Back-patch to 9.0 where the current LISTEN/NOTIFY logic was introduced.Matt Newell, slightly adjusted by me
1 parentb62c870 commit03f9b63

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

‎src/backend/commands/async.c

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,19 @@ typedef struct QueuePosition
199199
(x).page != (y).page ? (y) : \
200200
(x).offset < (y).offset ? (x) : (y))
201201

202+
/* choose logically larger QueuePosition */
203+
#defineQUEUE_POS_MAX(x,y) \
204+
(asyncQueuePagePrecedes((x).page, (y).page) ? (y) : \
205+
(x).page != (y).page ? (x) : \
206+
(x).offset > (y).offset ? (x) : (y))
207+
202208
/*
203209
* Struct describing a listening backend's status
204210
*/
205211
typedefstructQueueBackendStatus
206212
{
207213
int32pid;/* either a PID or InvalidPid */
214+
Oiddboid;/* backend's database OID, or InvalidOid */
208215
QueuePositionpos;/* backend has read queue up to here */
209216
}QueueBackendStatus;
210217

@@ -221,6 +228,7 @@ typedef struct QueueBackendStatus
221228
* When holding the lock in EXCLUSIVE mode, backends can inspect the entries
222229
* of other backends and also change the head and tail pointers.
223230
*
231+
* AsyncCtlLock is used as the control lock for the pg_notify SLRU buffers.
224232
* In order to avoid deadlocks, whenever we need both locks, we always first
225233
* get AsyncQueueLock and then AsyncCtlLock.
226234
*
@@ -231,8 +239,8 @@ typedef struct QueueBackendStatus
231239
typedefstructAsyncQueueControl
232240
{
233241
QueuePositionhead;/* head points to the next free location */
234-
QueuePositiontail;/* the global tail is equivalent to thetail
235-
*ofthe "slowest" backend */
242+
QueuePositiontail;/* the global tail is equivalent to thepos of
243+
* the "slowest" backend */
236244
TimestampTzlastQueueFillWarn;/* time of last queue-full msg */
237245
QueueBackendStatusbackend[1];/* actually of length MaxBackends+1 */
238246
/* DO NOT ADD FURTHER STRUCT MEMBERS HERE */
@@ -243,6 +251,7 @@ static AsyncQueueControl *asyncQueueControl;
243251
#defineQUEUE_HEAD(asyncQueueControl->head)
244252
#defineQUEUE_TAIL(asyncQueueControl->tail)
245253
#defineQUEUE_BACKEND_PID(i)(asyncQueueControl->backend[i].pid)
254+
#defineQUEUE_BACKEND_DBOID(i)(asyncQueueControl->backend[i].dboid)
246255
#defineQUEUE_BACKEND_POS(i)(asyncQueueControl->backend[i].pos)
247256

248257
/*
@@ -461,6 +470,7 @@ AsyncShmemInit(void)
461470
for (i=0;i <=MaxBackends;i++)
462471
{
463472
QUEUE_BACKEND_PID(i)=InvalidPid;
473+
QUEUE_BACKEND_DBOID(i)=InvalidOid;
464474
SET_QUEUE_POS(QUEUE_BACKEND_POS(i),0,0);
465475
}
466476
}
@@ -906,6 +916,10 @@ AtCommit_Notify(void)
906916
staticvoid
907917
Exec_ListenPreCommit(void)
908918
{
919+
QueuePositionhead;
920+
QueuePositionmax;
921+
inti;
922+
909923
/*
910924
* Nothing to do if we are already listening to something, nor if we
911925
* already ran this routine in this transaction.
@@ -933,10 +947,34 @@ Exec_ListenPreCommit(void)
933947
* over already-committed notifications. This ensures we cannot miss any
934948
* not-yet-committed notifications. We might get a few more but that
935949
* doesn't hurt.
950+
*
951+
* In some scenarios there might be a lot of committed notifications that
952+
* have not yet been pruned away (because some backend is being lazy about
953+
* reading them). To reduce our startup time, we can look at other
954+
* backends and adopt the maximum "pos" pointer of any backend that's in
955+
* our database; any notifications it's already advanced over are surely
956+
* committed and need not be re-examined by us. (We must consider only
957+
* backends connected to our DB, because others will not have bothered to
958+
* check committed-ness of notifications in our DB.) But we only bother
959+
* with that if there's more than a page worth of notifications
960+
* outstanding, otherwise scanning all the other backends isn't worth it.
961+
*
962+
* We need exclusive lock here so we can look at other backends' entries.
936963
*/
937-
LWLockAcquire(AsyncQueueLock,LW_SHARED);
938-
QUEUE_BACKEND_POS(MyBackendId)=QUEUE_TAIL;
964+
LWLockAcquire(AsyncQueueLock,LW_EXCLUSIVE);
965+
head=QUEUE_HEAD;
966+
max=QUEUE_TAIL;
967+
if (QUEUE_POS_PAGE(max)!=QUEUE_POS_PAGE(head))
968+
{
969+
for (i=1;i <=MaxBackends;i++)
970+
{
971+
if (QUEUE_BACKEND_DBOID(i)==MyDatabaseId)
972+
max=QUEUE_POS_MAX(max,QUEUE_BACKEND_POS(i));
973+
}
974+
}
975+
QUEUE_BACKEND_POS(MyBackendId)=max;
939976
QUEUE_BACKEND_PID(MyBackendId)=MyProcPid;
977+
QUEUE_BACKEND_DBOID(MyBackendId)=MyDatabaseId;
940978
LWLockRelease(AsyncQueueLock);
941979

942980
/* Now we are listed in the global array, so remember we're listening */
@@ -952,7 +990,8 @@ Exec_ListenPreCommit(void)
952990
*
953991
* This will also advance the global tail pointer if possible.
954992
*/
955-
asyncQueueReadAllNotifications();
993+
if (!QUEUE_POS_EQUAL(max,head))
994+
asyncQueueReadAllNotifications();
956995
}
957996

958997
/*
@@ -1155,6 +1194,7 @@ asyncQueueUnregister(void)
11551194
QUEUE_POS_EQUAL(QUEUE_BACKEND_POS(MyBackendId),QUEUE_TAIL);
11561195
/* ... then mark it invalid */
11571196
QUEUE_BACKEND_PID(MyBackendId)=InvalidPid;
1197+
QUEUE_BACKEND_DBOID(MyBackendId)=InvalidOid;
11581198
LWLockRelease(AsyncQueueLock);
11591199

11601200
/* mark ourselves as no longer listed in the global array */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp