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

MAX_BACKENDS influences many things besides postmaster. I e.g. 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.

While at that, add MAX_BACKENDS_BITS, as that's useful in various places for
static assertions (to be added in subsequent commits).

Discussion: https://postgr.es/m/wptizm4qt6yikgm2pt52xzyv6ycmqiutloyvypvmagn7xvqkce@d4xuv3mylpg4
---
 src/include/postmaster/postmaster.h | 12 ------------
 src/include/storage/procnumber.h    | 13 +++++++++++++
 src/backend/storage/lmgr/lwlock.c   |  2 +-
 src/backend/utils/adt/xid8funcs.c   |  2 +-
 src/backend/utils/init/postinit.c   |  1 +
 src/backend/utils/misc/guc_tables.c |  1 +
 6 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/src/include/postmaster/postmaster.h b/src/include/postmaster/postmaster.h
index d8a9f14b3b8..b6a3f275a1b 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/include/storage/procnumber.h b/src/include/storage/procnumber.h
index 7cf981ab673..fdbecc55dbe 100644
--- a/src/include/storage/procnumber.h
+++ b/src/include/storage/procnumber.h
@@ -25,6 +25,19 @@ typedef int ProcNumber;
 
 #define INVALID_PROC_NUMBER		(-1)
 
+/*
+ * Note: MAX_BACKENDS_BITS is 18 as that is the space available for buffer
+ * refcounts 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_BITS		18
+#define MAX_BACKENDS			((1 << MAX_BACKENDS_BITS)-1)
+
 /*
  * Proc number of this backend (same as GetNumberFromPGProc(MyProc))
  */
diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index f1e74f184f1..d08144e9c22 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -80,9 +80,9 @@
 #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/procnumber.h"
 #include "storage/spin.h"
 #include "utils/memutils.h"
 
diff --git a/src/backend/utils/adt/xid8funcs.c b/src/backend/utils/adt/xid8funcs.c
index 20b28b2528c..88d798fbf4b 100644
--- a/src/backend/utils/adt/xid8funcs.c
+++ b/src/backend/utils/adt/xid8funcs.c
@@ -32,9 +32,9 @@
 #include "lib/qunique.h"
 #include "libpq/pqformat.h"
 #include "miscadmin.h"
-#include "postmaster/postmaster.h"
 #include "storage/lwlock.h"
 #include "storage/procarray.h"
+#include "storage/procnumber.h"
 #include "utils/builtins.h"
 #include "utils/memutils.h"
 #include "utils/snapmgr.h"
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 01bb6a410cb..318600d6d02 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -49,6 +49,7 @@
 #include "storage/lmgr.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
+#include "storage/procnumber.h"
 #include "storage/procsignal.h"
 #include "storage/sinvaladt.h"
 #include "storage/smgr.h"
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index 03a6dd49154..ad25cbb39c5 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -77,6 +77,7 @@
 #include "storage/large_object.h"
 #include "storage/pg_shmem.h"
 #include "storage/predicate.h"
+#include "storage/procnumber.h"
 #include "storage/standby.h"
 #include "tcop/backend_startup.h"
 #include "tcop/tcopprot.h"
-- 
2.48.1.76.g4e746b1a31.dirty

