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/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"
29 #include "pgstat.h"
30 #include "postmaster/autovacuum.h"
32 #include "postmaster/bgwriter.h"
35 #include "replication/origin.h"
36 #include "replication/slot.h"
37 #include "replication/slotsync.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"
53 
54 /* GUCs */
56 
58 
60 
61 static void CreateOrAttachShmemStructs(void);
62 
63 /*
64  * RequestAddinShmemSpace
65  * Request that extra shmem space be allocated for use by
66  * a loadable module.
67  *
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.
71  */
72 void
74 {
76  elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
78 }
79 
80 /*
81  * CalculateShmemSize
82  * Calculates the amount of shared memory and number of semaphores needed.
83  *
84  * If num_semaphores is not NULL, it will be set to the number of semaphores
85  * required.
86  */
87 Size
88 CalculateShmemSize(int *num_semaphores)
89 {
90  Size size;
91  int numSemas;
92 
93  /* Compute number of semaphores we'll need */
94  numSemas = ProcGlobalSemas();
95 
96  /* Return the number of semaphores if requested by the caller */
97  if (num_semaphores)
98  *num_semaphores = numSemas;
99 
100  /*
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.
104  *
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.
108  */
109  size = 100000;
110  size = add_size(size, PGSemaphoreShmemSize(numSemas));
112  sizeof(ShmemIndexEnt)));
151 
152  /* include additional requested shmem from preload libraries */
154 
155  /* might as well round it off to a multiple of a typical page size */
156  size = add_size(size, 8192 - (size % 8192));
157 
158  return size;
159 }
160 
161 #ifdef EXEC_BACKEND
162 /*
163  * AttachSharedMemoryStructs
164  * Initialize a postmaster child process's access to shared memory
165  * structures.
166  *
167  * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
168  * isn't needed.
169  */
170 void
171 AttachSharedMemoryStructs(void)
172 {
173  /* InitProcess must've been called already */
174  Assert(MyProc != NULL);
176 
177  /*
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.
180  */
182 
184 
185  /*
186  * Now give loadable modules a chance to set up their shmem allocations
187  */
188  if (shmem_startup_hook)
190 }
191 #endif
192 
193 /*
194  * CreateSharedMemoryAndSemaphores
195  * Creates and initializes shared memory and semaphores.
196  */
197 void
199 {
200  PGShmemHeader *shim;
201  PGShmemHeader *seghdr;
202  Size size;
203  int numSemas;
204 
206 
207  /* Compute the size of the shared-memory block */
208  size = CalculateShmemSize(&numSemas);
209  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
210 
211  /*
212  * Create the shmem segment
213  */
214  seghdr = PGSharedMemoryCreate(size, &shim);
215 
216  /*
217  * Make sure that huge pages are never reported as "unknown" while the
218  * server is running.
219  */
220  Assert(strcmp("unknown",
221  GetConfigOption("huge_pages_status", false, false)) != 0);
222 
223  InitShmemAccess(seghdr);
224 
225  /*
226  * Create semaphores
227  */
228  PGReserveSemaphores(numSemas);
229 
230  /*
231  * Set up shared memory allocation mechanism
232  */
234 
235  /* Initialize subsystems */
237 
238  /* Initialize dynamic shared memory facilities. */
240 
241  /*
242  * Now give loadable modules a chance to set up their shmem allocations
243  */
244  if (shmem_startup_hook)
246 }
247 
248 /*
249  * Initialize various subsystems, setting up their data structures in
250  * shared memory.
251  *
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.)
262  */
263 static void
265 {
266  /*
267  * Now initialize LWLocks, which do shared memory allocation and are
268  * needed for InitShmemIndex.
269  */
270  CreateLWLocks();
271 
272  /*
273  * Set up shmem.c index hashtable
274  */
275  InitShmemIndex();
276 
277  dsm_shmem_init();
279 
280  /*
281  * Set up xlog, clog, and buffers
282  */
283  VarsupShmemInit();
284  XLOGShmemInit();
287  CLOGShmemInit();
292 
293  /*
294  * Set up lock manager
295  */
297 
298  /*
299  * Set up predicate lock manager
300  */
302 
303  /*
304  * Set up process table
305  */
306  if (!IsUnderPostmaster)
307  InitProcGlobal();
312 
313  /*
314  * Set up shared-inval messaging
315  */
317 
318  /*
319  * Set up interprocess signaling mechanisms
320  */
327  WalSndShmemInit();
328  WalRcvShmemInit();
330  PgArchShmemInit();
333 
334  /*
335  * Set up other modules that need some shared memory space
336  */
337  BTreeShmemInit();
339  AsyncShmemInit();
340  StatsShmemInit();
343 }
344 
345 /*
346  * InitializeShmemGUCs
347  *
348  * This function initializes runtime-computed GUCs related to the amount of
349  * shared memory required for the current configuration.
350  */
351 void
353 {
354  char buf[64];
355  Size size_b;
356  Size size_mb;
357  Size hp_size;
358  int num_semas;
359 
360  /*
361  * Calculate the shared memory size and round up to the nearest megabyte.
362  */
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,
368 
369  /*
370  * Calculate the number of huge pages required.
371  */
372  GetHugePageSize(&hp_size, NULL);
373  if (hp_size != 0)
374  {
375  Size hp_required;
376 
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,
381  }
382 
383  sprintf(buf, "%d", num_semas);
384  SetConfigOption("num_os_semaphores", buf, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
385 }
Size AsyncShmemSize(void)
Definition: async.c:485
void AsyncShmemInit(void)
Definition: async.c:502
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3267
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3286
void BackendStatusShmemInit(void)
Size BackendStatusShmemSize(void)
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:162
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:146
Size BufferManagerShmemSize(void)
Definition: buf_init.c:159
void BufferManagerShmemInit(void)
Definition: buf_init.c:67
#define Assert(condition)
Definition: c.h:812
size_t Size
Definition: c.h:559
void CheckpointerShmemInit(void)
Definition: checkpointer.c:909
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:890
void CLOGShmemInit(void)
Definition: clog.c:787
Size CLOGShmemSize(void)
Definition: clog.c:781
Size CommitTsShmemSize(void)
Definition: commit_ts.c:519
void CommitTsShmemInit(void)
Definition: commit_ts.c:530
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:69
Size DSMRegistryShmemSize(void)
Definition: dsm_registry.c:63
Size hash_estimate_size(long 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:225
bool IsUnderPostmaster
Definition: globals.c:119
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4332
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:4355
@ 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:59
int shared_memory_type
Definition: ipci.c:55
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:57
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:73
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:88
void InitializeShmemGUCs(void)
Definition: ipci.c:352
static void CreateOrAttachShmemStructs(void)
Definition: ipci.c:264
void CreateSharedMemoryAndSemaphores(void)
Definition: ipci.c:198
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:905
void ApplyLauncherShmemInit(void)
Definition: launcher.c:960
Size LockManagerShmemSize(void)
Definition: lock.c:3663
void LockManagerShmemInit(void)
Definition: lock.c:438
void CreateLWLocks(void)
Definition: lwlock.c:450
Size LWLockShmemSize(void)
Definition: lwlock.c:420
bool process_shmem_requests_in_progress
Definition: miscinit.c:1843
void MultiXactShmemInit(void)
Definition: multixact.c:1964
Size MultiXactShmemSize(void)
Definition: multixact.c:1947
void BTreeShmemInit(void)
Definition: nbtutils.c:4535
Size BTreeShmemSize(void)
Definition: nbtutils.c:4522
Size ReplicationOriginShmemSize(void)
Definition: origin.c:510
void ReplicationOriginShmemInit(void)
Definition: origin.c:530
#define DEFAULT_SHARED_MEMORY_TYPE
Definition: pg_shmem.h:75
static char * buf
Definition: pg_test_fsync.c:72
Size PgArchShmemSize(void)
Definition: pgarch.c:155
void PgArchShmemInit(void)
Definition: pgarch.c:166
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:240
Size PGSemaphoreShmemSize(int maxSemas)
Definition: posix_sema.c:165
void PGReserveSemaphores(int maxSemas)
Definition: posix_sema.c:196
void InitializeFastPathLocks(void)
Definition: postinit.c:575
void PredicateLockShmemInit(void)
Definition: predicate.c:1145
Size PredicateLockShmemSize(void)
Definition: predicate.c:1347
Size ProcArrayShmemSize(void)
Definition: procarray.c:376
void ProcArrayShmemInit(void)
Definition: procarray.c:418
void ProcSignalShmemInit(void)
Definition: procsignal.c:130
Size ProcSignalShmemSize(void)
Definition: procsignal.c:116
void InitShmemIndex(void)
Definition: shmem.c:278
void InitShmemAccess(PGShmemHeader *seghdr)
Definition: shmem.c:97
Size add_size(Size s1, Size s2)
Definition: shmem.c:488
void InitShmemAllocation(void)
Definition: shmem.c:110
#define SHMEM_INDEX_SIZE
Definition: shmem.h:51
Size SharedInvalShmemSize(void)
Definition: sinvaladt.c:217
void SharedInvalShmemInit(void)
Definition: sinvaladt.c:233
static pg_noinline void Size size
Definition: slab.c:607
void ReplicationSlotsShmemInit(void)
Definition: slot.c:189
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:171
void SlotSyncShmemInit(void)
Definition: slotsync.c:1667
Size SlotSyncShmemSize(void)
Definition: slotsync.c:1658
PGPROC * MyProc
Definition: proc.c:66
Size ProcGlobalShmemSize(void)
Definition: proc.c:97
int ProcGlobalSemas(void)
Definition: proc.c:130
void InitProcGlobal(void)
Definition: proc.c:165
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:700
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:479
Size TwoPhaseShmemSize(void)
Definition: twophase.c:237
void TwoPhaseShmemInit(void)
Definition: twophase.c:253
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:3593
Size WalSndShmemSize(void)
Definition: walsender.c:3581
Size WalSummarizerShmemSize(void)
void WalSummarizerShmemInit(void)
void XLOGShmemInit(void)
Definition: xlog.c:4918
Size XLOGShmemSize(void)
Definition: xlog.c:4868
size_t XLogPrefetchShmemSize(void)
void XLogPrefetchShmemInit(void)
Size XLogRecoveryShmemSize(void)
Definition: xlogrecovery.c:447
void XLogRecoveryShmemInit(void)
Definition: xlogrecovery.c:458