Skip to content

Commit ee79928

Browse files
committed
Clarify what is protected by WaitLSNLock
Not just WaitLSNState.waitersHeap, but also WaitLSNState.procInfos and updating of WaitLSNState.minWaitedLSN is protected by WaitLSNLock. There is one now documented exclusion on fast-path checking of WaitLSNProcInfo.inHeap flag. Discussion: https://postgr.es/m/202404030658.hhj3vfxeyhft%40alvherre.pgsql
1 parent 25f4242 commit ee79928

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/backend/commands/waitlsn.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ addLSNWaiter(XLogRecPtr lsn)
109109
{
110110
WaitLSNProcInfo *procInfo = &waitLSN->procInfos[MyProcNumber];
111111

112+
LWLockAcquire(WaitLSNLock, LW_EXCLUSIVE);
113+
112114
Assert(!procInfo->inHeap);
113115

114116
procInfo->procnum = MyProcNumber;
115117
procInfo->waitLSN = lsn;
116118

117-
LWLockAcquire(WaitLSNLock, LW_EXCLUSIVE);
118-
119119
pairingheap_add(&waitLSN->waitersHeap, &procInfo->phNode);
120120
procInfo->inHeap = true;
121121
updateMinWaitedLSN();
@@ -203,6 +203,12 @@ WaitLSNSetLatches(XLogRecPtr currentLSN)
203203
void
204204
WaitLSNCleanup(void)
205205
{
206+
/*
207+
* We do a fast-path check of the 'inHeap' flag without the lock. This
208+
* flag is set to true only by the process itself. So, it's only possible
209+
* to get a false positive. But that will be eliminated by a recheck
210+
* inside deleteLSNWaiter().
211+
*/
206212
if (waitLSN->procInfos[MyProcNumber].inHeap)
207213
deleteLSNWaiter();
208214
}

src/include/commands/waitlsn.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ typedef struct WaitLSNState
4949
/*
5050
* The minimum LSN value some process is waiting for. Used for the
5151
* fast-path checking if we need to wake up any waiters after replaying a
52-
* WAL record.
52+
* WAL record. Could be read lock-less. Update protected by WaitLSNLock.
5353
*/
5454
pg_atomic_uint64 minWaitedLSN;
5555

@@ -59,7 +59,10 @@ typedef struct WaitLSNState
5959
*/
6060
pairingheap waitersHeap;
6161

62-
/* An array with per-process information, indexed by the process number */
62+
/*
63+
* An array with per-process information, indexed by the process number.
64+
* Protected by WaitLSNLock.
65+
*/
6366
WaitLSNProcInfo procInfos[FLEXIBLE_ARRAY_MEMBER];
6467
} WaitLSNState;
6568

0 commit comments

Comments
 (0)