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

Commit 963d307

Browse files
Convert unloggedLSN to an atomic variable.
Currently, this variable is an XLogRecPtr protected by a spinlock. By converting it to an atomic variable, we can remove the spinlock, which saves a small amount of shared memory space. Since this code is not performance-critical, we use atomic operations with full barrier semantics to make it easy to reason about correctness. Author: John Morris Reviewed-by: Michael Paquier, Robert Haas, Andres Freund, Stephen Frost, Bharath Rupireddy Discussion: https://postgr.es/m/BYAPR13MB26772534335255E50318C574A0409%40BYAPR13MB2677.namprd13.prod.outlook.com Discussion: https://postgr.es/m/MN2PR13MB2688FD8B757316CB5C54C8A2A0DDA%40MN2PR13MB2688.namprd13.prod.outlook.com
1 parent 3179701 commit 963d307

File tree

1 file changed

+9
-17
lines changed
  • src/backend/access/transam

1 file changed

+9
-17
lines changed

src/backend/access/transam/xlog.c

+9-17
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,8 @@ typedef struct XLogCtlData
470470

471471
XLogSegNo lastRemovedSegNo; /* latest removed/recycled XLOG segment */
472472

473-
/* Fake LSN counter, for unlogged relations. Protected by ulsn_lck. */
474-
XLogRecPtr unloggedLSN;
475-
slock_t ulsn_lck;
473+
/* Fake LSN counter, for unlogged relations. */
474+
pg_atomic_uint64 unloggedLSN;
476475

477476
/* Time and LSN of last xlog segment switch. Protected by WALWriteLock. */
478477
pg_time_t lastSegSwitchTime;
@@ -4498,14 +4497,7 @@ DataChecksumsEnabled(void)
44984497
XLogRecPtr
44994498
GetFakeLSNForUnloggedRel(void)
45004499
{
4501-
XLogRecPtr nextUnloggedLSN;
4502-
4503-
/* increment the unloggedLSN counter, need SpinLock */
4504-
SpinLockAcquire(&XLogCtl->ulsn_lck);
4505-
nextUnloggedLSN = XLogCtl->unloggedLSN++;
4506-
SpinLockRelease(&XLogCtl->ulsn_lck);
4507-
4508-
return nextUnloggedLSN;
4500+
return pg_atomic_fetch_add_u64(&XLogCtl->unloggedLSN, 1);
45094501
}
45104502

45114503
/*
@@ -4921,7 +4913,7 @@ XLOGShmemInit(void)
49214913

49224914
SpinLockInit(&XLogCtl->Insert.insertpos_lck);
49234915
SpinLockInit(&XLogCtl->info_lck);
4924-
SpinLockInit(&XLogCtl->ulsn_lck);
4916+
pg_atomic_init_u64(&XLogCtl->unloggedLSN, InvalidXLogRecPtr);
49254917
}
49264918

49274919
/*
@@ -5526,9 +5518,11 @@ StartupXLOG(void)
55265518
* the unlogged LSN counter can be reset too.
55275519
*/
55285520
if (ControlFile->state == DB_SHUTDOWNED)
5529-
XLogCtl->unloggedLSN = ControlFile->unloggedLSN;
5521+
pg_atomic_write_membarrier_u64(&XLogCtl->unloggedLSN,
5522+
ControlFile->unloggedLSN);
55305523
else
5531-
XLogCtl->unloggedLSN = FirstNormalUnloggedLSN;
5524+
pg_atomic_write_membarrier_u64(&XLogCtl->unloggedLSN,
5525+
FirstNormalUnloggedLSN);
55325526

55335527
/*
55345528
* Copy any missing timeline history files between 'now' and the recovery
@@ -7110,9 +7104,7 @@ CreateCheckPoint(int flags)
71107104
* unused on non-shutdown checkpoints, but seems useful to store it always
71117105
* for debugging purposes.
71127106
*/
7113-
SpinLockAcquire(&XLogCtl->ulsn_lck);
7114-
ControlFile->unloggedLSN = XLogCtl->unloggedLSN;
7115-
SpinLockRelease(&XLogCtl->ulsn_lck);
7107+
ControlFile->unloggedLSN = pg_atomic_read_membarrier_u64(&XLogCtl->unloggedLSN);
71167108

71177109
UpdateControlFile();
71187110
LWLockRelease(ControlFileLock);

0 commit comments

Comments
 (0)