PostgreSQL Source Code git master
ipci.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * ipci.c
4 * POSTGRES inter-process communication initialization code.
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/storage/ipc/ipci.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
17#include "access/clog.h"
18#include "access/commit_ts.h"
19#include "access/multixact.h"
20#include "access/nbtree.h"
21#include "access/subtrans.h"
22#include "access/syncscan.h"
23#include "access/transam.h"
24#include "access/twophase.h"
26#include "access/xlogrecovery.h"
27#include "commands/async.h"
28#include "miscadmin.h"
29#include "pgstat.h"
32#include "postmaster/bgwriter.h"
35#include "replication/origin.h"
36#include "replication/slot.h"
40#include "storage/aio_subsys.h"
41#include "storage/bufmgr.h"
42#include "storage/dsm.h"
44#include "storage/ipc.h"
45#include "storage/pg_shmem.h"
46#include "storage/pmsignal.h"
47#include "storage/predicate.h"
48#include "storage/proc.h"
49#include "storage/procarray.h"
50#include "storage/procsignal.h"
51#include "storage/sinvaladt.h"
52#include "utils/guc.h"
54
55/* GUCs */
57
59
61
62static void CreateOrAttachShmemStructs(void);
63
64/*
65 * RequestAddinShmemSpace
66 * Request that extra shmem space be allocated for use by
67 * a loadable module.
68 *
69 * This may only be called via the shmem_request_hook of a library that is
70 * loaded into the postmaster via shared_preload_libraries. Calls from
71 * elsewhere will fail.
72 */
73void
75{
77 elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
79}
80
81/*
82 * CalculateShmemSize
83 * Calculates the amount of shared memory and number of semaphores needed.
84 *
85 * If num_semaphores is not NULL, it will be set to the number of semaphores
86 * required.
87 */
88Size
89CalculateShmemSize(int *num_semaphores)
90{
91 Size size;
92 int numSemas;
93
94 /* Compute number of semaphores we'll need */
95 numSemas = ProcGlobalSemas();
96
97 /* Return the number of semaphores if requested by the caller */
98 if (num_semaphores)
99 *num_semaphores = numSemas;
100
101 /*
102 * Size of the Postgres shared-memory block is estimated via moderately-
103 * accurate estimates for the big hogs, plus 100K for the stuff that's too
104 * small to bother with estimating.
105 *
106 * We take some care to ensure that the total size request doesn't
107 * overflow size_t. If this gets through, we don't need to be so careful
108 * during the actual allocation phase.
109 */
110 size = 100000;
111 size = add_size(size, PGSemaphoreShmemSize(numSemas));
113 sizeof(ShmemIndexEnt)));
114 size = add_size(size, dsm_estimate_size());
115 size = add_size(size, DSMRegistryShmemSize());
116 size = add_size(size, BufferManagerShmemSize());
117 size = add_size(size, LockManagerShmemSize());
118 size = add_size(size, PredicateLockShmemSize());
119 size = add_size(size, ProcGlobalShmemSize());
120 size = add_size(size, XLogPrefetchShmemSize());
121 size = add_size(size, VarsupShmemSize());
122 size = add_size(size, XLOGShmemSize());
123 size = add_size(size, XLogRecoveryShmemSize());
124 size = add_size(size, CLOGShmemSize());
125 size = add_size(size, CommitTsShmemSize());
126 size = add_size(size, SUBTRANSShmemSize());
127 size = add_size(size, TwoPhaseShmemSize());
128 size = add_size(size, BackgroundWorkerShmemSize());
129 size = add_size(size, MultiXactShmemSize());
130 size = add_size(size, LWLockShmemSize());
131 size = add_size(size, ProcArrayShmemSize());
132 size = add_size(size, BackendStatusShmemSize());
133 size = add_size(size, SharedInvalShmemSize());
134 size = add_size(size, PMSignalShmemSize());
135 size = add_size(size, ProcSignalShmemSize());
136 size = add_size(size, CheckpointerShmemSize());
137 size = add_size(size, AutoVacuumShmemSize());
138 size = add_size(size, ReplicationSlotsShmemSize());
139 size = add_size(size, ReplicationOriginShmemSize());
140 size = add_size(size, WalSndShmemSize());
141 size = add_size(size, WalRcvShmemSize());
142 size = add_size(size, WalSummarizerShmemSize());
143 size = add_size(size, PgArchShmemSize());
144 size = add_size(size, ApplyLauncherShmemSize());
145 size = add_size(size, BTreeShmemSize());
146 size = add_size(size, SyncScanShmemSize());
147 size = add_size(size, AsyncShmemSize());
148 size = add_size(size, StatsShmemSize());
149 size = add_size(size, WaitEventCustomShmemSize());
150 size = add_size(size, InjectionPointShmemSize());
151 size = add_size(size, SlotSyncShmemSize());
152 size = add_size(size, AioShmemSize());
153
154 /* include additional requested shmem from preload libraries */
155 size = add_size(size, total_addin_request);
156
157 /* might as well round it off to a multiple of a typical page size */
158 size = add_size(size, 8192 - (size % 8192));
159
160 return size;
161}
162
163#ifdef EXEC_BACKEND
164/*
165 * AttachSharedMemoryStructs
166 * Initialize a postmaster child process's access to shared memory
167 * structures.
168 *
169 * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
170 * isn't needed.
171 */
172void
173AttachSharedMemoryStructs(void)
174{
175 /* InitProcess must've been called already */
176 Assert(MyProc != NULL);
178
179 /*
180 * In EXEC_BACKEND mode, backends don't inherit the number of fast-path
181 * groups we calculated before setting the shmem up, so recalculate it.
182 */
184
186
187 /*
188 * Now give loadable modules a chance to set up their shmem allocations
189 */
192}
193#endif
194
195/*
196 * CreateSharedMemoryAndSemaphores
197 * Creates and initializes shared memory and semaphores.
198 */
199void
201{
202 PGShmemHeader *shim;
203 PGShmemHeader *seghdr;
204 Size size;
205 int numSemas;
206
208
209 /* Compute the size of the shared-memory block */
210 size = CalculateShmemSize(&numSemas);
211 elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
212
213 /*
214 * Create the shmem segment
215 */
216 seghdr = PGSharedMemoryCreate(size, &shim);
217
218 /*
219 * Make sure that huge pages are never reported as "unknown" while the
220 * server is running.
221 */
222 Assert(strcmp("unknown",
223 GetConfigOption("huge_pages_status", false, false)) != 0);
224
225 InitShmemAccess(seghdr);
226
227 /*
228 * Create semaphores
229 */
230 PGReserveSemaphores(numSemas);
231
232 /*
233 * Set up shared memory allocation mechanism
234 */
236
237 /* Initialize subsystems */
239
240 /* Initialize dynamic shared memory facilities. */
242
243 /*
244 * Now give loadable modules a chance to set up their shmem allocations
245 */
248}
249
250/*
251 * Initialize various subsystems, setting up their data structures in
252 * shared memory.
253 *
254 * This is called by the postmaster or by a standalone backend.
255 * It is also called by a backend forked from the postmaster in the
256 * EXEC_BACKEND case. In the latter case, the shared memory segment
257 * already exists and has been physically attached to, but we have to
258 * initialize pointers in local memory that reference the shared structures,
259 * because we didn't inherit the correct pointer values from the postmaster
260 * as we do in the fork() scenario. The easiest way to do that is to run
261 * through the same code as before. (Note that the called routines mostly
262 * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
263 * This is a bit code-wasteful and could be cleaned up.)
264 */
265static void
267{
268 /*
269 * Now initialize LWLocks, which do shared memory allocation and are
270 * needed for InitShmemIndex.
271 */
273
274 /*
275 * Set up shmem.c index hashtable
276 */
278
281
282 /*
283 * Set up xlog, clog, and buffers
284 */
294
295 /*
296 * Set up lock manager
297 */
299
300 /*
301 * Set up predicate lock manager
302 */
304
305 /*
306 * Set up process table
307 */
314
315 /*
316 * Set up shared-inval messaging
317 */
319
320 /*
321 * Set up interprocess signaling mechanisms
322 */
335
336 /*
337 * Set up other modules that need some shared memory space
338 */
345 AioShmemInit();
346}
347
348/*
349 * InitializeShmemGUCs
350 *
351 * This function initializes runtime-computed GUCs related to the amount of
352 * shared memory required for the current configuration.
353 */
354void
356{
357 char buf[64];
358 Size size_b;
359 Size size_mb;
360 Size hp_size;
361 int num_semas;
362
363 /*
364 * Calculate the shared memory size and round up to the nearest megabyte.
365 */
366 size_b = CalculateShmemSize(&num_semas);
367 size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
368 sprintf(buf, "%zu", size_mb);
369 SetConfigOption("shared_memory_size", buf,
371
372 /*
373 * Calculate the number of huge pages required.
374 */
375 GetHugePageSize(&hp_size, NULL);
376 if (hp_size != 0)
377 {
378 Size hp_required;
379
380 hp_required = add_size(size_b / hp_size, 1);
381 sprintf(buf, "%zu", hp_required);
382 SetConfigOption("shared_memory_size_in_huge_pages", buf,
384 }
385
386 sprintf(buf, "%d", num_semas);
388}
void AioShmemInit(void)
Definition: aio_init.c:153
Size AioShmemSize(void)
Definition: aio_init.c:117
Size AsyncShmemSize(void)
Definition: async.c:485
void AsyncShmemInit(void)
Definition: async.c:502
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3319
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3338
void BackendStatusShmemInit(void)
Size BackendStatusShmemSize(void)
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:162
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:146
Size BufferManagerShmemSize(void)
Definition: buf_init.c:159
void BufferManagerShmemInit(void)
Definition: buf_init.c:67
size_t Size
Definition: c.h:576
void CheckpointerShmemInit(void)
Definition: checkpointer.c:954
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:935
void CLOGShmemInit(void)
Definition: clog.c:787
Size CLOGShmemSize(void)
Definition: clog.c:781
Size CommitTsShmemSize(void)
Definition: commit_ts.c:519
void CommitTsShmemInit(void)
Definition: commit_ts.c:530
size_t dsm_estimate_size(void)
Definition: dsm.c:470
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:177
void dsm_shmem_init(void)
Definition: dsm.c:479
void DSMRegistryShmemInit(void)
Definition: dsm_registry.c:69
Size DSMRegistryShmemSize(void)
Definition: dsm_registry.c:63
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:783
#define DEBUG3
Definition: elog.h:28
#define FATAL
Definition: elog.h:41
#define elog(elevel,...)
Definition: elog.h:225
bool IsUnderPostmaster
Definition: globals.c:119
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4332
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:4355
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:114
@ PGC_INTERNAL
Definition: guc.h:73
Assert(PointerIsAligned(start, uint64))
void InjectionPointShmemInit(void)
Size InjectionPointShmemSize(void)
void(* shmem_startup_hook_type)(void)
Definition: ipc.h:22
static Size total_addin_request
Definition: ipci.c:60
int shared_memory_type
Definition: ipci.c:56
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:58
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:74
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:89
void InitializeShmemGUCs(void)
Definition: ipci.c:355
static void CreateOrAttachShmemStructs(void)
Definition: ipci.c:266
void CreateSharedMemoryAndSemaphores(void)
Definition: ipci.c:200
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:896
void ApplyLauncherShmemInit(void)
Definition: launcher.c:951
Size LockManagerShmemSize(void)
Definition: lock.c:3714
void LockManagerShmemInit(void)
Definition: lock.c:440
void CreateLWLocks(void)
Definition: lwlock.c:461
Size LWLockShmemSize(void)
Definition: lwlock.c:431
bool process_shmem_requests_in_progress
Definition: miscinit.c:1841
void MultiXactShmemInit(void)
Definition: multixact.c:1964
Size MultiXactShmemSize(void)
Definition: multixact.c:1947
void BTreeShmemInit(void)
Definition: nbtutils.c:2691
Size BTreeShmemSize(void)
Definition: nbtutils.c:2678
Size ReplicationOriginShmemSize(void)
Definition: origin.c:510
void ReplicationOriginShmemInit(void)
Definition: origin.c:530
#define DEFAULT_SHARED_MEMORY_TYPE
Definition: pg_shmem.h:75
static char * buf
Definition: pg_test_fsync.c:72
Size PgArchShmemSize(void)
Definition: pgarch.c:156
void PgArchShmemInit(void)
Definition: pgarch.c:167
void StatsShmemInit(void)
Definition: pgstat_shmem.c:156
Size StatsShmemSize(void)
Definition: pgstat_shmem.c:127
Size PMSignalShmemSize(void)
Definition: pmsignal.c:130
void PMSignalShmemInit(void)
Definition: pmsignal.c:145
#define sprintf
Definition: port.h:241
Size PGSemaphoreShmemSize(int maxSemas)
Definition: posix_sema.c:165
void PGReserveSemaphores(int maxSemas)
Definition: posix_sema.c:196
void InitializeFastPathLocks(void)
Definition: postinit.c:587
void PredicateLockShmemInit(void)
Definition: predicate.c:1145
Size PredicateLockShmemSize(void)
Definition: predicate.c:1347
Size ProcArrayShmemSize(void)
Definition: procarray.c:376
void ProcArrayShmemInit(void)
Definition: procarray.c:418
void ProcSignalShmemInit(void)
Definition: procsignal.c:130
Size ProcSignalShmemSize(void)
Definition: procsignal.c:116
void InitShmemIndex(void)
Definition: shmem.c:278
void InitShmemAccess(PGShmemHeader *seghdr)
Definition: shmem.c:97
Size add_size(Size s1, Size s2)
Definition: shmem.c:488
void InitShmemAllocation(void)
Definition: shmem.c:110
#define SHMEM_INDEX_SIZE
Definition: shmem.h:51
Size SharedInvalShmemSize(void)
Definition: sinvaladt.c:217
void SharedInvalShmemInit(void)
Definition: sinvaladt.c:233
void ReplicationSlotsShmemInit(void)
Definition: slot.c:204
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:186
void SlotSyncShmemInit(void)
Definition: slotsync.c:1665
Size SlotSyncShmemSize(void)
Definition: slotsync.c:1656
PGPROC * MyProc
Definition: proc.c:66
Size ProcGlobalShmemSize(void)
Definition: proc.c:97
int ProcGlobalSemas(void)
Definition: proc.c:130
void InitProcGlobal(void)
Definition: proc.c:165
void SUBTRANSShmemInit(void)
Definition: subtrans.c:220
Size SUBTRANSShmemSize(void)
Definition: subtrans.c:214
void SyncScanShmemInit(void)
Definition: syncscan.c:135
Size SyncScanShmemSize(void)
Definition: syncscan.c:126
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
Definition: sysv_shmem.c:700
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:479
Size TwoPhaseShmemSize(void)
Definition: twophase.c:237
void TwoPhaseShmemInit(void)
Definition: twophase.c:253
Size VarsupShmemSize(void)
Definition: varsup.c:41
void VarsupShmemInit(void)
Definition: varsup.c:47
Size WaitEventCustomShmemSize(void)
Definition: wait_event.c:103
void WaitEventCustomShmemInit(void)
Definition: wait_event.c:119
void WalRcvShmemInit(void)
Size WalRcvShmemSize(void)
void WalSndShmemInit(void)
Definition: walsender.c:3594
Size WalSndShmemSize(void)
Definition: walsender.c:3582
Size WalSummarizerShmemSize(void)
void WalSummarizerShmemInit(void)
void XLOGShmemInit(void)
Definition: xlog.c:4964
Size XLOGShmemSize(void)
Definition: xlog.c:4914
size_t XLogPrefetchShmemSize(void)
void XLogPrefetchShmemInit(void)
Size XLogRecoveryShmemSize(void)
Definition: xlogrecovery.c:448
void XLogRecoveryShmemInit(void)
Definition: xlogrecovery.c:459