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-2023, 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/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"
33 #include "postmaster/postmaster.h"
35 #include "replication/origin.h"
36 #include "replication/slot.h"
38 #include "replication/walsender.h"
39 #include "storage/bufmgr.h"
40 #include "storage/dsm.h"
41 #include "storage/ipc.h"
42 #include "storage/pg_shmem.h"
43 #include "storage/pmsignal.h"
44 #include "storage/predicate.h"
45 #include "storage/proc.h"
46 #include "storage/procarray.h"
47 #include "storage/procsignal.h"
48 #include "storage/sinvaladt.h"
49 #include "storage/spin.h"
50 #include "utils/guc.h"
51 #include "utils/snapmgr.h"
52 #include "utils/wait_event.h"
53 
54 /* GUCs */
56 
58 
60 
61 /*
62  * RequestAddinShmemSpace
63  * Request that extra shmem space be allocated for use by
64  * a loadable module.
65  *
66  * This may only be called via the shmem_request_hook of a library that is
67  * loaded into the postmaster via shared_preload_libraries. Calls from
68  * elsewhere will fail.
69  */
70 void
72 {
74  elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
76 }
77 
78 /*
79  * CalculateShmemSize
80  * Calculates the amount of shared memory and number of semaphores needed.
81  *
82  * If num_semaphores is not NULL, it will be set to the number of semaphores
83  * required.
84  */
85 Size
86 CalculateShmemSize(int *num_semaphores)
87 {
88  Size size;
89  int numSemas;
90 
91  /* Compute number of semaphores we'll need */
92  numSemas = ProcGlobalSemas();
93  numSemas += SpinlockSemas();
94 
95  /* Return the number of semaphores if requested by the caller */
96  if (num_semaphores)
97  *num_semaphores = numSemas;
98 
99  /*
100  * Size of the Postgres shared-memory block is estimated via moderately-
101  * accurate estimates for the big hogs, plus 100K for the stuff that's too
102  * small to bother with estimating.
103  *
104  * We take some care to ensure that the total size request doesn't
105  * overflow size_t. If this gets through, we don't need to be so careful
106  * during the actual allocation phase.
107  */
108  size = 100000;
109  size = add_size(size, PGSemaphoreShmemSize(numSemas));
110  size = add_size(size, SpinlockSemaSize());
112  sizeof(ShmemIndexEnt)));
113  size = add_size(size, dsm_estimate_size());
114  size = add_size(size, BufferShmemSize());
115  size = add_size(size, LockShmemSize());
116  size = add_size(size, PredicateLockShmemSize());
117  size = add_size(size, ProcGlobalShmemSize());
118  size = add_size(size, XLogPrefetchShmemSize());
119  size = add_size(size, XLOGShmemSize());
120  size = add_size(size, XLogRecoveryShmemSize());
121  size = add_size(size, CLOGShmemSize());
122  size = add_size(size, CommitTsShmemSize());
123  size = add_size(size, SUBTRANSShmemSize());
124  size = add_size(size, TwoPhaseShmemSize());
125  size = add_size(size, BackgroundWorkerShmemSize());
126  size = add_size(size, MultiXactShmemSize());
127  size = add_size(size, LWLockShmemSize());
128  size = add_size(size, ProcArrayShmemSize());
129  size = add_size(size, BackendStatusShmemSize());
130  size = add_size(size, SInvalShmemSize());
131  size = add_size(size, PMSignalShmemSize());
132  size = add_size(size, ProcSignalShmemSize());
133  size = add_size(size, CheckpointerShmemSize());
134  size = add_size(size, AutoVacuumShmemSize());
135  size = add_size(size, ReplicationSlotsShmemSize());
136  size = add_size(size, ReplicationOriginShmemSize());
137  size = add_size(size, WalSndShmemSize());
138  size = add_size(size, WalRcvShmemSize());
139  size = add_size(size, PgArchShmemSize());
140  size = add_size(size, ApplyLauncherShmemSize());
141  size = add_size(size, BTreeShmemSize());
142  size = add_size(size, SyncScanShmemSize());
143  size = add_size(size, AsyncShmemSize());
144  size = add_size(size, StatsShmemSize());
145  size = add_size(size, WaitEventExtensionShmemSize());
146 #ifdef EXEC_BACKEND
147  size = add_size(size, ShmemBackendArraySize());
148 #endif
149 
150  /* include additional requested shmem from preload libraries */
151  size = add_size(size, total_addin_request);
152 
153  /* might as well round it off to a multiple of a typical page size */
154  size = add_size(size, 8192 - (size % 8192));
155 
156  return size;
157 }
158 
159 /*
160  * CreateSharedMemoryAndSemaphores
161  * Creates and initializes shared memory and semaphores.
162  *
163  * This is called by the postmaster or by a standalone backend.
164  * It is also called by a backend forked from the postmaster in the
165  * EXEC_BACKEND case. In the latter case, the shared memory segment
166  * already exists and has been physically attached to, but we have to
167  * initialize pointers in local memory that reference the shared structures,
168  * because we didn't inherit the correct pointer values from the postmaster
169  * as we do in the fork() scenario. The easiest way to do that is to run
170  * through the same code as before. (Note that the called routines mostly
171  * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
172  * This is a bit code-wasteful and could be cleaned up.)
173  */
174 void
176 {
177  PGShmemHeader *shim = NULL;
178 
179  if (!IsUnderPostmaster)
180  {
181  PGShmemHeader *seghdr;
182  Size size;
183  int numSemas;
184 
185  /* Compute the size of the shared-memory block */
186  size = CalculateShmemSize(&numSemas);
187  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
188 
189  /*
190  * Create the shmem segment
191  */
192  seghdr = PGSharedMemoryCreate(size, &shim);
193 
194  /*
195  * Make sure that huge pages are never reported as "unknown" while the
196  * server is running.
197  */
198  Assert(strcmp("unknown",
199  GetConfigOption("huge_pages_status", false, false)) != 0);
200 
201  InitShmemAccess(seghdr);
202 
203  /*
204  * Create semaphores
205  */
206  PGReserveSemaphores(numSemas);
207 
208  /*
209  * If spinlocks are disabled, initialize emulation layer (which
210  * depends on semaphores, so the order is important here).
211  */
212 #ifndef HAVE_SPINLOCKS
214 #endif
215  }
216  else
217  {
218  /*
219  * We are reattaching to an existing shared memory segment. This
220  * should only be reached in the EXEC_BACKEND case.
221  */
222 #ifndef EXEC_BACKEND
223  elog(PANIC, "should be attached to shared memory already");
224 #endif
225  }
226 
227  /*
228  * Set up shared memory allocation mechanism
229  */
230  if (!IsUnderPostmaster)
232 
233  /*
234  * Now initialize LWLocks, which do shared memory allocation and are
235  * needed for InitShmemIndex.
236  */
237  CreateLWLocks();
238 
239  /*
240  * Set up shmem.c index hashtable
241  */
242  InitShmemIndex();
243 
244  dsm_shmem_init();
245 
246  /*
247  * Set up xlog, clog, and buffers
248  */
249  XLOGShmemInit();
252  CLOGShmemInit();
256  InitBufferPool();
257 
258  /*
259  * Set up lock manager
260  */
261  InitLocks();
262 
263  /*
264  * Set up predicate lock manager
265  */
267 
268  /*
269  * Set up process table
270  */
271  if (!IsUnderPostmaster)
272  InitProcGlobal();
277 
278  /*
279  * Set up shared-inval messaging
280  */
282 
283  /*
284  * Set up interprocess signaling mechanisms
285  */
292  WalSndShmemInit();
293  WalRcvShmemInit();
294  PgArchShmemInit();
296 
297  /*
298  * Set up other modules that need some shared memory space
299  */
300  BTreeShmemInit();
302  AsyncShmemInit();
303  StatsShmemInit();
305 
306 #ifdef EXEC_BACKEND
307 
308  /*
309  * Alloc the win32 shared backend array
310  */
311  if (!IsUnderPostmaster)
312  ShmemBackendArrayAllocation();
313 #endif
314 
315  /* Initialize dynamic shared memory facilities. */
316  if (!IsUnderPostmaster)
318 
319  /*
320  * Now give loadable modules a chance to set up their shmem allocations
321  */
322  if (shmem_startup_hook)
324 }
325 
326 /*
327  * InitializeShmemGUCs
328  *
329  * This function initializes runtime-computed GUCs related to the amount of
330  * shared memory required for the current configuration.
331  */
332 void
334 {
335  char buf[64];
336  Size size_b;
337  Size size_mb;
338  Size hp_size;
339 
340  /*
341  * Calculate the shared memory size and round up to the nearest megabyte.
342  */
343  size_b = CalculateShmemSize(NULL);
344  size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
345  sprintf(buf, "%zu", size_mb);
346  SetConfigOption("shared_memory_size", buf,
348 
349  /*
350  * Calculate the number of huge pages required.
351  */
352  GetHugePageSize(&hp_size, NULL);
353  if (hp_size != 0)
354  {
355  Size hp_required;
356 
357  hp_required = add_size(size_b / hp_size, 1);
358  sprintf(buf, "%zu", hp_required);
359  SetConfigOption("shared_memory_size_in_huge_pages", buf,
361  }
362 }
Size AsyncShmemSize(void)
Definition: async.c:516
void AsyncShmemInit(void)
Definition: async.c:533
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3408
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3427
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:594
void CheckpointerShmemInit(void)
Definition: checkpointer.c:887
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:868
void CLOGShmemInit(void)
Definition: clog.c:695
Size CLOGShmemSize(void)
Definition: clog.c:689
Size CommitTsShmemSize(void)
Definition: commit_ts.c:507
void CommitTsShmemInit(void)
Definition: commit_ts.c:518
size_t dsm_estimate_size(void)
Definition: dsm.c:443
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:150
void dsm_shmem_init(void)
Definition: dsm.c:452
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 PANIC
Definition: elog.h:42
bool IsUnderPostmaster
Definition: globals.c:113
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4176
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:4200
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:110
@ PGC_INTERNAL
Definition: guc.h:69
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:71
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:86
void InitializeShmemGUCs(void)
Definition: ipci.c:333
void CreateSharedMemoryAndSemaphores(void)
Definition: ipci.c:175
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:905
void ApplyLauncherShmemInit(void)
Definition: launcher.c:953
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:474
Size LWLockShmemSize(void)
Definition: lwlock.c:444
bool process_shmem_requests_in_progress
Definition: miscinit.c:1768
void MultiXactShmemInit(void)
Definition: multixact.c:1844
Size MultiXactShmemSize(void)
Definition: multixact.c:1827
void BTreeShmemInit(void)
Definition: nbtutils.c:2083
Size BTreeShmemSize(void)
Definition: nbtutils.c:2070
Size ReplicationOriginShmemSize(void)
Definition: origin.c:506
void ReplicationOriginShmemInit(void)
Definition: origin.c:526
#define DEFAULT_SHARED_MEMORY_TYPE
Definition: pg_shmem.h:75
static char * buf
Definition: pg_test_fsync.c:67
Size PgArchShmemSize(void)
Definition: pgarch.c:152
void PgArchShmemInit(void)
Definition: pgarch.c:163
void StatsShmemInit(void)
Definition: pgstat_shmem.c:140
Size StatsShmemSize(void)
Definition: pgstat_shmem.c:126
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:1089
Size PredicateLockShmemSize(void)
Definition: predicate.c:1291
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:292
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
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:203
void CreateSharedInvalidationState(void)
Definition: sinvaladt.c:227
void ReplicationSlotsShmemInit(void)
Definition: slot.c:136
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:118
Size SpinlockSemaSize(void)
Definition: spin.c:55
void SpinlockSemaInit(void)
Definition: spin.c:77
int SpinlockSemas(void)
Definition: spin.c:64
Size ProcGlobalShmemSize(void)
Definition: proc.c:100
int ProcGlobalSemas(void)
Definition: proc.c:122
void InitProcGlobal(void)
Definition: proc.c:157
void SUBTRANSShmemInit(void)
Definition: subtrans.c:191
Size SUBTRANSShmemSize(void)
Definition: subtrans.c:185
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:241
void TwoPhaseShmemInit(void)
Definition: twophase.c:257
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:3289
Size WalSndShmemSize(void)
Definition: walsender.c:3277
void XLOGShmemInit(void)
Definition: xlog.c:4560
Size XLOGShmemSize(void)
Definition: xlog.c:4510
size_t XLogPrefetchShmemSize(void)
void XLogPrefetchShmemInit(void)
Size XLogRecoveryShmemSize(void)
Definition: xlogrecovery.c:447
void XLogRecoveryShmemInit(void)
Definition: xlogrecovery.c:458