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-2022, 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/snapmgr.h"
51 
52 /* GUCs */
54 
56 
58 
59 /*
60  * RequestAddinShmemSpace
61  * Request that extra shmem space be allocated for use by
62  * a loadable module.
63  *
64  * This may only be called via the shmem_request_hook of a library that is
65  * loaded into the postmaster via shared_preload_libraries. Calls from
66  * elsewhere will fail.
67  */
68 void
70 {
72  elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
74 }
75 
76 /*
77  * CalculateShmemSize
78  * Calculates the amount of shared memory and number of semaphores needed.
79  *
80  * If num_semaphores is not NULL, it will be set to the number of semaphores
81  * required.
82  */
83 Size
84 CalculateShmemSize(int *num_semaphores)
85 {
86  Size size;
87  int numSemas;
88 
89  /* Compute number of semaphores we'll need */
90  numSemas = ProcGlobalSemas();
91  numSemas += SpinlockSemas();
92 
93  /* Return the number of semaphores if requested by the caller */
94  if (num_semaphores)
95  *num_semaphores = numSemas;
96 
97  /*
98  * Size of the Postgres shared-memory block is estimated via moderately-
99  * accurate estimates for the big hogs, plus 100K for the stuff that's too
100  * small to bother with estimating.
101  *
102  * We take some care to ensure that the total size request doesn't
103  * overflow size_t. If this gets through, we don't need to be so careful
104  * during the actual allocation phase.
105  */
106  size = 100000;
107  size = add_size(size, PGSemaphoreShmemSize(numSemas));
108  size = add_size(size, SpinlockSemaSize());
110  sizeof(ShmemIndexEnt)));
111  size = add_size(size, dsm_estimate_size());
112  size = add_size(size, BufferShmemSize());
113  size = add_size(size, LockShmemSize());
114  size = add_size(size, PredicateLockShmemSize());
115  size = add_size(size, ProcGlobalShmemSize());
116  size = add_size(size, XLogPrefetchShmemSize());
117  size = add_size(size, XLOGShmemSize());
118  size = add_size(size, XLogRecoveryShmemSize());
119  size = add_size(size, CLOGShmemSize());
120  size = add_size(size, CommitTsShmemSize());
121  size = add_size(size, SUBTRANSShmemSize());
122  size = add_size(size, TwoPhaseShmemSize());
123  size = add_size(size, BackgroundWorkerShmemSize());
124  size = add_size(size, MultiXactShmemSize());
125  size = add_size(size, LWLockShmemSize());
126  size = add_size(size, ProcArrayShmemSize());
127  size = add_size(size, BackendStatusShmemSize());
128  size = add_size(size, SInvalShmemSize());
129  size = add_size(size, PMSignalShmemSize());
130  size = add_size(size, ProcSignalShmemSize());
131  size = add_size(size, CheckpointerShmemSize());
132  size = add_size(size, AutoVacuumShmemSize());
133  size = add_size(size, ReplicationSlotsShmemSize());
134  size = add_size(size, ReplicationOriginShmemSize());
135  size = add_size(size, WalSndShmemSize());
136  size = add_size(size, WalRcvShmemSize());
137  size = add_size(size, PgArchShmemSize());
138  size = add_size(size, ApplyLauncherShmemSize());
139  size = add_size(size, SnapMgrShmemSize());
140  size = add_size(size, BTreeShmemSize());
141  size = add_size(size, SyncScanShmemSize());
142  size = add_size(size, AsyncShmemSize());
143  size = add_size(size, StatsShmemSize());
144 #ifdef EXEC_BACKEND
145  size = add_size(size, ShmemBackendArraySize());
146 #endif
147 
148  /* include additional requested shmem from preload libraries */
149  size = add_size(size, total_addin_request);
150 
151  /* might as well round it off to a multiple of a typical page size */
152  size = add_size(size, 8192 - (size % 8192));
153 
154  return size;
155 }
156 
157 /*
158  * CreateSharedMemoryAndSemaphores
159  * Creates and initializes shared memory and semaphores.
160  *
161  * This is called by the postmaster or by a standalone backend.
162  * It is also called by a backend forked from the postmaster in the
163  * EXEC_BACKEND case. In the latter case, the shared memory segment
164  * already exists and has been physically attached to, but we have to
165  * initialize pointers in local memory that reference the shared structures,
166  * because we didn't inherit the correct pointer values from the postmaster
167  * as we do in the fork() scenario. The easiest way to do that is to run
168  * through the same code as before. (Note that the called routines mostly
169  * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
170  * This is a bit code-wasteful and could be cleaned up.)
171  */
172 void
174 {
175  PGShmemHeader *shim = NULL;
176 
177  if (!IsUnderPostmaster)
178  {
179  PGShmemHeader *seghdr;
180  Size size;
181  int numSemas;
182 
183  /* Compute the size of the shared-memory block */
184  size = CalculateShmemSize(&numSemas);
185  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
186 
187  /*
188  * Create the shmem segment
189  */
190  seghdr = PGSharedMemoryCreate(size, &shim);
191 
192  InitShmemAccess(seghdr);
193 
194  /*
195  * Create semaphores
196  */
197  PGReserveSemaphores(numSemas);
198 
199  /*
200  * If spinlocks are disabled, initialize emulation layer (which
201  * depends on semaphores, so the order is important here).
202  */
203 #ifndef HAVE_SPINLOCKS
205 #endif
206  }
207  else
208  {
209  /*
210  * We are reattaching to an existing shared memory segment. This
211  * should only be reached in the EXEC_BACKEND case.
212  */
213 #ifndef EXEC_BACKEND
214  elog(PANIC, "should be attached to shared memory already");
215 #endif
216  }
217 
218  /*
219  * Set up shared memory allocation mechanism
220  */
221  if (!IsUnderPostmaster)
223 
224  /*
225  * Now initialize LWLocks, which do shared memory allocation and are
226  * needed for InitShmemIndex.
227  */
228  CreateLWLocks();
229 
230  /*
231  * Set up shmem.c index hashtable
232  */
233  InitShmemIndex();
234 
235  dsm_shmem_init();
236 
237  /*
238  * Set up xlog, clog, and buffers
239  */
240  XLOGShmemInit();
243  CLOGShmemInit();
247  InitBufferPool();
248 
249  /*
250  * Set up lock manager
251  */
252  InitLocks();
253 
254  /*
255  * Set up predicate lock manager
256  */
258 
259  /*
260  * Set up process table
261  */
262  if (!IsUnderPostmaster)
263  InitProcGlobal();
268 
269  /*
270  * Set up shared-inval messaging
271  */
273 
274  /*
275  * Set up interprocess signaling mechanisms
276  */
283  WalSndShmemInit();
284  WalRcvShmemInit();
285  PgArchShmemInit();
287 
288  /*
289  * Set up other modules that need some shared memory space
290  */
291  SnapMgrInit();
292  BTreeShmemInit();
294  AsyncShmemInit();
295  StatsShmemInit();
296 
297 #ifdef EXEC_BACKEND
298 
299  /*
300  * Alloc the win32 shared backend array
301  */
302  if (!IsUnderPostmaster)
303  ShmemBackendArrayAllocation();
304 #endif
305 
306  /* Initialize dynamic shared memory facilities. */
307  if (!IsUnderPostmaster)
309 
310  /*
311  * Now give loadable modules a chance to set up their shmem allocations
312  */
313  if (shmem_startup_hook)
315 }
316 
317 /*
318  * InitializeShmemGUCs
319  *
320  * This function initializes runtime-computed GUCs related to the amount of
321  * shared memory required for the current configuration.
322  */
323 void
325 {
326  char buf[64];
327  Size size_b;
328  Size size_mb;
329  Size hp_size;
330 
331  /*
332  * Calculate the shared memory size and round up to the nearest megabyte.
333  */
334  size_b = CalculateShmemSize(NULL);
335  size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
336  sprintf(buf, "%zu", size_mb);
337  SetConfigOption("shared_memory_size", buf,
339 
340  /*
341  * Calculate the number of huge pages required.
342  */
343  GetHugePageSize(&hp_size, NULL);
344  if (hp_size != 0)
345  {
346  Size hp_required;
347 
348  hp_required = add_size(size_b / hp_size, 1);
349  sprintf(buf, "%zu", hp_required);
350  SetConfigOption("shared_memory_size_in_huge_pages", buf,
352  }
353 }
Size AsyncShmemSize(void)
Definition: async.c:516
void AsyncShmemInit(void)
Definition: async.c:533
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3320
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3339
void CreateSharedBackendStatus(void)
Size BackendStatusShmemSize(void)
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:158
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:142
Size BufferShmemSize(void)
Definition: buf_init.c:157
void InitBufferPool(void)
Definition: buf_init.c:68
size_t Size
Definition: c.h:540
void CheckpointerShmemInit(void)
Definition: checkpointer.c:888
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:869
void CLOGShmemInit(void)
Definition: clog.c:697
Size CLOGShmemSize(void)
Definition: clog.c:691
Size CommitTsShmemSize(void)
Definition: commit_ts.c:528
void CommitTsShmemInit(void)
Definition: commit_ts.c:539
size_t dsm_estimate_size(void)
Definition: dsm.c:444
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:150
void dsm_shmem_init(void)
Definition: dsm.c:453
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:780
#define DEBUG3
Definition: elog.h:22
#define FATAL
Definition: elog.h:35
#define PANIC
Definition: elog.h:36
#define elog(elevel,...)
Definition: elog.h:218
bool IsUnderPostmaster
Definition: globals.c:113
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:8376
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:111
@ PGC_INTERNAL
Definition: guc.h:70
void(* shmem_startup_hook_type)(void)
Definition: ipc.h:22
static Size total_addin_request
Definition: ipci.c:57
int shared_memory_type
Definition: ipci.c:53
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:55
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:69
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:84
void InitializeShmemGUCs(void)
Definition: ipci.c:324
void CreateSharedMemoryAndSemaphores(void)
Definition: ipci.c:173
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:687
void ApplyLauncherShmemInit(void)
Definition: launcher.c:735
void InitLocks(void)
Definition: lock.c:405
Size LockShmemSize(void)
Definition: lock.c:3609
void CreateLWLocks(void)
Definition: lwlock.c:467
Size LWLockShmemSize(void)
Definition: lwlock.c:437
bool process_shmem_requests_in_progress
Definition: miscinit.c:1622
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:490
void ReplicationOriginShmemInit(void)
Definition: origin.c:510
#define DEFAULT_SHARED_MEMORY_TYPE
Definition: pg_shmem.h:74
static char * buf
Definition: pg_test_fsync.c:67
Size PgArchShmemSize(void)
Definition: pgarch.c:149
void PgArchShmemInit(void)
Definition: pgarch.c:160
void StatsShmemInit(void)
Definition: pgstat_shmem.c:140
Size StatsShmemSize(void)
Definition: pgstat_shmem.c:126
Size PMSignalShmemSize(void)
Definition: pmsignal.c:119
void PMSignalShmemInit(void)
Definition: pmsignal.c:134
#define sprintf
Definition: port.h:227
Size PGSemaphoreShmemSize(int maxSemas)
Definition: posix_sema.c:165
void PGReserveSemaphores(int maxSemas)
Definition: posix_sema.c:196
void InitPredicateLocks(void)
Definition: predicate.c:1153
Size PredicateLockShmemSize(void)
Definition: predicate.c:1356
Size ProcArrayShmemSize(void)
Definition: procarray.c:367
void CreateSharedProcArray(void)
Definition: procarray.c:409
void ProcSignalShmemInit(void)
Definition: procsignal.c:125
Size ProcSignalShmemSize(void)
Definition: procsignal.c:111
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:55
Size SInvalShmemSize(void)
Definition: sinvaladt.c:203
void CreateSharedInvalidationState(void)
Definition: sinvaladt.c:227
void ReplicationSlotsShmemInit(void)
Definition: slot.c:134
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:116
void SnapMgrInit(void)
Definition: snapmgr.c:213
Size SnapMgrShmemSize(void)
Definition: snapmgr.c:197
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:102
int ProcGlobalSemas(void)
Definition: proc.c:124
void InitProcGlobal(void)
Definition: proc.c:159
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:678
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:482
Size TwoPhaseShmemSize(void)
Definition: twophase.c:240
void TwoPhaseShmemInit(void)
Definition: twophase.c:256
void WalRcvShmemInit(void)
Size WalRcvShmemSize(void)
void WalSndShmemInit(void)
Definition: walsender.c:3277
Size WalSndShmemSize(void)
Definition: walsender.c:3265
void XLOGShmemInit(void)
Definition: xlog.c:4387
Size XLOGShmemSize(void)
Definition: xlog.c:4337
size_t XLogPrefetchShmemSize(void)
void XLogPrefetchShmemInit(void)
Size XLogRecoveryShmemSize(void)
Definition: xlogrecovery.c:443
void XLogRecoveryShmemInit(void)
Definition: xlogrecovery.c:454