From 0e5b4d1b9311a628a70218d89abf12308c9d782f Mon Sep 17 00:00:00 2001 From: Alexander Korotkov Date: Thu, 9 Apr 2026 16:49:04 +0300 Subject: [PATCH v3 1/5] Add a memory barrier to GetWalRcvWriteRecPtr() Add pg_memory_barrier() before reading writtenUpto so that callers see up-to-date shared memory state. This matches the barrier semantics that GetWalRcvFlushRecPtr() and other LSN-position functions get implicitly from their spinlock acquire/release, and in turn protects from bugs caused by expectations of similar barrier guarantees from different LSN-position functions. Reported-by: Andres Freund Discussion: https://postgr.es/m/zqbppucpmkeqecfy4s5kscnru4tbk6khp3ozqz6ad2zijz354k%40w4bdf4z3wqoz --- src/backend/replication/walreceiverfuncs.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/backend/replication/walreceiverfuncs.c b/src/backend/replication/walreceiverfuncs.c index bd5d47be964..0408ddff43e 100644 --- a/src/backend/replication/walreceiverfuncs.c +++ b/src/backend/replication/walreceiverfuncs.c @@ -363,14 +363,22 @@ 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 a memory barrier 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; + XLogRecPtr recptr; + + pg_memory_barrier(); - return pg_atomic_read_u64(&walrcv->writtenUpto); + recptr = pg_atomic_read_u64(&walrcv->writtenUpto); + + return recptr; } /* -- 2.39.5 (Apple Git-154)