From b4cf37ce1d32bc251d77bfa2976324946354202c Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sun, 26 Jan 2025 14:10:58 -0500
Subject: [PATCH v1 1/3] Move MAX_BACKENDS to pg_config_manual.h

MAX_BACKENDS influences many things besides postmaster. I just noticed that we
don't have static assertions ensuring BUF_REFCOUNT_MASK is big enough for
MAX_BACKENDS, adding them would require including postmaster.h in
buf_internals.h which doesn't seem right.

Discussion: https://postgr.es/m/wptizm4qt6yikgm2pt52xzyv6ycmqiutloyvypvmagn7xvqkce@d4xuv3mylpg4
---
 src/include/pg_config_manual.h      | 12 ++++++++++++
 src/include/postmaster/postmaster.h | 12 ------------
 src/backend/storage/lmgr/lwlock.c   |  1 -
 src/backend/utils/adt/xid8funcs.c   |  1 -
 4 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/src/include/pg_config_manual.h b/src/include/pg_config_manual.h
index 449e50bd78c..9874302946e 100644
--- a/src/include/pg_config_manual.h
+++ b/src/include/pg_config_manual.h
@@ -42,6 +42,18 @@
  */
 #define FUNC_MAX_ARGS		100
 
+/*
+ * Note: MAX_BACKENDS is limited to 2^18-1 because that's the width reserved
+ * for buffer references in buf_internals.h.  This limitation could be lifted
+ * by using a 64bit state; but it's unlikely to be worthwhile as 2^18-1
+ * backends exceed currently realistic configurations. Even if that limitation
+ * were removed, we still could not a) exceed 2^23-1 because inval.c stores
+ * the ProcNumber as a 3-byte signed integer, b) INT_MAX/4 because some places
+ * compute 4*MaxBackends without any overflow check.  This is rechecked in the
+ * relevant GUC check hooks and in RegisterBackgroundWorker().
+ */
+#define MAX_BACKENDS	0x3FFFF
+
 /*
  * When creating a product derived from PostgreSQL with changes that cause
  * incompatibilities for loadable modules, it is recommended to change this
diff --git a/src/include/postmaster/postmaster.h b/src/include/postmaster/postmaster.h
index 188a06e2379..e12af62a370 100644
--- a/src/include/postmaster/postmaster.h
+++ b/src/include/postmaster/postmaster.h
@@ -126,18 +126,6 @@ extern PMChild *AllocDeadEndChild(void);
 extern bool ReleasePostmasterChildSlot(PMChild *pmchild);
 extern PMChild *FindPostmasterChildByPid(int pid);
 
-/*
- * Note: MAX_BACKENDS is limited to 2^18-1 because that's the width reserved
- * for buffer references in buf_internals.h.  This limitation could be lifted
- * by using a 64bit state; but it's unlikely to be worthwhile as 2^18-1
- * backends exceed currently realistic configurations. Even if that limitation
- * were removed, we still could not a) exceed 2^23-1 because inval.c stores
- * the ProcNumber as a 3-byte signed integer, b) INT_MAX/4 because some places
- * compute 4*MaxBackends without any overflow check.  This is rechecked in the
- * relevant GUC check hooks and in RegisterBackgroundWorker().
- */
-#define MAX_BACKENDS	0x3FFFF
-
 /*
  * These values correspond to the special must-be-first options for dispatching
  * to various subprograms.  parse_dispatch_option() can be used to convert an
diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index 2f558ffea14..328b1bab659 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -80,7 +80,6 @@
 #include "pg_trace.h"
 #include "pgstat.h"
 #include "port/pg_bitutils.h"
-#include "postmaster/postmaster.h"
 #include "storage/proc.h"
 #include "storage/proclist.h"
 #include "storage/spin.h"
diff --git a/src/backend/utils/adt/xid8funcs.c b/src/backend/utils/adt/xid8funcs.c
index 20b28b2528c..d505e3fcf57 100644
--- a/src/backend/utils/adt/xid8funcs.c
+++ b/src/backend/utils/adt/xid8funcs.c
@@ -32,7 +32,6 @@
 #include "lib/qunique.h"
 #include "libpq/pqformat.h"
 #include "miscadmin.h"
-#include "postmaster/postmaster.h"
 #include "storage/lwlock.h"
 #include "storage/procarray.h"
 #include "utils/builtins.h"
-- 
2.48.1.76.g4e746b1a31.dirty

