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-2020, 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 "commands/async.h"
26 #include "miscadmin.h"
27 #include "pgstat.h"
28 #include "postmaster/autovacuum.h"
30 #include "postmaster/bgwriter.h"
31 #include "postmaster/postmaster.h"
33 #include "replication/origin.h"
34 #include "replication/slot.h"
36 #include "replication/walsender.h"
37 #include "storage/bufmgr.h"
38 #include "storage/dsm.h"
39 #include "storage/ipc.h"
40 #include "storage/pg_shmem.h"
41 #include "storage/pmsignal.h"
42 #include "storage/predicate.h"
43 #include "storage/proc.h"
44 #include "storage/procarray.h"
45 #include "storage/procsignal.h"
46 #include "storage/sinvaladt.h"
47 #include "storage/spin.h"
48 #include "utils/snapmgr.h"
49 
50 /* GUCs */
52 
54 
56 static bool addin_request_allowed = true;
57 
58 
59 /*
60  * RequestAddinShmemSpace
61  * Request that extra shmem space be allocated for use by
62  * a loadable module.
63  *
64  * This is only useful if called from the _PG_init hook of a library that
65  * is loaded into the postmaster via shared_preload_libraries. Once
66  * shared memory has been allocated, calls will be ignored. (We could
67  * raise an error, but it seems better to make it a no-op, so that
68  * libraries containing such calls can be reloaded if needed.)
69  */
70 void
72 {
74  return; /* too late */
76 }
77 
78 
79 /*
80  * CreateSharedMemoryAndSemaphores
81  * Creates and initializes shared memory and semaphores.
82  *
83  * This is called by the postmaster or by a standalone backend.
84  * It is also called by a backend forked from the postmaster in the
85  * EXEC_BACKEND case. In the latter case, the shared memory segment
86  * already exists and has been physically attached to, but we have to
87  * initialize pointers in local memory that reference the shared structures,
88  * because we didn't inherit the correct pointer values from the postmaster
89  * as we do in the fork() scenario. The easiest way to do that is to run
90  * through the same code as before. (Note that the called routines mostly
91  * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
92  * This is a bit code-wasteful and could be cleaned up.)
93  */
94 void
96 {
97  PGShmemHeader *shim = NULL;
98 
99  if (!IsUnderPostmaster)
100  {
101  PGShmemHeader *seghdr;
102  Size size;
103  int numSemas;
104 
105  /* Compute number of semaphores we'll need */
106  numSemas = ProcGlobalSemas();
107  numSemas += SpinlockSemas();
108 
109  /*
110  * Size of the Postgres shared-memory block is estimated via
111  * moderately-accurate estimates for the big hogs, plus 100K for the
112  * stuff that's too small to bother with estimating.
113  *
114  * We take some care during this phase to ensure that the total size
115  * request doesn't overflow size_t. If this gets through, we don't
116  * need to be so careful during the actual allocation phase.
117  */
118  size = 100000;
119  size = add_size(size, PGSemaphoreShmemSize(numSemas));
120  size = add_size(size, SpinlockSemaSize());
122  sizeof(ShmemIndexEnt)));
123  size = add_size(size, dsm_estimate_size());
124  size = add_size(size, BufferShmemSize());
125  size = add_size(size, LockShmemSize());
126  size = add_size(size, PredicateLockShmemSize());
127  size = add_size(size, ProcGlobalShmemSize());
128  size = add_size(size, XLOGShmemSize());
129  size = add_size(size, CLOGShmemSize());
130  size = add_size(size, CommitTsShmemSize());
131  size = add_size(size, SUBTRANSShmemSize());
132  size = add_size(size, TwoPhaseShmemSize());
133  size = add_size(size, BackgroundWorkerShmemSize());
134  size = add_size(size, MultiXactShmemSize());
135  size = add_size(size, LWLockShmemSize());
136  size = add_size(size, ProcArrayShmemSize());
137  size = add_size(size, BackendStatusShmemSize());
138  size = add_size(size, SInvalShmemSize());
139  size = add_size(size, PMSignalShmemSize());
140  size = add_size(size, ProcSignalShmemSize());
141  size = add_size(size, CheckpointerShmemSize());
142  size = add_size(size, AutoVacuumShmemSize());
143  size = add_size(size, ReplicationSlotsShmemSize());
144  size = add_size(size, ReplicationOriginShmemSize());
145  size = add_size(size, WalSndShmemSize());
146  size = add_size(size, WalRcvShmemSize());
147  size = add_size(size, ApplyLauncherShmemSize());
148  size = add_size(size, SnapMgrShmemSize());
149  size = add_size(size, BTreeShmemSize());
150  size = add_size(size, SyncScanShmemSize());
151  size = add_size(size, AsyncShmemSize());
152 #ifdef EXEC_BACKEND
153  size = add_size(size, ShmemBackendArraySize());
154 #endif
155 
156  /* freeze the addin request size and include it */
157  addin_request_allowed = false;
158  size = add_size(size, total_addin_request);
159 
160  /* might as well round it off to a multiple of a typical page size */
161  size = add_size(size, 8192 - (size % 8192));
162 
163  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
164 
165  /*
166  * Create the shmem segment
167  */
168  seghdr = PGSharedMemoryCreate(size, &shim);
169 
170  InitShmemAccess(seghdr);
171 
172  /*
173  * Create semaphores
174  */
175  PGReserveSemaphores(numSemas);
176 
177  /*
178  * If spinlocks are disabled, initialize emulation layer (which
179  * depends on semaphores, so the order is important here).
180  */
181 #ifndef HAVE_SPINLOCKS
183 #endif
184  }
185  else
186  {
187  /*
188  * We are reattaching to an existing shared memory segment. This
189  * should only be reached in the EXEC_BACKEND case.
190  */
191 #ifndef EXEC_BACKEND
192  elog(PANIC, "should be attached to shared memory already");
193 #endif
194  }
195 
196  /*
197  * Set up shared memory allocation mechanism
198  */
199  if (!IsUnderPostmaster)
201 
202  /*
203  * Now initialize LWLocks, which do shared memory allocation and are
204  * needed for InitShmemIndex.
205  */
206  CreateLWLocks();
207 
208  /*
209  * Set up shmem.c index hashtable
210  */
211  InitShmemIndex();
212 
213  dsm_shmem_init();
214 
215  /*
216  * Set up xlog, clog, and buffers
217  */
218  XLOGShmemInit();
219  CLOGShmemInit();
223  InitBufferPool();
224 
225  /*
226  * Set up lock manager
227  */
228  InitLocks();
229 
230  /*
231  * Set up predicate lock manager
232  */
234 
235  /*
236  * Set up process table
237  */
238  if (!IsUnderPostmaster)
239  InitProcGlobal();
244 
245  /*
246  * Set up shared-inval messaging
247  */
249 
250  /*
251  * Set up interprocess signaling mechanisms
252  */
259  WalSndShmemInit();
260  WalRcvShmemInit();
262 
263  /*
264  * Set up other modules that need some shared memory space
265  */
266  SnapMgrInit();
267  BTreeShmemInit();
269  AsyncShmemInit();
270 
271 #ifdef EXEC_BACKEND
272 
273  /*
274  * Alloc the win32 shared backend array
275  */
276  if (!IsUnderPostmaster)
277  ShmemBackendArrayAllocation();
278 #endif
279 
280  /* Initialize dynamic shared memory facilities. */
281  if (!IsUnderPostmaster)
283 
284  /*
285  * Now give loadable modules a chance to set up their shmem allocations
286  */
287  if (shmem_startup_hook)
289 }
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:149
Size SInvalShmemSize(void)
Definition: sinvaladt.c:203
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
Definition: sysv_shmem.c:651
void RequestAddinShmemSpace(Size size)
Definition: ipci.c:71
void BTreeShmemInit(void)
Definition: nbtutils.c:2075
Size PMSignalShmemSize(void)
Definition: pmsignal.c:113
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:117
#define SHMEM_INDEX_SIZE
Definition: shmem.h:55
void dsm_shmem_init(void)
Definition: dsm.c:450
void SUBTRANSShmemInit(void)
Definition: subtrans.c:191
#define DEBUG3
Definition: elog.h:23
Size SpinlockSemaSize(void)
Definition: spin.c:55
void InitShmemAccess(void *seghdr)
Definition: shmem.c:101
void AsyncShmemInit(void)
Definition: async.c:518
void CreateSharedInvalidationState(void)
Definition: sinvaladt.c:218
Size SyncScanShmemSize(void)
Definition: syncscan.c:126
Size WalSndShmemSize(void)
Definition: walsender.c:3075
int shared_memory_type
Definition: ipci.c:51
void CheckpointerShmemInit(void)
Definition: checkpointer.c:870
Size PredicateLockShmemSize(void)
Definition: predicate.c:1285
Size TwoPhaseShmemSize(void)
Definition: twophase.c:236
void ReplicationOriginShmemInit(void)
Definition: origin.c:493
void WalRcvShmemInit(void)
#define PANIC
Definition: elog.h:53
void XLOGShmemInit(void)
Definition: xlog.c:5083
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:142
void ReplicationSlotsShmemInit(void)
Definition: slot.c:135
Size LWLockShmemSize(void)
Definition: lwlock.c:435
Size BTreeShmemSize(void)
Definition: nbtutils.c:2062
void InitProcGlobal(void)
Definition: proc.c:158
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:158
void MultiXactShmemInit(void)
Definition: multixact.c:1822
void PMSignalShmemInit(void)
Definition: pmsignal.c:128
Size XLOGShmemSize(void)
Definition: xlog.c:5042
void SpinlockSemaInit(void)
Definition: spin.c:77
size_t dsm_estimate_size(void)
Definition: dsm.c:441
bool IsUnderPostmaster
Definition: globals.c:109
void(* shmem_startup_hook_type)(void)
Definition: ipc.h:22
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:748
void InitBufferPool(void)
Definition: buf_init.c:67
void SnapMgrInit(void)
Definition: snapmgr.c:264
void InitLocks(void)
Definition: lock.c:404
Size ProcGlobalShmemSize(void)
Definition: proc.c:101
static bool addin_request_allowed
Definition: ipci.c:56
Size ProcSignalShmemSize(void)
Definition: procsignal.c:102
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:53
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:745
void TwoPhaseShmemInit(void)
Definition: twophase.c:252
int SpinlockSemas(void)
Definition: spin.c:64
static Size total_addin_request
Definition: ipci.c:55
Size BackendStatusShmemSize(void)
Definition: pgstat.c:2718
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3336
Size ReplicationOriginShmemSize(void)
Definition: origin.c:473
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:851
Size CLOGShmemSize(void)
Definition: clog.c:684
void CreateSharedBackendStatus(void)
Definition: pgstat.c:2746
void PGReserveSemaphores(int maxSemas)
Definition: posix_sema.c:196
Size WalRcvShmemSize(void)
void CreateLWLocks(void)
Definition: lwlock.c:468
void SyncScanShmemInit(void)
Definition: syncscan.c:135
void CreateSharedProcArray(void)
Definition: procarray.c:383
Size AsyncShmemSize(void)
Definition: async.c:501
size_t Size
Definition: c.h:473
Size CommitTsShmemSize(void)
Definition: commit_ts.c:540
void ApplyLauncherShmemInit(void)
Definition: launcher.c:796
Size MultiXactShmemSize(void)
Definition: multixact.c:1805
void CreateSharedMemoryAndSemaphores(void)
Definition: ipci.c:95
Size LockShmemSize(void)
Definition: lock.c:3532
Size SUBTRANSShmemSize(void)
Definition: subtrans.c:185
Size BufferShmemSize(void)
Definition: buf_init.c:157
#define elog(elevel,...)
Definition: elog.h:214
void WalSndShmemInit(void)
Definition: walsender.c:3087
void CLOGShmemInit(void)
Definition: clog.c:690
Size PGSemaphoreShmemSize(int maxSemas)
Definition: posix_sema.c:165
void ProcSignalShmemInit(void)
Definition: procsignal.c:116
void InitShmemIndex(void)
Definition: shmem.c:292
void InitPredicateLocks(void)
Definition: predicate.c:1080
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3355
Size SnapMgrShmemSize(void)
Definition: snapmgr.c:248
#define DEFAULT_SHARED_MEMORY_TYPE
Definition: pg_shmem.h:74
Size ProcArrayShmemSize(void)
Definition: procarray.c:341
int ProcGlobalSemas(void)
Definition: proc.c:123
void InitShmemAllocation(void)
Definition: shmem.c:116
void CommitTsShmemInit(void)
Definition: commit_ts.c:551