3939PG_MODULE_MAGIC ;
4040
4141void _PG_init (void );
42- void _PG_fini (void );
4342
4443/* Global variables */
4544bool shmem_initialized = false;
@@ -59,6 +58,9 @@ shm_mq *recv_mq = NULL;
5958shm_mq_handle * recv_mqh = NULL ;
6059LOCKTAG queueTag ;
6160
61+ #if PG_VERSION_NUM >=150000
62+ static shmem_request_hook_type prev_shmem_request_hook = NULL ;
63+ #endif
6264static shmem_startup_hook_type prev_shmem_startup_hook = NULL ;
6365static PGPROC * search_proc (int backendPid );
6466static PlannedStmt * pgws_planner_hook (Query * parse ,
@@ -74,28 +76,40 @@ static void pgws_ExecutorEnd(QueryDesc *queryDesc);
7476 * The value has to be in sync with ProcGlobal->allProcCount, initialized in
7577 * InitProcGlobal() (proc.c).
7678 *
77- * We calculate the value here as it won't initialized when we need it during
78- * _PG_init().
79- *
80- * Note that the value returned during _PG_init() might be different from the
81- * value returned later if some third-party modules change one of the
82- * underlying GUC. This isn't ideal but can't lead to a crash, as the value
83- * returned during _PG_init() is only used to ask for additional shmem with
84- * RequestAddinShmemSpace(), and postgres has an extra 100kB of shmem to
85- * compensate some small unaccounted usage. So if the value later changes, we
86- * will allocate and initialize the new (and correct) memory size, which
87- * will either work thanks for the extra 100kB of shmem, of fail (and prevent
88- * postgres startup) due to an out of shared memory error.
8979 */
9080static int
9181get_max_procs_count (void )
9282{
9383int count = 0 ;
9484
85+ /* First, add the maximum number of backends (MaxBackends). */
86+ #if PG_VERSION_NUM >=150000
9587/*
96- * MaxBackends: bgworkers, autovacuum workers and launcher.
88+ * On pg15+, we can directly access the MaxBackends variable, as it will
89+ * have already been initialized in shmem_request_hook.
90+ */
91+ Assert (MaxBackends > 0 );
92+ count += MaxBackends ;
93+ #else
94+ /*
95+ * On older versions, we need to compute MaxBackends: bgworkers, autovacuum
96+ * workers and launcher.
9797 * This has to be in sync with the value computed in
9898 * InitializeMaxBackends() (postinit.c)
99+ *
100+ * Note that we need to calculate the value as it won't initialized when we
101+ * need it during _PG_init().
102+ *
103+ * Note also that the value returned during _PG_init() might be different
104+ * from the value returned later if some third-party modules change one of
105+ * the underlying GUC. This isn't ideal but can't lead to a crash, as the
106+ * value returned during _PG_init() is only used to ask for additional
107+ * shmem with RequestAddinShmemSpace(), and postgres has an extra 100kB of
108+ * shmem to compensate some small unaccounted usage. So if the value later
109+ * changes, we will allocate and initialize the new (and correct) memory
110+ * size, which will either work thanks for the extra 100kB of shmem, of
111+ * fail (and prevent postgres startup) due to an out of shared memory
112+ * error.
99113 */
100114count += MaxConnections + autovacuum_max_workers + 1
101115+ max_worker_processes ;
@@ -106,9 +120,11 @@ get_max_procs_count(void)
106120 */
107121#if PG_VERSION_NUM >=120000
108122count += max_wal_senders ;
109- #endif
123+ #endif /* pg 12+ */
124+ #endif /* pg 15- */
125+ /* End of MaxBackends calculation. */
110126
111- /* AuxiliaryProcs */
127+ /*Add AuxiliaryProcs */
112128count += NUM_AUXILIARY_PROCS ;
113129
114130return count ;
@@ -266,6 +282,23 @@ setup_gucs()
266282}
267283}
268284
285+ #if PG_VERSION_NUM >=150000
286+ /*
287+ * shmem_request hook: request additional shared memory resources.
288+ *
289+ * If you change code here, don't forget to also report the modifications in
290+ * _PG_init() for pg14 and below.
291+ */
292+ static void
293+ pgws_shmem_request (void )
294+ {
295+ if (prev_shmem_request_hook )
296+ prev_shmem_request_hook ();
297+
298+ RequestAddinShmemSpace (pgws_shmem_size ());
299+ }
300+ #endif
301+
269302/*
270303 * Distribute shared memory.
271304 */
@@ -345,18 +378,27 @@ _PG_init(void)
345378if (!process_shared_preload_libraries_in_progress )
346379return ;
347380
381+ #if PG_VERSION_NUM < 150000
348382/*
349383 * Request additional shared resources. (These are no-ops if we're not in
350384 * the postmaster process.) We'll allocate or attach to the shared
351385 * resources in pgws_shmem_startup().
386+ *
387+ * If you change code here, don't forget to also report the modifications
388+ * in pgsp_shmem_request() for pg15 and later.
352389 */
353390RequestAddinShmemSpace (pgws_shmem_size ());
391+ #endif
354392
355393register_wait_collector ();
356394
357395/*
358396 * Install hooks.
359397 */
398+ #if PG_VERSION_NUM >=150000
399+ prev_shmem_request_hook = shmem_request_hook ;
400+ shmem_request_hook = pgws_shmem_request ;
401+ #endif
360402prev_shmem_startup_hook = shmem_startup_hook ;
361403shmem_startup_hook = pgws_shmem_startup ;
362404planner_hook_next = planner_hook ;
@@ -365,16 +407,6 @@ _PG_init(void)
365407ExecutorEnd_hook = pgws_ExecutorEnd ;
366408}
367409
368- /*
369- * Module unload callback
370- */
371- void
372- _PG_fini (void )
373- {
374- /* Uninstall hooks. */
375- shmem_startup_hook = prev_shmem_startup_hook ;
376- }
377-
378410/*
379411 * Find PGPROC entry responsible for given pid assuming ProcArrayLock was
380412 * already taken.