Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 25f4242

Browse files
committed
Use an LWLock instead of a spinlock in waitlsn.c
This should prevent busy-waiting when number of waiting processes is high. Discussion: https://postgr.es/m/202404030658.hhj3vfxeyhft%40alvherre.pgsql Author: Alvaro Herrera
1 parent 1577081 commit 25f4242

File tree

4 files changed

+10
-12
lines changed

4 files changed

+10
-12
lines changed

src/backend/commands/waitlsn.c

+7-8
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ WaitLSNShmemInit(void)
5858
&found);
5959
if (!found)
6060
{
61-
SpinLockInit(&waitLSN->waitersHeapMutex);
6261
pg_atomic_init_u64(&waitLSN->minWaitedLSN, PG_UINT64_MAX);
6362
pairingheap_initialize(&waitLSN->waitersHeap, lsn_cmp, NULL);
6463
memset(&waitLSN->procInfos, 0, MaxBackends * sizeof(WaitLSNProcInfo));
@@ -115,13 +114,13 @@ addLSNWaiter(XLogRecPtr lsn)
115114
procInfo->procnum = MyProcNumber;
116115
procInfo->waitLSN = lsn;
117116

118-
SpinLockAcquire(&waitLSN->waitersHeapMutex);
117+
LWLockAcquire(WaitLSNLock, LW_EXCLUSIVE);
119118

120119
pairingheap_add(&waitLSN->waitersHeap, &procInfo->phNode);
121120
procInfo->inHeap = true;
122121
updateMinWaitedLSN();
123122

124-
SpinLockRelease(&waitLSN->waitersHeapMutex);
123+
LWLockRelease(WaitLSNLock);
125124
}
126125

127126
/*
@@ -132,19 +131,19 @@ deleteLSNWaiter(void)
132131
{
133132
WaitLSNProcInfo *procInfo = &waitLSN->procInfos[MyProcNumber];
134133

135-
SpinLockAcquire(&waitLSN->waitersHeapMutex);
134+
LWLockAcquire(WaitLSNLock, LW_EXCLUSIVE);
136135

137136
if (!procInfo->inHeap)
138137
{
139-
SpinLockRelease(&waitLSN->waitersHeapMutex);
138+
LWLockRelease(WaitLSNLock);
140139
return;
141140
}
142141

143142
pairingheap_remove(&waitLSN->waitersHeap, &procInfo->phNode);
144143
procInfo->inHeap = false;
145144
updateMinWaitedLSN();
146145

147-
SpinLockRelease(&waitLSN->waitersHeapMutex);
146+
LWLockRelease(WaitLSNLock);
148147
}
149148

150149
/*
@@ -160,7 +159,7 @@ WaitLSNSetLatches(XLogRecPtr currentLSN)
160159

161160
wakeUpProcNums = palloc(sizeof(int) * MaxBackends);
162161

163-
SpinLockAcquire(&waitLSN->waitersHeapMutex);
162+
LWLockAcquire(WaitLSNLock, LW_EXCLUSIVE);
164163

165164
/*
166165
* Iterate the pairing heap of waiting processes till we find LSN not yet
@@ -182,7 +181,7 @@ WaitLSNSetLatches(XLogRecPtr currentLSN)
182181

183182
updateMinWaitedLSN();
184183

185-
SpinLockRelease(&waitLSN->waitersHeapMutex);
184+
LWLockRelease(WaitLSNLock);
186185

187186
/*
188187
* Set latches for processes, whose waited LSNs are already replayed. This

src/backend/utils/activity/wait_event_names.txt

+1
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ WALSummarizer "Waiting to read or update WAL summarization state."
345345
DSMRegistry "Waiting to read or update the dynamic shared memory registry."
346346
InjectionPoint "Waiting to read or update information related to injection points."
347347
SerialControl "Waiting to read or update shared <filename>pg_serial</filename> state."
348+
WaitLSN "Waiting to read or update shared Wait-for-LSN state."
348349

349350
#
350351
# END OF PREDEFINED LWLOCKS (DO NOT CHANGE THIS LINE)

src/include/commands/waitlsn.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,10 @@ typedef struct WaitLSNState
5555

5656
/*
5757
* A pairing heap of waiting processes order by LSN values (least LSN is
58-
* on top).
58+
* on top). Protected by WaitLSNLock.
5959
*/
6060
pairingheap waitersHeap;
6161

62-
/* A mutex protecting the pairing heap above */
63-
slock_t waitersHeapMutex;
64-
6562
/* An array with per-process information, indexed by the process number */
6663
WaitLSNProcInfo procInfos[FLEXIBLE_ARRAY_MEMBER];
6764
} WaitLSNState;

src/include/storage/lwlocklist.h

+1
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,4 @@ PG_LWLOCK(49, WALSummarizer)
8383
PG_LWLOCK(50, DSMRegistry)
8484
PG_LWLOCK(51, InjectionPoint)
8585
PG_LWLOCK(52, SerialControl)
86+
PG_LWLOCK(53, WaitLSN)

0 commit comments

Comments
 (0)