PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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#include "utils/wait_event.h"
56
57/* GUCs */
59
61
63
64static void CreateOrAttachShmemStructs(void);
65
66/*
67 * RequestAddinShmemSpace
68 * Request that extra shmem space be allocated for use by
69 * a loadable module.
70 *
71 * This may only be called via the shmem_request_hook of a library that is
72 * loaded into the postmaster via shared_preload_libraries. Calls from
73 * elsewhere will fail.
74 */
75void
77{
79 elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
81}
82
83/*
84 * CalculateShmemSize
85 * Calculates the amount of shared memory needed.
86 */
87Size
89{
90 Size size;
91
92 /*
93 * Size of the Postgres shared-memory block is estimated via moderately-
94 * accurate estimates for the big hogs, plus 100K for the stuff that's too
95 * small to bother with estimating.
96 *
97 * We take some care to ensure that the total size request doesn't
98 * overflow size_t. If this gets through, we don't need to be so careful
99 * during the actual allocation phase.
100 */
101 size = 100000;
103 sizeof(ShmemIndexEnt)));
104 size = add_size(size, dsm_estimate_size());
105 size = add_size(size, DSMRegistryShmemSize());
106 size = add_size(size, BufferManagerShmemSize());
107 size = add_size(size, LockManagerShmemSize());
108 size = add_size(size, PredicateLockShmemSize());
109 size = add_size(size, ProcGlobalShmemSize());
110 size = add_size(size, XLogPrefetchShmemSize());
111 size = add_size(size, VarsupShmemSize());
112 size = add_size(size, XLOGShmemSize());
113 size = add_size(size, XLogRecoveryShmemSize());
114 size = add_size(size, CLOGShmemSize());
115 size = add_size(size, CommitTsShmemSize());
116 size = add_size(size, SUBTRANSShmemSize());
117 size = add_size(size, TwoPhaseShmemSize());
118 size = add_size(size, BackgroundWorkerShmemSize());
119 size = add_size(size, MultiXactShmemSize());
120 size = add_size(size, LWLockShmemSize());
121 size = add_size(size, ProcArrayShmemSize());
122 size = add_size(size, BackendStatusShmemSize());
123 size = add_size(size, SharedInvalShmemSize());
124 size = add_size(size, PMSignalShmemSize());
125 size = add_size(size, ProcSignalShmemSize());
126 size = add_size(size, CheckpointerShmemSize());
127 size = add_size(size, AutoVacuumShmemSize());
128 size = add_size(size, ReplicationSlotsShmemSize());
129 size = add_size(size, ReplicationOriginShmemSize());
130 size = add_size(size, WalSndShmemSize());
131 size = add_size(size, WalRcvShmemSize());
132 size = add_size(size, WalSummarizerShmemSize());
133 size = add_size(size, PgArchShmemSize());
134 size = add_size(size, ApplyLauncherShmemSize());
135 size = add_size(size, BTreeShmemSize());
136 size = add_size(size, SyncScanShmemSize());
137 size = add_size(size, AsyncShmemSize());
138 size = add_size(size, StatsShmemSize());
139 size = add_size(size, WaitEventCustomShmemSize());
140 size = add_size(size, InjectionPointShmemSize());
141 size = add_size(size, SlotSyncShmemSize());
142 size = add_size(size, AioShmemSize());
143 size = add_size(size, WaitLSNShmemSize());
145
146 /* include additional requested shmem from preload libraries */
147 size = add_size(size, total_addin_request);
148
149 /* might as well round it off to a multiple of a typical page size */
150 size = add_size(size, 8192 - (size % 8192));
151
152 return size;
153}
154
155#ifdef EXEC_BACKEND
156/*
157 * AttachSharedMemoryStructs
158 * Initialize a postmaster child process's access to shared memory
159 * structures.
160 *
161 * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
162 * isn't needed.
163 */
164void
166{
167 /* InitProcess must've been called already */
168 Assert(MyProc != NULL);
170
171 /*
172 * In EXEC_BACKEND mode, backends don't inherit the number of fast-path
173 * groups we calculated before setting the shmem up, so recalculate it.
174 */
176
178
179 /*
180 * Now give loadable modules a chance to set up their shmem allocations
181 */
184}
185#endif
186
187/*
188 * CreateSharedMemoryAndSemaphores
189 * Creates and initializes shared memory and semaphores.
190 */
191void
193{
196 Size size;
197
199
200 /* Compute the size of the shared-memory block */
201 size = CalculateShmemSize();
202 elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
203
204 /*
205 * Create the shmem segment
206 */
208
209 /*
210 * Make sure that huge pages are never reported as "unknown" while the
211 * server is running.
212 */
213 Assert(strcmp("unknown",
214 GetConfigOption("huge_pages_status", false, false)) != 0);
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();
332}
333
334/*
335 * InitializeShmemGUCs
336 *
337 * This function initializes runtime-computed GUCs related to the amount of
338 * shared memory required for the current configuration.
339 */
340void
342{
343 char buf[64];
344 Size size_b;
347
348 /*
349 * Calculate the shared memory size and round up to the nearest megabyte.
350 */
352 size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
353 sprintf(buf, "%zu", size_mb);
354 SetConfigOption("shared_memory_size", buf,
356
357 /*
358 * Calculate the number of huge pages required.
359 */
361 if (hp_size != 0)
362 {
364
366 if (size_b % hp_size != 0)
368 sprintf(buf, "%zu", hp_required);
369 SetConfigOption("shared_memory_size_in_huge_pages", buf,
371 }
372
373 sprintf(buf, "%d", ProcGlobalSemas());
375}
void AioShmemInit(void)
Definition aio_init.c:149
Size AioShmemSize(void)
Definition aio_init.c:113
Size AsyncShmemSize(void)
Definition async.c:776
void AsyncShmemInit(void)
Definition async.c:793
Size AutoVacuumShmemSize(void)
void AutoVacuumShmemInit(void)
void BackendStatusShmemInit(void)
Size BackendStatusShmemSize(void)
void BackgroundWorkerShmemInit(void)
Definition bgworker.c:173
Size BackgroundWorkerShmemSize(void)
Definition bgworker.c:157
Size BufferManagerShmemSize(void)
Definition buf_init.c:152
void BufferManagerShmemInit(void)
Definition buf_init.c:69
#define Assert(condition)
Definition c.h:906
size_t Size
Definition c.h:652
void CheckpointerShmemInit(void)
Size CheckpointerShmemSize(void)
void CLOGShmemInit(void)
Definition clog.c:787
Size CLOGShmemSize(void)
Definition clog.c:781
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)
Size DSMRegistryShmemSize(void)
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
void InjectionPointShmemInit(void)
Size InjectionPointShmemSize(void)
void(* shmem_startup_hook_type)(void)
Definition ipc.h:22
static Size total_addin_request
Definition ipci.c:62
int shared_memory_type
Definition ipci.c:58
shmem_startup_hook_type shmem_startup_hook
Definition ipci.c:60
void RequestAddinShmemSpace(Size size)
Definition ipci.c:76
Size CalculateShmemSize(void)
Definition ipci.c:88
void InitializeShmemGUCs(void)
Definition ipci.c:341
static void CreateOrAttachShmemStructs(void)
Definition ipci.c:250
void CreateSharedMemoryAndSemaphores(void)
Definition ipci.c:192
Size ApplyLauncherShmemSize(void)
Definition launcher.c:979
void ApplyLauncherShmemInit(void)
Definition launcher.c:1034
Size LockManagerShmemSize(void)
Definition lock.c:3756
void LockManagerShmemInit(void)
Definition lock.c:444
void LogicalDecodingCtlShmemInit(void)
Definition logicalctl.c:130
Size LogicalDecodingCtlShmemSize(void)
Definition logicalctl.c:124
void CreateLWLocks(void)
Definition lwlock.c:442
Size LWLockShmemSize(void)
Definition lwlock.c:398
bool process_shmem_requests_in_progress
Definition miscinit.c:1791
void MultiXactShmemInit(void)
Definition multixact.c:1773
Size MultiXactShmemSize(void)
Definition multixact.c:1761
void BTreeShmemInit(void)
Definition nbtutils.c:576
Size BTreeShmemSize(void)
Definition nbtutils.c:563
Size ReplicationOriginShmemSize(void)
Definition origin.c:543
void ReplicationOriginShmemInit(void)
Definition origin.c:558
#define DEFAULT_SHARED_MEMORY_TYPE
Definition pg_shmem.h:76
static char buf[DEFAULT_XLOG_SEG_SIZE]
Size PgArchShmemSize(void)
Definition pgarch.c:157
void PgArchShmemInit(void)
Definition pgarch.c:168
void StatsShmemInit(void)
Size StatsShmemSize(void)
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:583
void PredicateLockShmemInit(void)
Definition predicate.c:1146
Size PredicateLockShmemSize(void)
Definition predicate.c:1358
static int fb(int x)
Size ProcArrayShmemSize(void)
Definition procarray.c:380
void ProcArrayShmemInit(void)
Definition procarray.c:422
void ProcSignalShmemInit(void)
Definition procsignal.c:134
Size ProcSignalShmemSize(void)
Definition procsignal.c:120
void InitShmemAllocator(PGShmemHeader *seghdr)
Definition shmem.c:122
void InitShmemIndex(void)
Definition shmem.c:274
Size add_size(Size s1, Size s2)
Definition shmem.c:482
#define SHMEM_INDEX_SIZE
Definition shmem.h:52
Size SharedInvalShmemSize(void)
Definition sinvaladt.c:218
void SharedInvalShmemInit(void)
Definition sinvaladt.c:234
void ReplicationSlotsShmemInit(void)
Definition slot.c:207
Size ReplicationSlotsShmemSize(void)
Definition slot.c:189
void SlotSyncShmemInit(void)
Definition slotsync.c:1843
Size SlotSyncShmemSize(void)
Definition slotsync.c:1834
PGPROC * MyProc
Definition proc.c:68
Size ProcGlobalShmemSize(void)
Definition proc.c:130
int ProcGlobalSemas(void)
Definition proc.c:149
void InitProcGlobal(void)
Definition proc.c:184
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:702
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition sysv_shmem.c:480
Size TwoPhaseShmemSize(void)
Definition twophase.c:240
void TwoPhaseShmemInit(void)
Definition twophase.c:256
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:3766
Size WalSndShmemSize(void)
Definition walsender.c:3754
Size WalSummarizerShmemSize(void)
void WalSummarizerShmemInit(void)
void XLOGShmemInit(void)
Definition xlog.c:4995
Size XLOGShmemSize(void)
Definition xlog.c:4945
size_t XLogPrefetchShmemSize(void)
void XLogPrefetchShmemInit(void)
Size XLogRecoveryShmemSize(void)
void XLogRecoveryShmemInit(void)
void WaitLSNShmemInit(void)
Definition xlogwait.c:125
Size WaitLSNShmemSize(void)
Definition xlogwait.c:114