39
39
PG_MODULE_MAGIC ;
40
40
41
41
void _PG_init (void );
42
- void _PG_fini (void );
43
42
44
43
/* Global variables */
45
44
bool shmem_initialized = false;
@@ -59,6 +58,9 @@ shm_mq *recv_mq = NULL;
59
58
shm_mq_handle * recv_mqh = NULL ;
60
59
LOCKTAG queueTag ;
61
60
61
+ #if PG_VERSION_NUM >= 150000
62
+ static shmem_request_hook_type prev_shmem_request_hook = NULL ;
63
+ #endif
62
64
static shmem_startup_hook_type prev_shmem_startup_hook = NULL ;
63
65
static PGPROC * search_proc (int backendPid );
64
66
static PlannedStmt * pgws_planner_hook (Query * parse ,
@@ -74,28 +76,40 @@ static void pgws_ExecutorEnd(QueryDesc *queryDesc);
74
76
* The value has to be in sync with ProcGlobal->allProcCount, initialized in
75
77
* InitProcGlobal() (proc.c).
76
78
*
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.
89
79
*/
90
80
static int
91
81
get_max_procs_count (void )
92
82
{
93
83
int count = 0 ;
94
84
85
+ /* First, add the maximum number of backends (MaxBackends). */
86
+ #if PG_VERSION_NUM >= 150000
95
87
/*
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.
97
97
* This has to be in sync with the value computed in
98
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.
99
113
*/
100
114
count += MaxConnections + autovacuum_max_workers + 1
101
115
+ max_worker_processes ;
@@ -106,9 +120,11 @@ get_max_procs_count(void)
106
120
*/
107
121
#if PG_VERSION_NUM >= 120000
108
122
count += max_wal_senders ;
109
- #endif
123
+ #endif /* pg 12+ */
124
+ #endif /* pg 15- */
125
+ /* End of MaxBackends calculation. */
110
126
111
- /* AuxiliaryProcs */
127
+ /* Add AuxiliaryProcs */
112
128
count += NUM_AUXILIARY_PROCS ;
113
129
114
130
return count ;
@@ -266,6 +282,23 @@ setup_gucs()
266
282
}
267
283
}
268
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
+
269
302
/*
270
303
* Distribute shared memory.
271
304
*/
@@ -345,18 +378,27 @@ _PG_init(void)
345
378
if (!process_shared_preload_libraries_in_progress )
346
379
return ;
347
380
381
+ #if PG_VERSION_NUM < 150000
348
382
/*
349
383
* Request additional shared resources. (These are no-ops if we're not in
350
384
* the postmaster process.) We'll allocate or attach to the shared
351
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.
352
389
*/
353
390
RequestAddinShmemSpace (pgws_shmem_size ());
391
+ #endif
354
392
355
393
register_wait_collector ();
356
394
357
395
/*
358
396
* Install hooks.
359
397
*/
398
+ #if PG_VERSION_NUM >= 150000
399
+ prev_shmem_request_hook = shmem_request_hook ;
400
+ shmem_request_hook = pgws_shmem_request ;
401
+ #endif
360
402
prev_shmem_startup_hook = shmem_startup_hook ;
361
403
shmem_startup_hook = pgws_shmem_startup ;
362
404
planner_hook_next = planner_hook ;
@@ -365,16 +407,6 @@ _PG_init(void)
365
407
ExecutorEnd_hook = pgws_ExecutorEnd ;
366
408
}
367
409
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
-
378
410
/*
379
411
* Find PGPROC entry responsible for given pid assuming ProcArrayLock was
380
412
* already taken.
0 commit comments