From 1feb619383d05395936c6c52144500c7becb409e Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Sat, 10 Oct 2026 01:01:02 +0300 Subject: [PATCH] refactor: Move BackendWritebackContext initialization BackendWritebackContext is a purely per-process thing, so it feels wrong to initialize it in the shmem init/attach functions. Move the initialization to InitBufferManagerAccess() instead. Backpatch to version 19, where the new shared memory callbacks system was introduced. While this isn't new with that, it made the ShmemInit functions more closely related to shared memory initialization than before. Reviewed-by: Bharath Rupireddy Reviewed-by: Ashutosh Bapat Discussion: https://www.postgresql.org/message-id/fb1bd612-4a58-4235-976f-2b2ef036bfff@iki.fi Backpatch-through: 19 --- src/backend/storage/buffer/buf_init.c | 15 --------------- src/backend/storage/buffer/bufmgr.c | 6 ++++++ src/include/storage/buf_internals.h | 1 - 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/backend/storage/buffer/buf_init.c b/src/backend/storage/buffer/buf_init.c index 1407c930c56..9c5f2449cf3 100644 --- a/src/backend/storage/buffer/buf_init.c +++ b/src/backend/storage/buffer/buf_init.c @@ -24,17 +24,14 @@ BufferDescPadded *BufferDescriptors; char *BufferBlocks; ConditionVariableMinimallyPadded *BufferIOCVArray; -WritebackContext BackendWritebackContext; CkptSortItem *CkptBufferIds; static void BufferManagerShmemRequest(void *arg); static void BufferManagerShmemInit(void *arg); -static void BufferManagerShmemAttach(void *arg); const ShmemCallbacks BufferManagerShmemCallbacks = { .request_fn = BufferManagerShmemRequest, .init_fn = BufferManagerShmemInit, - .attach_fn = BufferManagerShmemAttach, }; /* @@ -138,16 +135,4 @@ BufferManagerShmemInit(void *arg) proclist_init(&buf->lock_waiters); ConditionVariableInit(BufferDescriptorGetIOCV(buf)); } - - /* Initialize per-backend file flush context */ - WritebackContextInit(&BackendWritebackContext, - &backend_flush_after); -} - -static void -BufferManagerShmemAttach(void *arg) -{ - /* Initialize per-backend file flush context */ - WritebackContextInit(&BackendWritebackContext, - &backend_flush_after); } diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 169829eb020..791abe4a633 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -270,6 +270,8 @@ static int PrivateRefCountEntryLast = -1; static uint32 MaxProportionalPins; +static WritebackContext BackendWritebackContext; + static void ReservePrivateRefCountEntry(void); static PrivateRefCountEntry *NewPrivateRefCountEntry(Buffer buffer); static PrivateRefCountEntry *GetPrivateRefCountEntry(Buffer buffer, bool do_move); @@ -4252,6 +4254,10 @@ InitBufferManagerAccess(void) PrivateRefCountHash = refcount_create(CurrentMemoryContext, 100, NULL); + /* Initialize per-backend file flush context */ + WritebackContextInit(&BackendWritebackContext, + &backend_flush_after); + /* * AtProcExit_Buffers needs LWLock access, and thereby has to be called at * the corresponding phase of backend shutdown. diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h index a23abf14f8f..fac5205d92c 100644 --- a/src/include/storage/buf_internals.h +++ b/src/include/storage/buf_internals.h @@ -412,7 +412,6 @@ typedef struct WritebackContext /* in buf_init.c */ extern PGDLLIMPORT BufferDescPadded *BufferDescriptors; extern PGDLLIMPORT ConditionVariableMinimallyPadded *BufferIOCVArray; -extern PGDLLIMPORT WritebackContext BackendWritebackContext; /* in localbuf.c */ extern PGDLLIMPORT BufferDesc *LocalBufferDescriptors;