From 06aa3c327d7ba21a70d416a1bf7a9bc955f629c6 Mon Sep 17 00:00:00 2001
From: Matthias van de Meent <boekewurm+postgres@gmail.com>
Date: Sun, 5 Apr 2026 21:48:23 +0200
Subject: [PATCH vnocfbot.v11 2/2] Const-qualify SlruShared's fields and
 internal pointers

These fields are expected to never update, so by const-qualifying
them we protect against inadvertent modification and we allow the
compiler to apply a few more optimizations it previously may not
have been able to prove.
---
 src/backend/access/transam/slru.c | 82 ++++++++++++++++++++-----------
 src/include/access/slru.h         | 24 ++++-----
 2 files changed, 66 insertions(+), 40 deletions(-)

diff --git a/src/backend/access/transam/slru.c b/src/backend/access/transam/slru.c
index a1e688dd702..cb2bdb35cf4 100644
--- a/src/backend/access/transam/slru.c
+++ b/src/backend/access/transam/slru.c
@@ -278,6 +278,15 @@ shmem_slru_init(void *location, ShmemStructOpts *base_options)
 	int			nlsns = options->nlsns;
 	char	   *ptr;
 	Size		offset;
+	char	  **page_buffer;
+	SlruPageStatus *page_status;
+	bool	   *page_dirty;
+	int64	   *page_number;
+	int		   *page_lru_count;
+	LWLockPadded *buffer_locks;
+	LWLockPadded *bank_locks;
+	int		   *bank_cur_lru_count;
+	XLogRecPtr *group_lsn;
 
 	shared = (SlruShared) location;
 	desc->shared = shared;
@@ -300,62 +309,79 @@ shmem_slru_init(void *location, ShmemStructOpts *base_options)
 
 	memset(shared, 0, sizeof(SlruSharedData));
 
-	shared->num_slots = nslots;
-	shared->lsn_groups_per_page = nlsns;
-
-	pg_atomic_init_u64(&shared->latest_page_number, 0);
-
-	shared->slru_stats_idx = pgstat_get_slru_index(desc->options.name);
-
 	ptr = (char *) shared;
 	offset = MAXALIGN(sizeof(SlruSharedData));
-	shared->page_buffer = (char **) (ptr + offset);
+	page_buffer = (char **) (ptr + offset);
 	offset += MAXALIGN(nslots * sizeof(char *));
-	shared->page_status = (SlruPageStatus *) (ptr + offset);
+	page_status = (SlruPageStatus *) (ptr + offset);
 	offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
-	shared->page_dirty = (bool *) (ptr + offset);
+	page_dirty = (bool *) (ptr + offset);
 	offset += MAXALIGN(nslots * sizeof(bool));
-	shared->page_number = (int64 *) (ptr + offset);
+	page_number = (int64 *) (ptr + offset);
 	offset += MAXALIGN(nslots * sizeof(int64));
-	shared->page_lru_count = (int *) (ptr + offset);
+	page_lru_count = (int *) (ptr + offset);
 	offset += MAXALIGN(nslots * sizeof(int));
 
 	/* Initialize LWLocks */
-	shared->buffer_locks = (LWLockPadded *) (ptr + offset);
+	buffer_locks = (LWLockPadded *) (ptr + offset);
 	offset += MAXALIGN(nslots * sizeof(LWLockPadded));
-	shared->bank_locks = (LWLockPadded *) (ptr + offset);
+	bank_locks = (LWLockPadded *) (ptr + offset);
 	offset += MAXALIGN(nbanks * sizeof(LWLockPadded));
-	shared->bank_cur_lru_count = (int *) (ptr + offset);
+	bank_cur_lru_count = (int *) (ptr + offset);
 	offset += MAXALIGN(nbanks * sizeof(int));
 
 	if (nlsns > 0)
 	{
-		shared->group_lsn = (XLogRecPtr *) (ptr + offset);
+		group_lsn = (XLogRecPtr *) (ptr + offset);
 		offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
 	}
+	else
+		group_lsn = NULL;
+
+	/* Initialize the slot banks. */
+	for (int bankno = 0; bankno < nbanks; bankno++)
+	{
+		LWLockInitialize(&bank_locks[bankno].lock, desc->options.bank_tranche_id);
+		bank_cur_lru_count[bankno] = 0;
+	}
 
 	ptr += BUFFERALIGN(offset);
 	for (int slotno = 0; slotno < nslots; slotno++)
 	{
-		LWLockInitialize(&shared->buffer_locks[slotno].lock,
+		LWLockInitialize(&buffer_locks[slotno].lock,
 						 desc->options.buffer_tranche_id);
 
-		shared->page_buffer[slotno] = ptr;
-		shared->page_status[slotno] = SLRU_PAGE_EMPTY;
-		shared->page_dirty[slotno] = false;
-		shared->page_lru_count[slotno] = 0;
+		page_buffer[slotno] = ptr;
+		page_status[slotno] = SLRU_PAGE_EMPTY;
+		page_dirty[slotno] = false;
+		page_lru_count[slotno] = 0;
 		ptr += BLCKSZ;
 	}
 
-	/* Initialize the slot banks. */
-	for (int bankno = 0; bankno < nbanks; bankno++)
-	{
-		LWLockInitialize(&shared->bank_locks[bankno].lock, desc->options.bank_tranche_id);
-		shared->bank_cur_lru_count[bankno] = 0;
-	}
-
 	/* Should fit to estimated shmem size */
 	Assert(ptr - (char *) shared <= SimpleLruShmemSize(nslots, nlsns));
+
+
+	{
+		SlruSharedData template = {
+			.num_slots = nslots,
+			.lsn_groups_per_page = nlsns,
+			.page_buffer = page_buffer,
+			.page_status = page_status,
+			.page_dirty = page_dirty,
+			.page_number = page_number,
+			.page_lru_count = page_lru_count,
+			.buffer_locks = buffer_locks,
+			.bank_locks = bank_locks,
+			.bank_cur_lru_count = bank_cur_lru_count,
+			.group_lsn = group_lsn,
+			.slru_stats_idx = pgstat_get_slru_index(desc->options.name),
+		};
+
+		pg_atomic_init_u64(&template.latest_page_number, 0);
+
+		memcpy(shared, &template, sizeof(SlruSharedData));
+	}
 }
 
 void
diff --git a/src/include/access/slru.h b/src/include/access/slru.h
index 74ea84deec5..b6854af3119 100644
--- a/src/include/access/slru.h
+++ b/src/include/access/slru.h
@@ -48,23 +48,23 @@ typedef enum
 typedef struct SlruSharedData
 {
 	/* Number of buffers managed by this SLRU structure */
-	int			num_slots;
+	const int	num_slots;
 
 	/*
 	 * Arrays holding info for each buffer slot.  Page number is undefined
 	 * when status is EMPTY, as is page_lru_count.
 	 */
-	char	  **page_buffer;
-	SlruPageStatus *page_status;
-	bool	   *page_dirty;
-	int64	   *page_number;
-	int		   *page_lru_count;
+	char	   *const *const page_buffer;
+	SlruPageStatus *const page_status;
+	bool	   *const page_dirty;
+	int64	   *const page_number;
+	int		   *const page_lru_count;
 
 	/* The buffer_locks protects the I/O on each buffer slots */
-	LWLockPadded *buffer_locks;
+	LWLockPadded *const buffer_locks;
 
 	/* Locks to protect the in memory buffer slot access in SLRU bank. */
-	LWLockPadded *bank_locks;
+	LWLockPadded *const bank_locks;
 
 	/*----------
 	 * A bank-wise LRU counter is maintained because we do a victim buffer
@@ -81,7 +81,7 @@ typedef struct SlruSharedData
 	 * works as long as no page's age exceeds INT_MAX counts.
 	 *----------
 	 */
-	int		   *bank_cur_lru_count;
+	int		   *const bank_cur_lru_count;
 
 	/*
 	 * Optional array of WAL flush LSNs associated with entries in the SLRU
@@ -91,8 +91,8 @@ typedef struct SlruSharedData
 	 * highest LSN known for a contiguous group of SLRU entries on that slot's
 	 * page.
 	 */
-	XLogRecPtr *group_lsn;
-	int			lsn_groups_per_page;
+	XLogRecPtr *const group_lsn;
+	const int	lsn_groups_per_page;
 
 	/*
 	 * latest_page_number is the page number of the current end of the log;
@@ -102,7 +102,7 @@ typedef struct SlruSharedData
 	pg_atomic_uint64 latest_page_number;
 
 	/* SLRU's index for statistics purposes (might not be unique) */
-	int			slru_stats_idx;
+	const int	slru_stats_idx;
 } SlruSharedData;
 
 typedef SlruSharedData *SlruShared;
-- 
2.50.1 (Apple Git-155)

