From 383f8bb806c5925c21832be2abead6b136ebcf2f Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Thu, 8 Oct 2026 13:00:43 +0300
Subject: [PATCH v2 2/2] 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.
---
 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 5c82865a084..f81c7732e68 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 e4ff5619b79..726ab75dfe9 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;
-- 
2.47.3

