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-2024, 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/heapam.h"
20 #include "access/multixact.h"
21 #include "access/nbtree.h"
22 #include "access/subtrans.h"
23 #include "access/syncscan.h"
24 #include "access/transam.h"
25 #include "access/twophase.h"
26 #include "access/xlogprefetcher.h"
27 #include "access/xlogrecovery.h"
28 #include "commands/async.h"
29 #include "miscadmin.h"
30 #include "pgstat.h"
31 #include "postmaster/autovacuum.h"
33 #include "postmaster/bgwriter.h"
34 #include "postmaster/postmaster.h"
37 #include "replication/origin.h"
38 #include "replication/slot.h"
39 #include "replication/slotsync.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"
56 #include "utils/snapmgr.h"
57 #include "utils/wait_event.h"
58 
59 /* GUCs */
61 
63 
65 
66 static void CreateOrAttachShmemStructs(void);
67 
68 /*
69  * RequestAddinShmemSpace
70  * Request that extra shmem space be allocated for use by
71  * a loadable module.
72  *
73  * This may only be called via the shmem_request_hook of a library that is
74  * loaded into the postmaster via shared_preload_libraries. Calls from
75  * elsewhere will fail.
76  */
77 void
79 {
81  elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
83 }
84 
85 /*
86  * CalculateShmemSize
87  * Calculates the amount of shared memory and number of semaphores needed.
88  *
89  * If num_semaphores is not NULL, it will be set to the number of semaphores
90  * required.
91  */
92 Size
93 CalculateShmemSize(int *num_semaphores)
94 {
95  Size size;
96  int numSemas;
97 
98  /* Compute number of semaphores we'll need */
99  numSemas = ProcGlobalSemas();
100  numSemas += SpinlockSemas();
101 
102  /* Return the number of semaphores if requested by the caller */
103  if (num_semaphores)
104  *num_semaphores = numSemas;
105 
106  /*
107  * Size of the Postgres shared-memory block is estimated via moderately-
108  * accurate estimates for the big hogs, plus 100K for the stuff that's too
109  * small to bother with estimating.
110  *
111  * We take some care to ensure that the total size request doesn't
112  * overflow size_t. If this gets through, we don't need to be so careful
113  * during the actual allocation phase.
114  */
115  size = 100000;
116  size = add_size(size, PGSemaphoreShmemSize(numSemas));
119  sizeof(ShmemIndexEnt)));
158 #ifdef EXEC_BACKEND
159  size = add_size(size, ShmemBackendArraySize());
160 #endif
161 
162  /* include additional requested shmem from preload libraries */
164 
165  /* might as well round it off to a multiple of a typical page size */
166  size = add_size(size, 8192 - (size % 8192));
167 
168  return size;
169 }
170 
171 #ifdef EXEC_BACKEND
172 /*
173  * AttachSharedMemoryStructs
174  * Initialize a postmaster child process's access to shared memory
175  * structures.
176  *
177  * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
178  * isn't needed.
179  */
180 void
181 AttachSharedMemoryStructs(void)
182 {
183  /* InitProcess must've been called already */
184  Assert(MyProc != NULL);
186 
188 
189  /*
190  * Now give loadable modules a chance to set up their shmem allocations
191  */
192  if (shmem_startup_hook)
194 }
195 #endif
196 
197 /*
198  * CreateSharedMemoryAndSemaphores
199  * Creates and initializes shared memory and semaphores.
200  */
201 void
203 {
204  PGShmemHeader *shim;
205  PGShmemHeader *seghdr;
206  Size size;
207  int numSemas;
208 
210 
211  /* Compute the size of the shared-memory block */
212  size = CalculateShmemSize(&numSemas);
213  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
214 
215  /*
216  * Create the shmem segment
217  */
218  seghdr = PGSharedMemoryCreate(size, &shim);
219 
220  /*
221  * Make sure that huge pages are never reported as "unknown" while the
222  * server is running.
223  */
224  Assert(strcmp("unknown",
225  GetConfigOption("huge_pages_status", false, false)) != 0);
226 
227  InitShmemAccess(seghdr);
228 
229  /*
230  * Create semaphores
231  */
232  PGReserveSemaphores(numSemas);
233 
234  /*
235  * If spinlocks are disabled, initialize emulation layer (which depends on
236  * semaphores, so the order is important here).
237  */
238 #ifndef HAVE_SPINLOCKS
240 #endif
241 
242  /*
243  * Set up shared memory allocation mechanism
244  */
246 
247  /* Initialize subsystems */
249 
250 #ifdef EXEC_BACKEND
251 
252  /*
253  * Alloc the win32 shared backend array
254  */
255  ShmemBackendArrayAllocation();
256 #endif
257 
258  /* Initialize dynamic shared memory facilities. */
260 
261  /*
262  * Now give loadable modules a chance to set up their shmem allocations
263  */
264  if (shmem_startup_hook)
266 }
267 
268 /*
269  * Initialize various subsystems, setting up their data structures in
270  * shared memory.
271  *
272  * This is called by the postmaster or by a standalone backend.
273  * It is also called by a backend forked from the postmaster in the
274  * EXEC_BACKEND case. In the latter case, the shared memory segment
275  * already exists and has been physically attached to, but we have to
276  * initialize pointers in local memory that reference the shared structures,
277  * because we didn't inherit the correct pointer values from the postmaster
278  * as we do in the fork() scenario. The easiest way to do that is to run
279  * through the same code as before. (Note that the called routines mostly
280  * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
281  * This is a bit code-wasteful and could be cleaned up.)
282  */
283 static void
285 {
286  /*
287  * Now initialize LWLocks, which do shared memory allocation and are
288  * needed for InitShmemIndex.
289  */
290  CreateLWLocks();
291 
292  /*
293  * Set up shmem.c index hashtable
294  */
295  InitShmemIndex();
296 
297  dsm_shmem_init();
299 
300  /*
301  * Set up xlog, clog, and buffers
302  */
303  VarsupShmemInit();
304  XLOGShmemInit();
307  CLOGShmemInit();
311  InitBufferPool();
312 
313  /*
314  * Set up lock manager
315  */
316  InitLocks();
317 
318  /*
319  * Set up predicate lock manager
320  */
322 
323  /*
324  * Set up process table
325  */
326  if (!IsUnderPostmaster)
327  InitProcGlobal();
332 
333  /*
334  * Set up shared-inval messaging
335  */
337 
338  /*
339  * Set up interprocess signaling mechanisms
340  */
347  WalSndShmemInit();
348  WalRcvShmemInit();
350  PgArchShmemInit();
353 
354  /*
355  * Set up other modules that need some shared memory space
356  */
357  BTreeShmemInit();
359  AsyncShmemInit();
360  StatsShmemInit();
363 }
364 
365 /*
366  * InitializeShmemGUCs
367  *
368  * This function initializes runtime-computed GUCs related to the amount of
369  * shared memory required for the current configuration.
370  */
371 void
373 {
374  char buf[64];
375  Size size_b;
376  Size size_mb;
377  Size hp_size;
378 
379  /*
380  * Calculate the shared memory size and round up to the nearest megabyte.
381  */
382  size_b = CalculateShmemSize(NULL);
383  size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
384  sprintf(buf, "%zu", size_mb);
385  SetConfigOption("shared_memory_size", buf,
387 
388  /*
389  * Calculate the number of huge pages required.
390  */
391  GetHugePageSize(&hp_size, NULL);
392  if (hp_size != 0)
393  {
394  Size hp_required;
395 
396  hp_required = add_size(size_b / hp_size, 1);
397  sprintf(buf, "%zu", hp_required);
398  SetConfigOption("shared_memory_size_in_huge_pages", buf,
400  }
401 }
Size AsyncShmemSize(void)
Definition: async.c:488
void AsyncShmemInit(void)
Definition: async.c:505
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3378
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3397
void CreateSharedBackendStatus(void)
Size BackendStatusShmemSize(void)
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:164
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:148
Size BufferShmemSize(void)
Definition: buf_init.c:160
void InitBufferPool(void)
Definition: buf_init.c:68
size_t Size
Definition: c.h:592
void CheckpointerShmemInit(void)
Definition: checkpointer.c:892
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:873
void CLOGShmemInit(void)
Definition: clog.c:787
Size CLOGShmemSize(void)
Definition: clog.c:781
Size CommitTsShmemSize(void)
Definition: commit_ts.c:522
void CommitTsShmemInit(void)
Definition: commit_ts.c:533
size_t dsm_estimate_size(void)
Definition: dsm.c:471
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:178
void dsm_shmem_init(void)
Definition: dsm.c:480
void DSMRegistryShmemInit(void)
Definition: dsm_registry.c:69
Size DSMRegistryShmemSize(void)
Definition: dsm_registry.c:63
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:781
#define DEBUG3
Definition: elog.h:28
#define FATAL
Definition: elog.h:41
#define elog(elevel,...)
Definition: elog.h:224
bool IsUnderPostmaster
Definition: globals.c:116
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4256
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:4279
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:110
@ PGC_INTERNAL
Definition: guc.h:69
void InjectionPointShmemInit(void)
Size InjectionPointShmemSize(void)
void(* shmem_startup_hook_type)(void)
Definition: ipc.h:22
static Size total_addin_request
Definition: ipci.c:64
int shared_memory_type
Definition: ipci.c:60
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:62
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:78
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:93
void InitializeShmemGUCs(void)
Definition: ipci.c:372
static void CreateOrAttachShmemStructs(void)
Definition: ipci.c:284
void CreateSharedMemoryAndSemaphores(void)
Definition: ipci.c:202
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:906
void ApplyLauncherShmemInit(void)
Definition: launcher.c:961
Assert(fmt[strlen(fmt) - 1] !='\n')
void InitLocks(void)
Definition: lock.c:393
Size LockShmemSize(void)
Definition: lock.c:3535
void CreateLWLocks(void)
Definition: lwlock.c:454
Size LWLockShmemSize(void)
Definition: lwlock.c:424
bool process_shmem_requests_in_progress
Definition: miscinit.c:1783
void MultiXactShmemInit(void)
Definition: multixact.c:1901
Size MultiXactShmemSize(void)
Definition: multixact.c:1884
void BTreeShmemInit(void)
Definition: nbtutils.c:2135
Size BTreeShmemSize(void)
Definition: nbtutils.c:2122
Size ReplicationOriginShmemSize(void)
Definition: origin.c:507
void ReplicationOriginShmemInit(void)
Definition: origin.c:527
#define DEFAULT_SHARED_MEMORY_TYPE
Definition: pg_shmem.h:75
static char * buf
Definition: pg_test_fsync.c:73
Size PgArchShmemSize(void)
Definition: pgarch.c:149
void PgArchShmemInit(void)
Definition: pgarch.c:160
void StatsShmemInit(void)
Definition: pgstat_shmem.c:141
Size StatsShmemSize(void)
Definition: pgstat_shmem.c:127
Size PMSignalShmemSize(void)
Definition: pmsignal.c:129
void PMSignalShmemInit(void)
Definition: pmsignal.c:144
#define sprintf
Definition: port.h:240
Size PGSemaphoreShmemSize(int maxSemas)
Definition: posix_sema.c:165
void PGReserveSemaphores(int maxSemas)
Definition: posix_sema.c:196
void InitPredicateLocks(void)
Definition: predicate.c:1137
Size PredicateLockShmemSize(void)
Definition: predicate.c:1339
Size ProcArrayShmemSize(void)
Definition: procarray.c:377
void CreateSharedProcArray(void)
Definition: procarray.c:419
void ProcSignalShmemInit(void)
Definition: procsignal.c:126
Size ProcSignalShmemSize(void)
Definition: procsignal.c:112
void InitShmemIndex(void)
Definition: shmem.c:284
Size add_size(Size s1, Size s2)
Definition: shmem.c:494
void InitShmemAllocation(void)
Definition: shmem.c:116
void InitShmemAccess(void *seghdr)
Definition: shmem.c:101
#define SHMEM_INDEX_SIZE
Definition: shmem.h:48
Size SInvalShmemSize(void)
Definition: sinvaladt.c:218
void CreateSharedInvalidationState(void)
Definition: sinvaladt.c:234
static pg_noinline void Size size
Definition: slab.c:607
void ReplicationSlotsShmemInit(void)
Definition: slot.c:152
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:134
void SlotSyncShmemInit(void)
Definition: slotsync.c:1463
Size SlotSyncShmemSize(void)
Definition: slotsync.c:1454
Size SpinlockSemaSize(void)
Definition: spin.c:55
void SpinlockSemaInit(void)
Definition: spin.c:77
int SpinlockSemas(void)
Definition: spin.c:64
PGPROC * MyProc
Definition: proc.c:68
Size ProcGlobalShmemSize(void)
Definition: proc.c:102
int ProcGlobalSemas(void)
Definition: proc.c:124
void InitProcGlobal(void)
Definition: proc.c:159
void SUBTRANSShmemInit(void)
Definition: subtrans.c:220
Size SUBTRANSShmemSize(void)
Definition: subtrans.c:214
void SyncScanShmemInit(void)
Definition: syncscan.c:135
Size SyncScanShmemSize(void)
Definition: syncscan.c:126
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
Definition: sysv_shmem.c:699
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:478
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 WaitEventExtensionShmemSize(void)
Definition: wait_event.c:106
void WaitEventExtensionShmemInit(void)
Definition: wait_event.c:122
void WalRcvShmemInit(void)
Size WalRcvShmemSize(void)
void WalSndShmemInit(void)
Definition: walsender.c:3516
Size WalSndShmemSize(void)
Definition: walsender.c:3504
Size WalSummarizerShmemSize(void)
void WalSummarizerShmemInit(void)
void XLOGShmemInit(void)
Definition: xlog.c:4813
Size XLOGShmemSize(void)
Definition: xlog.c:4763
size_t XLogPrefetchShmemSize(void)
void XLogPrefetchShmemInit(void)
Size XLogRecoveryShmemSize(void)
Definition: xlogrecovery.c:448
void XLogRecoveryShmemInit(void)
Definition: xlogrecovery.c:459