postgres.git / summary / log / commit / refs
commit 8ca9cfb77838b3a17b7c9b5ef26cb2a26b5e1bb2
Author: Nathan Bossart <nathan@postgresql.org>
Date: Thu Sep 03 20:16:16 2026 +0000
Convert PROC_HDR->startupBufferPinWaitBufId to a Buffer.
Presently, this field stores the buffer's index, i.e., the Buffer
minus one, and it uses -1 to mean "not waiting." The callers
subtract one before storing the value and add it back after reading
it. This commit changes the field to store the Buffer itself and
to use InvalidBuffer (0) as the sentinel, which gets rid of that
arithmetic.
This is preparatory work for a follow-up commit that will convert
the variable to an unsigned atomic, for which a negative sentinel
makes little sense.
Reviewed-by: Peter Eisentraut <peter@eisentraut.org>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com>
Discussion: https://postgr.es/m/alAJeRRzehDjLaF1%40nathan
src/backend/storage/buffer/bufmgr.c | 16 ++++++++--------
src/backend/storage/lmgr/proc.c | 20 ++++++++++----------
src/include/storage/proc.h | 9 +++++----
3 files changed, 23 insertions(+), 22 deletions(-)
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 17f142e4c5b..9bf6b88f22a 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -6806,12 +6806,12 @@ LockBufferForCleanup(Buffer buffer)
if (log_recovery_conflict_waits && waitStart == 0)
waitStart = GetCurrentTimestamp();
- /* Publish the bufid that Startup process waits on */
- SetStartupBufferPinWaitBufId(buffer - 1);
+ /* Publish the buffer that Startup process waits on */
+ SetStartupBufferPinWaitBuf(buffer);
/* Set alarm and then wait to be signaled by UnpinBuffer() */
ResolveRecoveryConflictWithBufferPin();
- /* Reset the published bufid */
- SetStartupBufferPinWaitBufId(-1);
+ /* Reset the published buffer */
+ SetStartupBufferPinWaitBuf(InvalidBuffer);
}
else
ProcWaitForSignal(WAIT_EVENT_BUFFER_CLEANUP);
@@ -6865,18 +6865,18 @@ cleanup_lock_acquired:
bool
HoldingBufferPinThatDelaysRecovery(void)
{
- int bufid = GetStartupBufferPinWaitBufId();
+ Buffer buffer = GetStartupBufferPinWaitBuf();
/*
* If we get woken slowly then it's possible that the Startup process was
* already woken by other backends before we got here. Also possible that
* we get here by multiple interrupts or interrupts at inappropriate
- * times, so make sure we do nothing if the bufid is not set.
+ * times, so make sure we do nothing if the buffer is not set.
*/
- if (bufid < 0)
+ if (buffer == InvalidBuffer)
return false;
- if (GetPrivateRefCount(bufid + 1) > 0)
+ if (GetPrivateRefCount(buffer) > 0)
return true;
return false;
diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index 624cecc6bc5..ab65a6dbcc9 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->startupBufferPinWaitBufId = -1;
+ 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);
@@ -759,30 +759,30 @@ InitAuxiliaryProcess(void)
/*
* Used from bufmgr to share the value of the buffer that Startup waits on,
- * or to reset the value to "not waiting" (-1). This allows processing
- * of recovery conflicts for buffer pins. Set is made before backends look
- * at this value, so locking not required, especially since the set is
- * an atomic integer set operation.
+ * or to reset the value to "not waiting" (InvalidBuffer). This allows
+ * processing of recovery conflicts for buffer pins. Set is made before
+ * backends look at this value, so locking not required, especially since
+ * the set is an atomic integer set operation.
*/
void
-SetStartupBufferPinWaitBufId(int bufid)
+SetStartupBufferPinWaitBuf(Buffer buffer)
{
/* use volatile pointer to prevent code rearrangement */
volatile PROC_HDR *procglobal = ProcGlobal;
- procglobal->startupBufferPinWaitBufId = bufid;
+ procglobal->startupBufferPinWaitBuf = buffer;
}
/*
* Used by backends when they receive a request to check for buffer pin waits.
*/
-int
-GetStartupBufferPinWaitBufId(void)
+Buffer
+GetStartupBufferPinWaitBuf(void)
{
/* use volatile pointer to prevent code rearrangement */
volatile PROC_HDR *procglobal = ProcGlobal;
- return procglobal->startupBufferPinWaitBufId;
+ return procglobal->startupBufferPinWaitBuf;
}
/*
diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h
index 03a1a466fa8..4c3f431b4eb 100644
--- a/src/include/storage/proc.h
+++ b/src/include/storage/proc.h
@@ -17,6 +17,7 @@
#include "access/xlogdefs.h"
#include "lib/ilist.h"
#include "miscadmin.h"
+#include "storage/buf.h"
#include "storage/latch.h"
#include "storage/lock.h"
#include "storage/pg_sema.h"
@@ -498,8 +499,8 @@ typedef struct PROC_HDR
/* Current shared estimate of appropriate spins_per_delay value */
int spins_per_delay;
- /* Buffer id of the buffer that Startup process waits for pin on, or -1 */
- int startupBufferPinWaitBufId;
+ /* Buffer that Startup process waits for pin on, or InvalidBuffer */
+ Buffer startupBufferPinWaitBuf;
} PROC_HDR;
extern PGDLLIMPORT PROC_HDR *ProcGlobal;
@@ -558,8 +559,8 @@ extern void InitProcess(void);
extern void InitProcessPhase2(void);
extern void InitAuxiliaryProcess(void);
-extern void SetStartupBufferPinWaitBufId(int bufid);
-extern int GetStartupBufferPinWaitBufId(void);
+extern void SetStartupBufferPinWaitBuf(Buffer buffer);
+extern Buffer GetStartupBufferPinWaitBuf(void);
extern bool HaveNFreeProcs(int n, int *nfree);
extern void ProcReleaseLocks(bool isCommit);