@@ -58,6 +58,9 @@ shm_mq *recv_mq = NULL;
58
58
shm_mq_handle * recv_mqh = NULL ;
59
59
LOCKTAG queueTag ;
60
60
61
+ #if PG_VERSION_NUM >= 150000
62
+ static shmem_request_hook_type prev_shmem_request_hook = NULL ;
63
+ #endif
61
64
static shmem_startup_hook_type prev_shmem_startup_hook = NULL ;
62
65
static PGPROC * search_proc (int backendPid );
63
66
static PlannedStmt * pgws_planner_hook (Query * parse ,
@@ -73,28 +76,40 @@ static void pgws_ExecutorEnd(QueryDesc *queryDesc);
73
76
* The value has to be in sync with ProcGlobal->allProcCount, initialized in
74
77
* InitProcGlobal() (proc.c).
75
78
*
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.
88
79
*/
89
80
static int
90
81
get_max_procs_count (void )
91
82
{
92
83
int count = 0 ;
93
84
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
94
94
/*
95
- * MaxBackends: bgworkers, autovacuum workers and launcher.
95
+ * On older versions, we need to compute MaxBackends: bgworkers, autovacuum
96
+ * workers and launcher.
96
97
* This has to be in sync with the value computed in
97
98
* 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.
98
113
*/
99
114
count += MaxConnections + autovacuum_max_workers + 1
100
115
+ max_worker_processes ;
@@ -105,9 +120,11 @@ get_max_procs_count(void)
105
120
*/
106
121
#if PG_VERSION_NUM >= 120000
107
122
count += max_wal_senders ;
108
- #endif
123
+ #endif /* pg 12+ */
124
+ #endif /* pg 15- */
125
+ /* End of MaxBackends calculation. */
109
126
110
- /* AuxiliaryProcs */
127
+ /* Add AuxiliaryProcs */
111
128
count += NUM_AUXILIARY_PROCS ;
112
129
113
130
return count ;
@@ -265,6 +282,23 @@ setup_gucs()
265
282
}
266
283
}
267
284
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
+
268
302
/*
269
303
* Distribute shared memory.
270
304
*/
@@ -344,20 +378,27 @@ _PG_init(void)
344
378
if (!process_shared_preload_libraries_in_progress )
345
379
return ;
346
380
381
+ #if PG_VERSION_NUM < 150000
347
382
/*
348
383
* Request additional shared resources. (These are no-ops if we're not in
349
384
* the postmaster process.) We'll allocate or attach to the shared
350
385
* 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.
351
389
*/
352
390
RequestAddinShmemSpace (pgws_shmem_size ());
391
+ #endif
353
392
354
393
register_wait_collector ();
355
394
356
395
/*
357
396
* Install hooks.
358
397
*/
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
361
402
prev_shmem_startup_hook = shmem_startup_hook ;
362
403
shmem_startup_hook = pgws_shmem_startup ;
363
404
planner_hook_next = planner_hook ;
0 commit comments