1 /*-------------------------------------------------------------------------
4 * POSTGRES inter-process communication initialization code.
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/storage/ipc/ipci.c
13 *-------------------------------------------------------------------------
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"
25 #include "access/xlogprefetcher.h"
26 #include "access/xlogrecovery.h"
27 #include "commands/async.h"
28 #include "miscadmin.h"
30 #include "postmaster/autovacuum.h"
31 #include "postmaster/bgworker_internals.h"
32 #include "postmaster/bgwriter.h"
33 #include "postmaster/walsummarizer.h"
34 #include "replication/logicallauncher.h"
35 #include "replication/origin.h"
36 #include "replication/slot.h"
37 #include "replication/slotsync.h"
38 #include "replication/walreceiver.h"
39 #include "replication/walsender.h"
40 #include "storage/bufmgr.h"
41 #include "storage/dsm.h"
42 #include "storage/dsm_registry.h"
43 #include "storage/ipc.h"
44 #include "storage/pg_shmem.h"
45 #include "storage/pmsignal.h"
46 #include "storage/predicate.h"
47 #include "storage/proc.h"
48 #include "storage/procarray.h"
49 #include "storage/procsignal.h"
50 #include "storage/sinvaladt.h"
51 #include "utils/guc.h"
52 #include "utils/injection_point.h"
55 int shared_memory_type
= DEFAULT_SHARED_MEMORY_TYPE
;
57 shmem_startup_hook_type shmem_startup_hook
= NULL
;
59 static Size total_addin_request
= 0;
61 static void CreateOrAttachShmemStructs(void);
64 * RequestAddinShmemSpace
65 * Request that extra shmem space be allocated for use by
68 * This may only be called via the shmem_request_hook of a library that is
69 * loaded into the postmaster via shared_preload_libraries. Calls from
70 * elsewhere will fail.
73 RequestAddinShmemSpace(Size size
)
75 if (!process_shmem_requests_in_progress
)
76 elog(FATAL
, "cannot request additional shared memory outside shmem_request_hook");
77 total_addin_request
= add_size(total_addin_request
, size
);
82 * Calculates the amount of shared memory and number of semaphores needed.
84 * If num_semaphores is not NULL, it will be set to the number of semaphores
88 CalculateShmemSize(int *num_semaphores
)
93 /* Compute number of semaphores we'll need */
94 numSemas
= ProcGlobalSemas();
96 /* Return the number of semaphores if requested by the caller */
98 *num_semaphores
= numSemas
;
101 * Size of the Postgres shared-memory block is estimated via moderately-
102 * accurate estimates for the big hogs, plus 100K for the stuff that's too
103 * small to bother with estimating.
105 * We take some care to ensure that the total size request doesn't
106 * overflow size_t. If this gets through, we don't need to be so careful
107 * during the actual allocation phase.
110 size
= add_size(size
, PGSemaphoreShmemSize(numSemas
));
111 size
= add_size(size
, hash_estimate_size(SHMEM_INDEX_SIZE
,
112 sizeof(ShmemIndexEnt
)));
113 size
= add_size(size
, dsm_estimate_size());
114 size
= add_size(size
, DSMRegistryShmemSize());
115 size
= add_size(size
, BufferManagerShmemSize());
116 size
= add_size(size
, LockManagerShmemSize());
117 size
= add_size(size
, PredicateLockShmemSize());
118 size
= add_size(size
, ProcGlobalShmemSize());
119 size
= add_size(size
, XLogPrefetchShmemSize());
120 size
= add_size(size
, VarsupShmemSize());
121 size
= add_size(size
, XLOGShmemSize());
122 size
= add_size(size
, XLogRecoveryShmemSize());
123 size
= add_size(size
, CLOGShmemSize());
124 size
= add_size(size
, CommitTsShmemSize());
125 size
= add_size(size
, SUBTRANSShmemSize());
126 size
= add_size(size
, TwoPhaseShmemSize());
127 size
= add_size(size
, BackgroundWorkerShmemSize());
128 size
= add_size(size
, MultiXactShmemSize());
129 size
= add_size(size
, LWLockShmemSize());
130 size
= add_size(size
, ProcArrayShmemSize());
131 size
= add_size(size
, BackendStatusShmemSize());
132 size
= add_size(size
, SharedInvalShmemSize());
133 size
= add_size(size
, PMSignalShmemSize());
134 size
= add_size(size
, ProcSignalShmemSize());
135 size
= add_size(size
, CheckpointerShmemSize());
136 size
= add_size(size
, AutoVacuumShmemSize());
137 size
= add_size(size
, ReplicationSlotsShmemSize());
138 size
= add_size(size
, ReplicationOriginShmemSize());
139 size
= add_size(size
, WalSndShmemSize());
140 size
= add_size(size
, WalRcvShmemSize());
141 size
= add_size(size
, WalSummarizerShmemSize());
142 size
= add_size(size
, PgArchShmemSize());
143 size
= add_size(size
, ApplyLauncherShmemSize());
144 size
= add_size(size
, BTreeShmemSize());
145 size
= add_size(size
, SyncScanShmemSize());
146 size
= add_size(size
, AsyncShmemSize());
147 size
= add_size(size
, StatsShmemSize());
148 size
= add_size(size
, WaitEventCustomShmemSize());
149 size
= add_size(size
, InjectionPointShmemSize());
150 size
= add_size(size
, SlotSyncShmemSize());
152 /* include additional requested shmem from preload libraries */
153 size
= add_size(size
, total_addin_request
);
155 /* might as well round it off to a multiple of a typical page size */
156 size
= add_size(size
, 8192 - (size
% 8192));
163 * AttachSharedMemoryStructs
164 * Initialize a postmaster child process's access to shared memory
167 * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
171 AttachSharedMemoryStructs(void)
173 /* InitProcess must've been called already */
174 Assert(MyProc
!= NULL
);
175 Assert(IsUnderPostmaster
);
178 * In EXEC_BACKEND mode, backends don't inherit the number of fast-path
179 * groups we calculated before setting the shmem up, so recalculate it.
181 InitializeFastPathLocks();
183 CreateOrAttachShmemStructs();
186 * Now give loadable modules a chance to set up their shmem allocations
188 if (shmem_startup_hook
)
189 shmem_startup_hook();
194 * CreateSharedMemoryAndSemaphores
195 * Creates and initializes shared memory and semaphores.
198 CreateSharedMemoryAndSemaphores(void)
201 PGShmemHeader
*seghdr
;
205 Assert(!IsUnderPostmaster
);
207 /* Compute the size of the shared-memory block */
208 size
= CalculateShmemSize(&numSemas
);
209 elog(DEBUG3
, "invoking IpcMemoryCreate(size=%zu)", size
);
212 * Create the shmem segment
214 seghdr
= PGSharedMemoryCreate(size
, &shim
);
217 * Make sure that huge pages are never reported as "unknown" while the
220 Assert(strcmp("unknown",
221 GetConfigOption("huge_pages_status", false, false)) != 0);
223 InitShmemAccess(seghdr
);
228 PGReserveSemaphores(numSemas
);
231 * Set up shared memory allocation mechanism
233 InitShmemAllocation();
235 /* Initialize subsystems */
236 CreateOrAttachShmemStructs();
238 /* Initialize dynamic shared memory facilities. */
239 dsm_postmaster_startup(shim
);
242 * Now give loadable modules a chance to set up their shmem allocations
244 if (shmem_startup_hook
)
245 shmem_startup_hook();
249 * Initialize various subsystems, setting up their data structures in
252 * This is called by the postmaster or by a standalone backend.
253 * It is also called by a backend forked from the postmaster in the
254 * EXEC_BACKEND case. In the latter case, the shared memory segment
255 * already exists and has been physically attached to, but we have to
256 * initialize pointers in local memory that reference the shared structures,
257 * because we didn't inherit the correct pointer values from the postmaster
258 * as we do in the fork() scenario. The easiest way to do that is to run
259 * through the same code as before. (Note that the called routines mostly
260 * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
261 * This is a bit code-wasteful and could be cleaned up.)
264 CreateOrAttachShmemStructs(void)
267 * Now initialize LWLocks, which do shared memory allocation and are
268 * needed for InitShmemIndex.
273 * Set up shmem.c index hashtable
278 DSMRegistryShmemInit();
281 * Set up xlog, clog, and buffers
285 XLogPrefetchShmemInit();
286 XLogRecoveryShmemInit();
290 MultiXactShmemInit();
291 BufferManagerShmemInit();
294 * Set up lock manager
296 LockManagerShmemInit();
299 * Set up predicate lock manager
301 PredicateLockShmemInit();
304 * Set up process table
306 if (!IsUnderPostmaster
)
308 ProcArrayShmemInit();
309 BackendStatusShmemInit();
311 BackgroundWorkerShmemInit();
314 * Set up shared-inval messaging
316 SharedInvalShmemInit();
319 * Set up interprocess signaling mechanisms
322 ProcSignalShmemInit();
323 CheckpointerShmemInit();
324 AutoVacuumShmemInit();
325 ReplicationSlotsShmemInit();
326 ReplicationOriginShmemInit();
329 WalSummarizerShmemInit();
331 ApplyLauncherShmemInit();
335 * Set up other modules that need some shared memory space
341 WaitEventCustomShmemInit();
342 InjectionPointShmemInit();
346 * InitializeShmemGUCs
348 * This function initializes runtime-computed GUCs related to the amount of
349 * shared memory required for the current configuration.
352 InitializeShmemGUCs(void)
361 * Calculate the shared memory size and round up to the nearest megabyte.
363 size_b
= CalculateShmemSize(&num_semas
);
364 size_mb
= add_size(size_b
, (1024 * 1024) - 1) / (1024 * 1024);
365 sprintf(buf
, "%zu", size_mb
);
366 SetConfigOption("shared_memory_size", buf
,
367 PGC_INTERNAL
, PGC_S_DYNAMIC_DEFAULT
);
370 * Calculate the number of huge pages required.
372 GetHugePageSize(&hp_size
, NULL
);
377 hp_required
= add_size(size_b
/ hp_size
, 1);
378 sprintf(buf
, "%zu", hp_required
);
379 SetConfigOption("shared_memory_size_in_huge_pages", buf
,
380 PGC_INTERNAL
, PGC_S_DYNAMIC_DEFAULT
);
383 sprintf(buf
, "%d", num_semas
);
384 SetConfigOption("num_os_semaphores", buf
, PGC_INTERNAL
, PGC_S_DYNAMIC_DEFAULT
);