PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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#include "utils/memutils.h"
55
56/* GUCs */
58
60
62
63static void CreateOrAttachShmemStructs(void);
64
65/*
66 * RequestAddinShmemSpace
67 * Request that extra shmem space be allocated for use by
68 * a loadable module.
69 *
70 * This may only be called via the shmem_request_hook of a library that is
71 * loaded into the postmaster via shared_preload_libraries. Calls from
72 * elsewhere will fail.
73 */
74void
76{
78 elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
80}
81
82/*
83 * CalculateShmemSize
84 * Calculates the amount of shared memory and number of semaphores needed.
85 *
86 * If num_semaphores is not NULL, it will be set to the number of semaphores
87 * required.
88 */
89Size
90CalculateShmemSize(int *num_semaphores)
91{
92 Size size;
93 int numSemas;
94
95 /* Compute number of semaphores we'll need */
96 numSemas = ProcGlobalSemas();
97
98 /* Return the number of semaphores if requested by the caller */
99 if (num_semaphores)
100 *num_semaphores = numSemas;
101
102 /*
103 * Size of the Postgres shared-memory block is estimated via moderately-
104 * accurate estimates for the big hogs, plus 100K for the stuff that's too
105 * small to bother with estimating.
106 *
107 * We take some care to ensure that the total size request doesn't
108 * overflow size_t. If this gets through, we don't need to be so careful
109 * during the actual allocation phase.
110 */
111 size = 100000;
112 size = add_size(size, PGSemaphoreShmemSize(numSemas));
114 sizeof(ShmemIndexEnt)));
115 size = add_size(size, dsm_estimate_size());
116 size = add_size(size, DSMRegistryShmemSize());
117 size = add_size(size, BufferManagerShmemSize());
118 size = add_size(size, LockManagerShmemSize());
119 size = add_size(size, PredicateLockShmemSize());
120 size = add_size(size, ProcGlobalShmemSize());
121 size = add_size(size, XLogPrefetchShmemSize());
122 size = add_size(size, VarsupShmemSize());
123 size = add_size(size, XLOGShmemSize());
124 size = add_size(size, XLogRecoveryShmemSize());
125 size = add_size(size, CLOGShmemSize());
126 size = add_size(size, CommitTsShmemSize());
127 size = add_size(size, SUBTRANSShmemSize());
128 size = add_size(size, TwoPhaseShmemSize());
129 size = add_size(size, BackgroundWorkerShmemSize());
130 size = add_size(size, MultiXactShmemSize());
131 size = add_size(size, LWLockShmemSize());
132 size = add_size(size, ProcArrayShmemSize());
133 size = add_size(size, BackendStatusShmemSize());
134 size = add_size(size, SharedInvalShmemSize());
135 size = add_size(size, PMSignalShmemSize());
136 size = add_size(size, ProcSignalShmemSize());
137 size = add_size(size, CheckpointerShmemSize());
138 size = add_size(size, AutoVacuumShmemSize());
139 size = add_size(size, ReplicationSlotsShmemSize());
140 size = add_size(size, ReplicationOriginShmemSize());
141 size = add_size(size, WalSndShmemSize());
142 size = add_size(size, WalRcvShmemSize());
143 size = add_size(size, WalSummarizerShmemSize());
144 size = add_size(size, PgArchShmemSize());
145 size = add_size(size, ApplyLauncherShmemSize());
146 size = add_size(size, BTreeShmemSize());
147 size = add_size(size, SyncScanShmemSize());
148 size = add_size(size, AsyncShmemSize());
149 size = add_size(size, StatsShmemSize());
150 size = add_size(size, WaitEventCustomShmemSize());
151 size = add_size(size, InjectionPointShmemSize());
152 size = add_size(size, SlotSyncShmemSize());
153 size = add_size(size, AioShmemSize());
155
156 /* include additional requested shmem from preload libraries */
157 size = add_size(size, total_addin_request);
158
159 /* might as well round it off to a multiple of a typical page size */
160 size = add_size(size, 8192 - (size % 8192));
161
162 return size;
163}
164
165#ifdef EXEC_BACKEND
166/*
167 * AttachSharedMemoryStructs
168 * Initialize a postmaster child process's access to shared memory
169 * structures.
170 *
171 * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
172 * isn't needed.
173 */
174void
175AttachSharedMemoryStructs(void)
176{
177 /* InitProcess must've been called already */
178 Assert(MyProc != NULL);
180
181 /*
182 * In EXEC_BACKEND mode, backends don't inherit the number of fast-path
183 * groups we calculated before setting the shmem up, so recalculate it.
184 */
186
188
189 /*
190 * Now give loadable modules a chance to set up their shmem allocations
191 */
194}
195#endif
196
197/*
198 * CreateSharedMemoryAndSemaphores
199 * Creates and initializes shared memory and semaphores.
200 */
201void
203{
204 PGShmemHeader *shim;
205 PGShmemHeader *seghdr;
206 Size size;
207 int numSemas;
208
210
211 /* Compute the size of the shared-memory block */
212 size = CalculateShmemSize(&numSemas);
213 elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
214
215 /*
216 * Create the shmem segment
217 */
218 seghdr = PGSharedMemoryCreate(size, &shim);
219
220 /*
221 * Make sure that huge pages are never reported as "unknown" while the
222 * server is running.
223 */
224 Assert(strcmp("unknown",
225 GetConfigOption("huge_pages_status", false, false)) != 0);
226
227 InitShmemAccess(seghdr);
228
229 /*
230 * Create semaphores
231 */
232 PGReserveSemaphores(numSemas);
233
234 /*
235 * Set up shared memory allocation mechanism
236 */
238
239 /* Initialize subsystems */
241
242 /* Initialize dynamic shared memory facilities. */
244
245 /*
246 * Now give loadable modules a chance to set up their shmem allocations
247 */
250}
251
252/*
253 * Initialize various subsystems, setting up their data structures in
254 * shared memory.
255 *
256 * This is called by the postmaster or by a standalone backend.
257 * It is also called by a backend forked from the postmaster in the
258 * EXEC_BACKEND case. In the latter case, the shared memory segment
259 * already exists and has been physically attached to, but we have to
260 * initialize pointers in local memory that reference the shared structures,
261 * because we didn't inherit the correct pointer values from the postmaster
262 * as we do in the fork() scenario. The easiest way to do that is to run
263 * through the same code as before. (Note that the called routines mostly
264 * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
265 * This is a bit code-wasteful and could be cleaned up.)
266 */
267static void
269{
270 /*
271 * Now initialize LWLocks, which do shared memory allocation and are
272 * needed for InitShmemIndex.
273 */
275
276 /*
277 * Set up shmem.c index hashtable
278 */
280
283
284 /*
285 * Set up xlog, clog, and buffers
286 */
296
297 /*
298 * Set up lock manager
299 */
301
302 /*
303 * Set up predicate lock manager
304 */
306
307 /*
308 * Set up process table
309 */
316
317 /*
318 * Set up shared-inval messaging
319 */
321
322 /*
323 * Set up interprocess signaling mechanisms
324 */
337
338 /*
339 * Set up other modules that need some shared memory space
340 */
347 AioShmemInit();
349}
350
351/*
352 * InitializeShmemGUCs
353 *
354 * This function initializes runtime-computed GUCs related to the amount of
355 * shared memory required for the current configuration.
356 */
357void
359{
360 char buf[64];
361 Size size_b;
362 Size size_mb;
363 Size hp_size;
364 int num_semas;
365
366 /*
367 * Calculate the shared memory size and round up to the nearest megabyte.
368 */
369 size_b = CalculateShmemSize(&num_semas);
370 size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
371 sprintf(buf, "%zu", size_mb);
372 SetConfigOption("shared_memory_size", buf,
374
375 /*
376 * Calculate the number of huge pages required.
377 */
378 GetHugePageSize(&hp_size, NULL);
379 if (hp_size != 0)
380 {
381 Size hp_required;
382
383 hp_required = add_size(size_b / hp_size, 1);
384 sprintf(buf, "%zu", hp_required);
385 SetConfigOption("shared_memory_size_in_huge_pages", buf,
387 }
388
389 sprintf(buf, "%d", num_semas);
391}
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:3323
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3342
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:162
void BufferManagerShmemInit(void)
Definition: buf_init.c:68
size_t Size
Definition: c.h:576
void CheckpointerShmemInit(void)
Definition: checkpointer.c:958
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:939
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:226
bool IsUnderPostmaster
Definition: globals.c:121
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:61
int shared_memory_type
Definition: ipci.c:57
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:59
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:75
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:90
void InitializeShmemGUCs(void)
Definition: ipci.c:358
static void CreateOrAttachShmemStructs(void)
Definition: ipci.c:268
void CreateSharedMemoryAndSemaphores(void)
Definition: ipci.c:202
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:896
void ApplyLauncherShmemInit(void)
Definition: launcher.c:951
Size LockManagerShmemSize(void)
Definition: lock.c:3723
void LockManagerShmemInit(void)
Definition: lock.c:440
void CreateLWLocks(void)
Definition: lwlock.c:464
Size LWLockShmemSize(void)
Definition: lwlock.c:434
Size MemoryContextReportingShmemSize(void)
Definition: mcxtfuncs.c:619
void MemoryContextReportingShmemInit(void)
Definition: mcxtfuncs.c:637
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:3668
Size BTreeShmemSize(void)
Definition: nbtutils.c:3655
Size ReplicationOriginShmemSize(void)
Definition: origin.c:511
void ReplicationOriginShmemInit(void)
Definition: origin.c:526
#define DEFAULT_SHARED_MEMORY_TYPE
Definition: pg_shmem.h:76
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:1357
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:283
void InitShmemAccess(PGShmemHeader *seghdr)
Definition: shmem.c:102
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
void InitShmemAllocation(void)
Definition: shmem.c:115
#define SHMEM_INDEX_SIZE
Definition: shmem.h:53
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:1671
Size SlotSyncShmemSize(void)
Definition: slotsync.c:1662
PGPROC * MyProc
Definition: proc.c:67
Size ProcGlobalShmemSize(void)
Definition: proc.c:140
int ProcGlobalSemas(void)
Definition: proc.c:158
void InitProcGlobal(void)
Definition: proc.c:193
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:3626
Size WalSndShmemSize(void)
Definition: walsender.c:3614
Size WalSummarizerShmemSize(void)
void WalSummarizerShmemInit(void)
void XLOGShmemInit(void)
Definition: xlog.c:5099
Size XLOGShmemSize(void)
Definition: xlog.c:5049
size_t XLogPrefetchShmemSize(void)
void XLogPrefetchShmemInit(void)
Size XLogRecoveryShmemSize(void)
Definition: xlogrecovery.c:453
void XLogRecoveryShmemInit(void)
Definition: xlogrecovery.c:464