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 "access/xlogwait.h"
28#include "commands/async.h"
29#include "miscadmin.h"
30#include "pgstat.h"
33#include "postmaster/bgwriter.h"
36#include "replication/origin.h"
37#include "replication/slot.h"
41#include "storage/aio_subsys.h"
42#include "storage/bufmgr.h"
43#include "storage/dsm.h"
45#include "storage/ipc.h"
46#include "storage/pg_shmem.h"
47#include "storage/pmsignal.h"
48#include "storage/predicate.h"
49#include "storage/proc.h"
50#include "storage/procarray.h"
51#include "storage/procsignal.h"
52#include "storage/sinvaladt.h"
53#include "utils/guc.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 needed.
85 */
86Size
88{
89 Size size;
90
91 /*
92 * Size of the Postgres shared-memory block is estimated via moderately-
93 * accurate estimates for the big hogs, plus 100K for the stuff that's too
94 * small to bother with estimating.
95 *
96 * We take some care to ensure that the total size request doesn't
97 * overflow size_t. If this gets through, we don't need to be so careful
98 * during the actual allocation phase.
99 */
100 size = 100000;
102 sizeof(ShmemIndexEnt)));
103 size = add_size(size, dsm_estimate_size());
104 size = add_size(size, DSMRegistryShmemSize());
105 size = add_size(size, BufferManagerShmemSize());
106 size = add_size(size, LockManagerShmemSize());
107 size = add_size(size, PredicateLockShmemSize());
108 size = add_size(size, ProcGlobalShmemSize());
109 size = add_size(size, XLogPrefetchShmemSize());
110 size = add_size(size, VarsupShmemSize());
111 size = add_size(size, XLOGShmemSize());
112 size = add_size(size, XLogRecoveryShmemSize());
113 size = add_size(size, CLOGShmemSize());
114 size = add_size(size, CommitTsShmemSize());
115 size = add_size(size, SUBTRANSShmemSize());
116 size = add_size(size, TwoPhaseShmemSize());
117 size = add_size(size, BackgroundWorkerShmemSize());
118 size = add_size(size, MultiXactShmemSize());
119 size = add_size(size, LWLockShmemSize());
120 size = add_size(size, ProcArrayShmemSize());
121 size = add_size(size, BackendStatusShmemSize());
122 size = add_size(size, SharedInvalShmemSize());
123 size = add_size(size, PMSignalShmemSize());
124 size = add_size(size, ProcSignalShmemSize());
125 size = add_size(size, CheckpointerShmemSize());
126 size = add_size(size, AutoVacuumShmemSize());
127 size = add_size(size, ReplicationSlotsShmemSize());
128 size = add_size(size, ReplicationOriginShmemSize());
129 size = add_size(size, WalSndShmemSize());
130 size = add_size(size, WalRcvShmemSize());
131 size = add_size(size, WalSummarizerShmemSize());
132 size = add_size(size, PgArchShmemSize());
133 size = add_size(size, ApplyLauncherShmemSize());
134 size = add_size(size, BTreeShmemSize());
135 size = add_size(size, SyncScanShmemSize());
136 size = add_size(size, AsyncShmemSize());
137 size = add_size(size, StatsShmemSize());
138 size = add_size(size, WaitEventCustomShmemSize());
139 size = add_size(size, InjectionPointShmemSize());
140 size = add_size(size, SlotSyncShmemSize());
141 size = add_size(size, AioShmemSize());
142 size = add_size(size, WaitLSNShmemSize());
143
144 /* include additional requested shmem from preload libraries */
145 size = add_size(size, total_addin_request);
146
147 /* might as well round it off to a multiple of a typical page size */
148 size = add_size(size, 8192 - (size % 8192));
149
150 return size;
151}
152
153#ifdef EXEC_BACKEND
154/*
155 * AttachSharedMemoryStructs
156 * Initialize a postmaster child process's access to shared memory
157 * structures.
158 *
159 * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
160 * isn't needed.
161 */
162void
163AttachSharedMemoryStructs(void)
164{
165 /* InitProcess must've been called already */
166 Assert(MyProc != NULL);
168
169 /*
170 * In EXEC_BACKEND mode, backends don't inherit the number of fast-path
171 * groups we calculated before setting the shmem up, so recalculate it.
172 */
174
176
177 /*
178 * Now give loadable modules a chance to set up their shmem allocations
179 */
182}
183#endif
184
185/*
186 * CreateSharedMemoryAndSemaphores
187 * Creates and initializes shared memory and semaphores.
188 */
189void
191{
192 PGShmemHeader *shim;
193 PGShmemHeader *seghdr;
194 Size size;
195
197
198 /* Compute the size of the shared-memory block */
199 size = CalculateShmemSize();
200 elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
201
202 /*
203 * Create the shmem segment
204 */
205 seghdr = PGSharedMemoryCreate(size, &shim);
206
207 /*
208 * Make sure that huge pages are never reported as "unknown" while the
209 * server is running.
210 */
211 Assert(strcmp("unknown",
212 GetConfigOption("huge_pages_status", false, false)) != 0);
213
214 InitShmemAccess(seghdr);
215
216 /*
217 * Set up shared memory allocation mechanism
218 */
220
221 /* Initialize subsystems */
223
224 /* Initialize dynamic shared memory facilities. */
226
227 /*
228 * Now give loadable modules a chance to set up their shmem allocations
229 */
232}
233
234/*
235 * Initialize various subsystems, setting up their data structures in
236 * shared memory.
237 *
238 * This is called by the postmaster or by a standalone backend.
239 * It is also called by a backend forked from the postmaster in the
240 * EXEC_BACKEND case. In the latter case, the shared memory segment
241 * already exists and has been physically attached to, but we have to
242 * initialize pointers in local memory that reference the shared structures,
243 * because we didn't inherit the correct pointer values from the postmaster
244 * as we do in the fork() scenario. The easiest way to do that is to run
245 * through the same code as before. (Note that the called routines mostly
246 * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
247 * This is a bit code-wasteful and could be cleaned up.)
248 */
249static void
251{
252 /*
253 * Now initialize LWLocks, which do shared memory allocation and are
254 * needed for InitShmemIndex.
255 */
257
258 /*
259 * Set up shmem.c index hashtable
260 */
262
265
266 /*
267 * Set up xlog, clog, and buffers
268 */
278
279 /*
280 * Set up lock manager
281 */
283
284 /*
285 * Set up predicate lock manager
286 */
288
289 /*
290 * Set up process table
291 */
298
299 /*
300 * Set up shared-inval messaging
301 */
303
304 /*
305 * Set up interprocess signaling mechanisms
306 */
319
320 /*
321 * Set up other modules that need some shared memory space
322 */
329 AioShmemInit();
331}
332
333/*
334 * InitializeShmemGUCs
335 *
336 * This function initializes runtime-computed GUCs related to the amount of
337 * shared memory required for the current configuration.
338 */
339void
341{
342 char buf[64];
343 Size size_b;
344 Size size_mb;
345 Size hp_size;
346
347 /*
348 * Calculate the shared memory size and round up to the nearest megabyte.
349 */
350 size_b = CalculateShmemSize();
351 size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
352 sprintf(buf, "%zu", size_mb);
353 SetConfigOption("shared_memory_size", buf,
355
356 /*
357 * Calculate the number of huge pages required.
358 */
359 GetHugePageSize(&hp_size, NULL);
360 if (hp_size != 0)
361 {
362 Size hp_required;
363
364 hp_required = add_size(size_b / hp_size, 1);
365 sprintf(buf, "%zu", hp_required);
366 SetConfigOption("shared_memory_size_in_huge_pages", buf,
368 }
369
370 sprintf(buf, "%d", ProcGlobalSemas());
372}
void AioShmemInit(void)
Definition: aio_init.c:149
Size AioShmemSize(void)
Definition: aio_init.c:113
Size AsyncShmemSize(void)
Definition: async.c:484
void AsyncShmemInit(void)
Definition: async.c:501
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3385
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3404
void BackendStatusShmemInit(void)
Size BackendStatusShmemSize(void)
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:165
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:149
Size BufferManagerShmemSize(void)
Definition: buf_init.c:153
void BufferManagerShmemInit(void)
Definition: buf_init.c:68
size_t Size
Definition: c.h:615
void CheckpointerShmemInit(void)
Definition: checkpointer.c:973
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:951
void CLOGShmemInit(void)
Definition: clog.c:786
Size CLOGShmemSize(void)
Definition: clog.c:780
Size CommitTsShmemSize(void)
Definition: commit_ts.c:517
void CommitTsShmemInit(void)
Definition: commit_ts.c:528
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:124
Size DSMRegistryShmemSize(void)
Definition: dsm_registry.c:118
Size hash_estimate_size(int64 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:120
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4196
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:4219
@ 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(void)
Definition: ipci.c:87
void InitializeShmemGUCs(void)
Definition: ipci.c:340
static void CreateOrAttachShmemStructs(void)
Definition: ipci.c:250
void CreateSharedMemoryAndSemaphores(void)
Definition: ipci.c:190
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:978
void ApplyLauncherShmemInit(void)
Definition: launcher.c:1033
Size LockManagerShmemSize(void)
Definition: lock.c:3758
void LockManagerShmemInit(void)
Definition: lock.c:444
void CreateLWLocks(void)
Definition: lwlock.c:441
Size LWLockShmemSize(void)
Definition: lwlock.c:397
bool process_shmem_requests_in_progress
Definition: miscinit.c:1790
void MultiXactShmemInit(void)
Definition: multixact.c:1955
Size MultiXactShmemSize(void)
Definition: multixact.c:1938
void BTreeShmemInit(void)
Definition: nbtutils.c:3761
Size BTreeShmemSize(void)
Definition: nbtutils.c:3748
Size ReplicationOriginShmemSize(void)
Definition: origin.c:534
void ReplicationOriginShmemInit(void)
Definition: origin.c:549
#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
void InitializeFastPathLocks(void)
Definition: postinit.c:575
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:131
Size ProcSignalShmemSize(void)
Definition: procsignal.c:117
void InitShmemIndex(void)
Definition: shmem.c:284
void InitShmemAccess(PGShmemHeader *seghdr)
Definition: shmem.c:103
Size add_size(Size s1, Size s2)
Definition: shmem.c:494
void InitShmemAllocation(void)
Definition: shmem.c:116
#define SHMEM_INDEX_SIZE
Definition: shmem.h:53
Size SharedInvalShmemSize(void)
Definition: sinvaladt.c:218
void SharedInvalShmemInit(void)
Definition: sinvaladt.c:234
void ReplicationSlotsShmemInit(void)
Definition: slot.c:206
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:188
void SlotSyncShmemInit(void)
Definition: slotsync.c:1692
Size SlotSyncShmemSize(void)
Definition: slotsync.c:1683
PGPROC * MyProc
Definition: proc.c:67
Size ProcGlobalShmemSize(void)
Definition: proc.c:140
int ProcGlobalSemas(void)
Definition: proc.c:159
void InitProcGlobal(void)
Definition: proc.c:194
void SUBTRANSShmemInit(void)
Definition: subtrans.c:219
Size SUBTRANSShmemSize(void)
Definition: subtrans.c:213
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:239
void TwoPhaseShmemInit(void)
Definition: twophase.c:255
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:3748
Size WalSndShmemSize(void)
Definition: walsender.c:3736
Size WalSummarizerShmemSize(void)
void WalSummarizerShmemInit(void)
void XLOGShmemInit(void)
Definition: xlog.c:4974
Size XLOGShmemSize(void)
Definition: xlog.c:4924
size_t XLogPrefetchShmemSize(void)
void XLogPrefetchShmemInit(void)
Size XLogRecoveryShmemSize(void)
Definition: xlogrecovery.c:455
void XLogRecoveryShmemInit(void)
Definition: xlogrecovery.c:466
void WaitLSNShmemInit(void)
Definition: xlogwait.c:78
Size WaitLSNShmemSize(void)
Definition: xlogwait.c:67