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 "commands/waitlsn.h"
29 #include "miscadmin.h"
31 #include "postmaster/autovacuum.h"
32 #include "postmaster/bgworker_internals.h"
33 #include "postmaster/bgwriter.h"
34 #include "postmaster/postmaster.h"
35 #include "postmaster/walsummarizer.h"
36 #include "replication/logicallauncher.h"
37 #include "replication/origin.h"
38 #include "replication/slot.h"
39 #include "replication/slotsync.h"
40 #include "replication/walreceiver.h"
41 #include "replication/walsender.h"
42 #include "storage/bufmgr.h"
43 #include "storage/dsm.h"
44 #include "storage/dsm_registry.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 "storage/spin.h"
54 #include "utils/guc.h"
55 #include "utils/injection_point.h"
58 int shared_memory_type
= DEFAULT_SHARED_MEMORY_TYPE
;
60 shmem_startup_hook_type shmem_startup_hook
= NULL
;
62 static Size total_addin_request
= 0;
64 static void CreateOrAttachShmemStructs(void);
67 * RequestAddinShmemSpace
68 * Request that extra shmem space be allocated for use by
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.
76 RequestAddinShmemSpace(Size size
)
78 if (!process_shmem_requests_in_progress
)
79 elog(FATAL
, "cannot request additional shared memory outside shmem_request_hook");
80 total_addin_request
= add_size(total_addin_request
, size
);
85 * Calculates the amount of shared memory and number of semaphores needed.
87 * If num_semaphores is not NULL, it will be set to the number of semaphores
91 CalculateShmemSize(int *num_semaphores
)
96 /* Compute number of semaphores we'll need */
97 numSemas
= ProcGlobalSemas();
99 /* Return the number of semaphores if requested by the caller */
101 *num_semaphores
= numSemas
;
104 * Size of the Postgres shared-memory block is estimated via moderately-
105 * accurate estimates for the big hogs, plus 100K for the stuff that's too
106 * small to bother with estimating.
108 * We take some care to ensure that the total size request doesn't
109 * overflow size_t. If this gets through, we don't need to be so careful
110 * during the actual allocation phase.
113 size
= add_size(size
, PGSemaphoreShmemSize(numSemas
));
114 size
= add_size(size
, hash_estimate_size(SHMEM_INDEX_SIZE
,
115 sizeof(ShmemIndexEnt
)));
116 size
= add_size(size
, dsm_estimate_size());
117 size
= add_size(size
, DSMRegistryShmemSize());
118 size
= add_size(size
, BufferManagerShmemSize());
119 size
= add_size(size
, LockManagerShmemSize());
120 size
= add_size(size
, PredicateLockShmemSize());
121 size
= add_size(size
, ProcGlobalShmemSize());
122 size
= add_size(size
, XLogPrefetchShmemSize());
123 size
= add_size(size
, VarsupShmemSize());
124 size
= add_size(size
, XLOGShmemSize());
125 size
= add_size(size
, XLogRecoveryShmemSize());
126 size
= add_size(size
, CLOGShmemSize());
127 size
= add_size(size
, CommitTsShmemSize());
128 size
= add_size(size
, SUBTRANSShmemSize());
129 size
= add_size(size
, TwoPhaseShmemSize());
130 size
= add_size(size
, BackgroundWorkerShmemSize());
131 size
= add_size(size
, MultiXactShmemSize());
132 size
= add_size(size
, LWLockShmemSize());
133 size
= add_size(size
, ProcArrayShmemSize());
134 size
= add_size(size
, BackendStatusShmemSize());
135 size
= add_size(size
, SharedInvalShmemSize());
136 size
= add_size(size
, PMSignalShmemSize());
137 size
= add_size(size
, ProcSignalShmemSize());
138 size
= add_size(size
, CheckpointerShmemSize());
139 size
= add_size(size
, AutoVacuumShmemSize());
140 size
= add_size(size
, ReplicationSlotsShmemSize());
141 size
= add_size(size
, ReplicationOriginShmemSize());
142 size
= add_size(size
, WalSndShmemSize());
143 size
= add_size(size
, WalRcvShmemSize());
144 size
= add_size(size
, WalSummarizerShmemSize());
145 size
= add_size(size
, PgArchShmemSize());
146 size
= add_size(size
, ApplyLauncherShmemSize());
147 size
= add_size(size
, BTreeShmemSize());
148 size
= add_size(size
, SyncScanShmemSize());
149 size
= add_size(size
, AsyncShmemSize());
150 size
= add_size(size
, StatsShmemSize());
151 size
= add_size(size
, WaitEventCustomShmemSize());
152 size
= add_size(size
, InjectionPointShmemSize());
153 size
= add_size(size
, SlotSyncShmemSize());
154 size
= add_size(size
, WaitLSNShmemSize());
156 /* include additional requested shmem from preload libraries */
157 size
= add_size(size
, total_addin_request
);
159 /* might as well round it off to a multiple of a typical page size */
160 size
= add_size(size
, 8192 - (size
% 8192));
167 * AttachSharedMemoryStructs
168 * Initialize a postmaster child process's access to shared memory
171 * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
175 AttachSharedMemoryStructs(void)
177 /* InitProcess must've been called already */
178 Assert(MyProc
!= NULL
);
179 Assert(IsUnderPostmaster
);
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.
185 InitializeFastPathLocks();
187 CreateOrAttachShmemStructs();
190 * Now give loadable modules a chance to set up their shmem allocations
192 if (shmem_startup_hook
)
193 shmem_startup_hook();
198 * CreateSharedMemoryAndSemaphores
199 * Creates and initializes shared memory and semaphores.
202 CreateSharedMemoryAndSemaphores(void)
205 PGShmemHeader
*seghdr
;
209 Assert(!IsUnderPostmaster
);
211 /* Compute the size of the shared-memory block */
212 size
= CalculateShmemSize(&numSemas
);
213 elog(DEBUG3
, "invoking IpcMemoryCreate(size=%zu)", size
);
216 * Create the shmem segment
218 seghdr
= PGSharedMemoryCreate(size
, &shim
);
221 * Make sure that huge pages are never reported as "unknown" while the
224 Assert(strcmp("unknown",
225 GetConfigOption("huge_pages_status", false, false)) != 0);
227 InitShmemAccess(seghdr
);
232 PGReserveSemaphores(numSemas
);
235 * Set up shared memory allocation mechanism
237 InitShmemAllocation();
239 /* Initialize subsystems */
240 CreateOrAttachShmemStructs();
242 /* Initialize dynamic shared memory facilities. */
243 dsm_postmaster_startup(shim
);
246 * Now give loadable modules a chance to set up their shmem allocations
248 if (shmem_startup_hook
)
249 shmem_startup_hook();
253 * Initialize various subsystems, setting up their data structures in
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.)
268 CreateOrAttachShmemStructs(void)
271 * Now initialize LWLocks, which do shared memory allocation and are
272 * needed for InitShmemIndex.
277 * Set up shmem.c index hashtable
282 DSMRegistryShmemInit();
285 * Set up xlog, clog, and buffers
289 XLogPrefetchShmemInit();
290 XLogRecoveryShmemInit();
294 MultiXactShmemInit();
295 BufferManagerShmemInit();
298 * Set up lock manager
300 LockManagerShmemInit();
303 * Set up predicate lock manager
305 PredicateLockShmemInit();
308 * Set up process table
310 if (!IsUnderPostmaster
)
312 ProcArrayShmemInit();
313 BackendStatusShmemInit();
315 BackgroundWorkerShmemInit();
318 * Set up shared-inval messaging
320 SharedInvalShmemInit();
323 * Set up interprocess signaling mechanisms
326 ProcSignalShmemInit();
327 CheckpointerShmemInit();
328 AutoVacuumShmemInit();
329 ReplicationSlotsShmemInit();
330 ReplicationOriginShmemInit();
333 WalSummarizerShmemInit();
335 ApplyLauncherShmemInit();
339 * Set up other modules that need some shared memory space
345 WaitEventCustomShmemInit();
346 InjectionPointShmemInit();
351 * InitializeShmemGUCs
353 * This function initializes runtime-computed GUCs related to the amount of
354 * shared memory required for the current configuration.
357 InitializeShmemGUCs(void)
366 * Calculate the shared memory size and round up to the nearest megabyte.
368 size_b
= CalculateShmemSize(&num_semas
);
369 size_mb
= add_size(size_b
, (1024 * 1024) - 1) / (1024 * 1024);
370 sprintf(buf
, "%zu", size_mb
);
371 SetConfigOption("shared_memory_size", buf
,
372 PGC_INTERNAL
, PGC_S_DYNAMIC_DEFAULT
);
375 * Calculate the number of huge pages required.
377 GetHugePageSize(&hp_size
, NULL
);
382 hp_required
= add_size(size_b
/ hp_size
, 1);
383 sprintf(buf
, "%zu", hp_required
);
384 SetConfigOption("shared_memory_size_in_huge_pages", buf
,
385 PGC_INTERNAL
, PGC_S_DYNAMIC_DEFAULT
);
388 sprintf(buf
, "%d", num_semas
);
389 SetConfigOption("num_os_semaphores", buf
, PGC_INTERNAL
, PGC_S_DYNAMIC_DEFAULT
);