v4-0001-Use-barrier-semantics-when-reading-writing-writte.patch
application/octet-stream
Filename: v4-0001-Use-barrier-semantics-when-reading-writing-writte.patch
Type: application/octet-stream
Part: 0
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v4-0001
Subject: Use barrier semantics when reading/writing writtenUpto
| File | + | − |
|---|---|---|
| src/backend/replication/walreceiver.c | 1 | 1 |
| src/backend/replication/walreceiverfuncs.c | 7 | 3 |
From 3bd688ce54aed58840e1602068f88c0b2b58db69 Mon Sep 17 00:00:00 2001
From: alterego655 <824662526@qq.com>
Date: Fri, 10 Apr 2026 10:45:52 +0800
Subject: [PATCH v4 1/5] Use barrier semantics when reading/writing writtenUpto
The walreceiver publishes its write position lock-free via writtenUpto.
On weakly-ordered architectures (ARM, PowerPC), both sides of this
handshake need explicit barriers so that the lock-less reader sees a
consistent state.
Use pg_atomic_write_membarrier_u64() at both write sites and
pg_atomic_read_membarrier_u64() in GetWalRcvWriteRecPtr(). This matches
the barrier semantics that GetWalRcvFlushRecPtr() and other LSN-position
functions get implicitly from their spinlock acquire/release, and
protects from bugs caused by expectations of similar barrier guarantees
from different LSN-position functions.
Reported-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/zqbppucpmkeqecfy4s5kscnru4tbk6khp3ozqz6ad2zijz354k%40w4bdf4z3wqoz
---
src/backend/replication/walreceiver.c | 2 +-
src/backend/replication/walreceiverfuncs.c | 10 +++++++---
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index 09fde92bfd7..b4196e8a933 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -975,7 +975,7 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr, TimeLineID tli)
}
/* Update shared-memory status */
- pg_atomic_write_u64(&WalRcv->writtenUpto, LogstreamResult.Write);
+ pg_atomic_write_membarrier_u64(&WalRcv->writtenUpto, LogstreamResult.Write);
/*
* If we wrote an LSN that someone was waiting for, notify the waiters.
diff --git a/src/backend/replication/walreceiverfuncs.c b/src/backend/replication/walreceiverfuncs.c
index bd5d47be964..9447913c0e7 100644
--- a/src/backend/replication/walreceiverfuncs.c
+++ b/src/backend/replication/walreceiverfuncs.c
@@ -321,7 +321,8 @@ RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr, const char *conninfo,
walrcv->flushedUpto = recptr;
walrcv->receivedTLI = tli;
walrcv->latestChunkStart = recptr;
- pg_atomic_write_u64(&walrcv->writtenUpto, recptr);
+ /* Pairs with pg_atomic_read_membarrier_u64() in GetWalRcvWriteRecPtr(). */
+ pg_atomic_write_membarrier_u64(&walrcv->writtenUpto, recptr);
}
walrcv->receiveStart = recptr;
walrcv->receiveStartTLI = tli;
@@ -363,14 +364,17 @@ GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
/*
* Returns the last+1 byte position that walreceiver has written.
- * This returns a recently written value without taking a lock.
+ *
+ * Use pg_atomic_read_membarrier_u64() to ensure that callers see up-to-date
+ * shared memory state, matching the barrier semantics provided by the
+ * spinlock in GetWalRcvFlushRecPtr() and other LSN-position functions.
*/
XLogRecPtr
GetWalRcvWriteRecPtr(void)
{
WalRcvData *walrcv = WalRcv;
- return pg_atomic_read_u64(&walrcv->writtenUpto);
+ return pg_atomic_read_membarrier_u64(&walrcv->writtenUpto);
}
/*
--
2.51.0