v2-0003-Make-lastImportantAt-64-bit-atomic.patch
application/octet-stream
Filename: v2-0003-Make-lastImportantAt-64-bit-atomic.patch
Type: application/octet-stream
Part: 2
Patch
Format: format-patch
Series: patch v2-0003
Subject: Make lastImportantAt 64-bit atomic
| File | + | − |
|---|---|---|
| src/backend/access/transam/xlog.c | 6 | 9 |
From 5b1380553e69f1f85343441697d7c6af0b6a0727 Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Fri, 2 Dec 2022 09:24:16 +0000
Subject: [PATCH v2] Make lastImportantAt 64-bit atomic
WAL insertion lwlock's lastImportantAt value is currently read
by GetLastImportantRecPtr() with the help of insertion lock
acquisition and release to avoid torn-free reads. Make it a 64-bit
atomic, which inherently provides torn-free reads and writes, to
reduce unnecessary WAL insertion lock acquisitions and releases.
---
src/backend/access/transam/xlog.c | 15 ++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index ec289e0f70..f40e82ba5f 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -377,7 +377,7 @@ typedef struct
{
LWLock lock;
pg_atomic_uint64 insertingAt;
- XLogRecPtr lastImportantAt;
+ pg_atomic_uint64 lastImportantAt;
} WALInsertLock;
/*
@@ -873,7 +873,7 @@ XLogInsertRecord(XLogRecData *rdata,
{
int lockno = holdingAllLocks ? 0 : MyLockNo;
- WALInsertLocks[lockno].l.lastImportantAt = StartPos;
+ pg_atomic_write_u64(&WALInsertLocks[lockno].l.lastImportantAt, StartPos);
}
}
else
@@ -4603,7 +4603,7 @@ XLOGShmemInit(void)
{
LWLockInitialize(&WALInsertLocks[i].l.lock, LWTRANCHE_WAL_INSERT);
pg_atomic_init_u64(&WALInsertLocks[i].l.insertingAt, InvalidXLogRecPtr);
- WALInsertLocks[i].l.lastImportantAt = InvalidXLogRecPtr;
+ pg_atomic_write_u64(&WALInsertLocks[i].l.lastImportantAt, InvalidXLogRecPtr);
}
/*
@@ -6124,13 +6124,10 @@ GetLastImportantRecPtr(void)
XLogRecPtr last_important;
/*
- * Need to take a lock to prevent torn reads of the LSN, which are
- * possible on some of the supported platforms. WAL insert locks only
- * support exclusive mode, so we have to use that.
+ * We atomically read lastImportantAt which prevents torn reads. Hence
+ * no need to take WAL insert lock here.
*/
- LWLockAcquire(&WALInsertLocks[i].l.lock, LW_EXCLUSIVE);
- last_important = WALInsertLocks[i].l.lastImportantAt;
- LWLockRelease(&WALInsertLocks[i].l.lock);
+ last_important = pg_atomic_read_u64(&WALInsertLocks[i].l.lastImportantAt);
if (res < last_important)
res = last_important;
--
2.34.1