agora inbox for pgsql-hackers@postgresql.org  
help / color / mirror / Atom feed
From: Nathan Bossart <nathan@postgresql.org>
Subject: [PATCH v3 4/6] Convert PROC_HDR->startupBufferPinWaitBuf to an atomic variable.
Date: Tue, 8 Sep 2026 12:40:06 -0500

Currently, this variable is a Buffer that is accessed via a volatile
pointer.  By converting it to an atomic variable, we can remove the
volatile qualifiers.  No barriers are needed; as the comment there
notes, the value is published before the backends that read it are
signaled.
---
 src/backend/storage/lmgr/proc.c | 12 +++---------
 src/include/storage/proc.h      |  2 +-
 2 files changed, 4 insertions(+), 10 deletions(-)

diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index ab65a6dbcc9..91fe2766640 100644
--- a/src/backend/storage/lmgr/proc.c
+++ b/src/backend/storage/lmgr/proc.c
@@ -238,7 +238,7 @@ ProcGlobalShmemInit(void *arg)
 	dlist_init(&ProcGlobal->autovacFreeProcs);
 	dlist_init(&ProcGlobal->bgworkerFreeProcs);
 	dlist_init(&ProcGlobal->walsenderFreeProcs);
-	ProcGlobal->startupBufferPinWaitBuf = InvalidBuffer;
+	pg_atomic_init_u32(&ProcGlobal->startupBufferPinWaitBuf, InvalidBuffer);
 	pg_atomic_init_u32(&ProcGlobal->avLauncherProc, INVALID_PROC_NUMBER);
 	pg_atomic_init_u32(&ProcGlobal->walwriterProc, INVALID_PROC_NUMBER);
 	pg_atomic_init_u32(&ProcGlobal->checkpointerProc, INVALID_PROC_NUMBER);
@@ -767,10 +767,7 @@ InitAuxiliaryProcess(void)
 void
 SetStartupBufferPinWaitBuf(Buffer buffer)
 {
-	/* use volatile pointer to prevent code rearrangement */
-	volatile PROC_HDR *procglobal = ProcGlobal;
-
-	procglobal->startupBufferPinWaitBuf = buffer;
+	pg_atomic_write_u32(&ProcGlobal->startupBufferPinWaitBuf, buffer);
 }
 
 /*
@@ -779,10 +776,7 @@ SetStartupBufferPinWaitBuf(Buffer buffer)
 Buffer
 GetStartupBufferPinWaitBuf(void)
 {
-	/* use volatile pointer to prevent code rearrangement */
-	volatile PROC_HDR *procglobal = ProcGlobal;
-
-	return procglobal->startupBufferPinWaitBuf;
+	return pg_atomic_read_u32(&ProcGlobal->startupBufferPinWaitBuf);
 }
 
 /*
diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h
index 4c3f431b4eb..abe40001d9a 100644
--- a/src/include/storage/proc.h
+++ b/src/include/storage/proc.h
@@ -500,7 +500,7 @@ typedef struct PROC_HDR
 	/* Current shared estimate of appropriate spins_per_delay value */
 	int			spins_per_delay;
 	/* Buffer that Startup process waits for pin on, or InvalidBuffer */
-	Buffer		startupBufferPinWaitBuf;
+	pg_atomic_uint32 startupBufferPinWaitBuf;
 } PROC_HDR;
 
 extern PGDLLIMPORT PROC_HDR *ProcGlobal;
-- 
2.55.0


--hwKv7DgSDwjNaYBK
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename=v3-0005-Convert-Sharedsort-s-worker-counters-to-atomic-va.patch



view thread (1519+ messages)  latest in thread

Message-ID: <no-message-id-1724670@localhost>
Permalink:  ../no-message-id-1724670@localhost/
Also on:    postgresql.org/message-id/no-message-id-1724670@localhost

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: nathan@postgresql.org
  Subject: Re: [PATCH v3 4/6] Convert PROC_HDR->startupBufferPinWaitBuf to an atomic variable.
  In-Reply-To: <no-message-id-1724670@localhost>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox