@@ -58,6 +58,9 @@ shm_mq *recv_mq = NULL;
5858shm_mq_handle * recv_mqh = NULL ;
5959LOCKTAG queueTag ;
6060
61+ #if PG_VERSION_NUM >=150000
62+ static shmem_request_hook_type prev_shmem_request_hook = NULL ;
63+ #endif
6164static shmem_startup_hook_type prev_shmem_startup_hook = NULL ;
6265static PGPROC * search_proc (int backendPid );
6366static PlannedStmt * pgws_planner_hook (Query * parse ,
@@ -73,28 +76,40 @@ static void pgws_ExecutorEnd(QueryDesc *queryDesc);
7376 * The value has to be in sync with ProcGlobal->allProcCount, initialized in
7477 * InitProcGlobal() (proc.c).
7578 *
76- * We calculate the value here as it won't initialized when we need it during
77- * _PG_init().
78- *
79- * Note that the value returned during _PG_init() might be different from the
80- * value returned later if some third-party modules change one of the
81- * underlying GUC. This isn't ideal but can't lead to a crash, as the value
82- * returned during _PG_init() is only used to ask for additional shmem with
83- * RequestAddinShmemSpace(), and postgres has an extra 100kB of shmem to
84- * compensate some small unaccounted usage. So if the value later changes, we
85- * will allocate and initialize the new (and correct) memory size, which
86- * will either work thanks for the extra 100kB of shmem, of fail (and prevent
87- * postgres startup) due to an out of shared memory error.
8879 */
8980static int
9081get_max_procs_count (void )
9182{
9283int count = 0 ;
9384
85+ /* First, add the maximum number of backends (MaxBackends). */
86+ #if PG_VERSION_NUM >=150000
87+ /*
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
9494/*
95- * MaxBackends: bgworkers, autovacuum workers and launcher.
95+ * On older versions, we need to compute MaxBackends: bgworkers, autovacuum
96+ * workers and launcher.
9697 * This has to be in sync with the value computed in
9798 * 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.
98113 */
99114count += MaxConnections + autovacuum_max_workers + 1
100115+ max_worker_processes ;
@@ -105,9 +120,11 @@ get_max_procs_count(void)
105120 */
106121#if PG_VERSION_NUM >=120000
107122count += max_wal_senders ;
108- #endif
123+ #endif /* pg 12+ */
124+ #endif /* pg 15- */
125+ /* End of MaxBackends calculation. */
109126
110- /* AuxiliaryProcs */
127+ /*Add AuxiliaryProcs */
111128count += NUM_AUXILIARY_PROCS ;
112129
113130return count ;
@@ -265,6 +282,23 @@ setup_gucs()
265282}
266283}
267284
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+
268302/*
269303 * Distribute shared memory.
270304 */
@@ -344,20 +378,27 @@ _PG_init(void)
344378if (!process_shared_preload_libraries_in_progress )
345379return ;
346380
381+ #if PG_VERSION_NUM < 150000
347382/*
348383 * Request additional shared resources. (These are no-ops if we're not in
349384 * the postmaster process.) We'll allocate or attach to the shared
350385 * 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.
351389 */
352390RequestAddinShmemSpace (pgws_shmem_size ());
391+ #endif
353392
354393register_wait_collector ();
355394
356395/*
357396 * Install hooks.
358397 */
359- prev_shmem_startup_hook = shmem_startup_hook ;
360- shmem_startup_hook = pgws_shmem_startup ;
398+ #if PG_VERSION_NUM >=150000
399+ prev_shmem_request_hook = shmem_request_hook ;
400+ shmem_request_hook = pgws_shmem_request ;
401+ #endif
361402prev_shmem_startup_hook = shmem_startup_hook ;
362403shmem_startup_hook = pgws_shmem_startup ;
363404planner_hook_next = planner_hook ;