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-2026, 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());
144
145 /* include additional requested shmem from preload libraries */
146 size = add_size(size, total_addin_request);
147
148 /* might as well round it off to a multiple of a typical page size */
149 size = add_size(size, 8192 - (size % 8192));
150
151 return size;
152}
153
154#ifdef EXEC_BACKEND
155/*
156 * AttachSharedMemoryStructs
157 * Initialize a postmaster child process's access to shared memory
158 * structures.
159 *
160 * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
161 * isn't needed.
162 */
163void
164AttachSharedMemoryStructs(void)
165{
166 /* InitProcess must've been called already */
167 Assert(MyProc != NULL);
169
170 /*
171 * In EXEC_BACKEND mode, backends don't inherit the number of fast-path
172 * groups we calculated before setting the shmem up, so recalculate it.
173 */
175
177
178 /*
179 * Now give loadable modules a chance to set up their shmem allocations
180 */
183}
184#endif
185
186/*
187 * CreateSharedMemoryAndSemaphores
188 * Creates and initializes shared memory and semaphores.
189 */
190void
192{
193 PGShmemHeader *shim;
194 PGShmemHeader *seghdr;
195 Size size;
196
198
199 /* Compute the size of the shared-memory block */
200 size = CalculateShmemSize();
201 elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
202
203 /*
204 * Create the shmem segment
205 */
206 seghdr = PGSharedMemoryCreate(size, &shim);
207
208 /*
209 * Make sure that huge pages are never reported as "unknown" while the
210 * server is running.
211 */
212 Assert(strcmp("unknown",
213 GetConfigOption("huge_pages_status", false, false)) != 0);
214
215 InitShmemAccess(seghdr);
216
217 /*
218 * Set up shared memory allocation mechanism
219 */
221
222 /* Initialize subsystems */
224
225 /* Initialize dynamic shared memory facilities. */
227
228 /*
229 * Now give loadable modules a chance to set up their shmem allocations
230 */
233}
234
235/*
236 * Initialize various subsystems, setting up their data structures in
237 * shared memory.
238 *
239 * This is called by the postmaster or by a standalone backend.
240 * It is also called by a backend forked from the postmaster in the
241 * EXEC_BACKEND case. In the latter case, the shared memory segment
242 * already exists and has been physically attached to, but we have to
243 * initialize pointers in local memory that reference the shared structures,
244 * because we didn't inherit the correct pointer values from the postmaster
245 * as we do in the fork() scenario. The easiest way to do that is to run
246 * through the same code as before. (Note that the called routines mostly
247 * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
248 * This is a bit code-wasteful and could be cleaned up.)
249 */
250static void
252{
253 /*
254 * Now initialize LWLocks, which do shared memory allocation and are
255 * needed for InitShmemIndex.
256 */
258
259 /*
260 * Set up shmem.c index hashtable
261 */
263
266
267 /*
268 * Set up xlog, clog, and buffers
269 */
279
280 /*
281 * Set up lock manager
282 */
284
285 /*
286 * Set up predicate lock manager
287 */
289
290 /*
291 * Set up process table
292 */
299
300 /*
301 * Set up shared-inval messaging
302 */
304
305 /*
306 * Set up interprocess signaling mechanisms
307 */
320
321 /*
322 * Set up other modules that need some shared memory space
323 */
330 AioShmemInit();
333}
334
335/*
336 * InitializeShmemGUCs
337 *
338 * This function initializes runtime-computed GUCs related to the amount of
339 * shared memory required for the current configuration.
340 */
341void
343{
344 char buf[64];
345 Size size_b;
346 Size size_mb;
347 Size hp_size;
348
349 /*
350 * Calculate the shared memory size and round up to the nearest megabyte.
351 */
352 size_b = CalculateShmemSize();
353 size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
354 sprintf(buf, "%zu", size_mb);
355 SetConfigOption("shared_memory_size", buf,
357
358 /*
359 * Calculate the number of huge pages required.
360 */
361 GetHugePageSize(&hp_size, NULL);
362 if (hp_size != 0)
363 {
364 Size hp_required;
365
366 hp_required = add_size(size_b / hp_size, 1);
367 sprintf(buf, "%zu", hp_required);
368 SetConfigOption("shared_memory_size_in_huge_pages", buf,
370 }
371
372 sprintf(buf, "%d", ProcGlobalSemas());
374}
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:166
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:150
Size BufferManagerShmemSize(void)
Definition: buf_init.c:153
void BufferManagerShmemInit(void)
Definition: buf_init.c:68
size_t Size
Definition: c.h:625
void CheckpointerShmemInit(void)
Definition: checkpointer.c:979
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:957
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:123
Size DSMRegistryShmemSize(void)
Definition: dsm_registry.c:117
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:342
static void CreateOrAttachShmemStructs(void)
Definition: ipci.c:251
void CreateSharedMemoryAndSemaphores(void)
Definition: ipci.c:191
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:978
void ApplyLauncherShmemInit(void)
Definition: launcher.c:1033
Size LockManagerShmemSize(void)
Definition: lock.c:3756
void LockManagerShmemInit(void)
Definition: lock.c:444
void LogicalDecodingCtlShmemInit(void)
Definition: logicalctl.c:129
Size LogicalDecodingCtlShmemSize(void)
Definition: logicalctl.c:123
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:1726
Size MultiXactShmemSize(void)
Definition: multixact.c:1709
void BTreeShmemInit(void)
Definition: nbtutils.c:576
Size BTreeShmemSize(void)
Definition: nbtutils.c:563
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[DEFAULT_XLOG_SEG_SIZE]
Definition: pg_test_fsync.c:71
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:262
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:378
void ProcArrayShmemInit(void)
Definition: procarray.c:420
void ProcSignalShmemInit(void)
Definition: procsignal.c:131
Size ProcSignalShmemSize(void)
Definition: procsignal.c:117
void InitShmemIndex(void)
Definition: shmem.c:285
void InitShmemAccess(PGShmemHeader *seghdr)
Definition: shmem.c:104
Size add_size(Size s1, Size s2)
Definition: shmem.c:495
void InitShmemAllocation(void)
Definition: shmem.c:117
#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:1900
Size SlotSyncShmemSize(void)
Definition: slotsync.c:1891
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:3755
Size WalSndShmemSize(void)
Definition: walsender.c:3743
Size WalSummarizerShmemSize(void)
void WalSummarizerShmemInit(void)
void XLOGShmemInit(void)
Definition: xlog.c:5012
Size XLOGShmemSize(void)
Definition: xlog.c:4962
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:125
Size WaitLSNShmemSize(void)
Definition: xlogwait.c:114