1 /*-------------------------------------------------------------------------
2 *
3 * ipci.c
4 * POSTGRES inter-process communication initialization code.
5 *
6 * Portions Copyright (c) 1996-2016, 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/twophase.h"
24 #include "commands/async.h"
25 #include "miscadmin.h"
26 #include "pgstat.h"
27 #include "postmaster/autovacuum.h"
28 #include "postmaster/bgworker_internals.h"
29 #include "postmaster/bgwriter.h"
30 #include "postmaster/postmaster.h"
31 #include "replication/slot.h"
32 #include "replication/walreceiver.h"
33 #include "replication/walsender.h"
34 #include "replication/origin.h"
35 #include "storage/bufmgr.h"
36 #include "storage/dsm.h"
37 #include "storage/ipc.h"
38 #include "storage/pg_shmem.h"
39 #include "storage/pmsignal.h"
40 #include "storage/predicate.h"
41 #include "storage/proc.h"
42 #include "storage/procarray.h"
43 #include "storage/procsignal.h"
44 #include "storage/sinvaladt.h"
45 #include "storage/spin.h"
46 #include "utils/snapmgr.h"
47
48
49 shmem_startup_hook_type shmem_startup_hook = NULL;
50
51 static Size total_addin_request = 0;
52 static bool addin_request_allowed = true;
53
54
55 /*
56 * RequestAddinShmemSpace
57 * Request that extra shmem space be allocated for use by
58 * a loadable module.
59 *
60 * This is only useful if called from the _PG_init hook of a library that
61 * is loaded into the postmaster via shared_preload_libraries. Once
62 * shared memory has been allocated, calls will be ignored. (We could
63 * raise an error, but it seems better to make it a no-op, so that
64 * libraries containing such calls can be reloaded if needed.)
65 */
66 void
RequestAddinShmemSpace(Size size)67 RequestAddinShmemSpace(Size size)
68 {
69 if (IsUnderPostmaster || !addin_request_allowed)
70 return; /* too late */
71 total_addin_request = add_size(total_addin_request, size);
72 }
73
74
75 /*
76 * CreateSharedMemoryAndSemaphores
77 * Creates and initializes shared memory and semaphores.
78 *
79 * This is called by the postmaster or by a standalone backend.
80 * It is also called by a backend forked from the postmaster in the
81 * EXEC_BACKEND case. In the latter case, the shared memory segment
82 * already exists and has been physically attached to, but we have to
83 * initialize pointers in local memory that reference the shared structures,
84 * because we didn't inherit the correct pointer values from the postmaster
85 * as we do in the fork() scenario. The easiest way to do that is to run
86 * through the same code as before. (Note that the called routines mostly
87 * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
88 * This is a bit code-wasteful and could be cleaned up.)
89 */
90 void
CreateSharedMemoryAndSemaphores(int port)91 CreateSharedMemoryAndSemaphores(int port)
92 {
93 PGShmemHeader *shim = NULL;
94
95 if (!IsUnderPostmaster)
96 {
97 PGShmemHeader *seghdr;
98 Size size;
99 int numSemas;
100
101 /*
102 * Size of the Postgres shared-memory block is estimated via
103 * moderately-accurate estimates for the big hogs, plus 100K for the
104 * stuff that's too small to bother with estimating.
105 *
106 * We take some care during this phase to ensure that the total size
107 * request doesn't overflow size_t. If this gets through, we don't
108 * need to be so careful during the actual allocation phase.
109 */
110 size = 100000;
111 size = add_size(size, SpinlockSemaSize());
112 size = add_size(size, hash_estimate_size(SHMEM_INDEX_SIZE,
113 sizeof(ShmemIndexEnt)));
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, XLOGShmemSize());
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, SnapMgrShmemSize());
138 size = add_size(size, BTreeShmemSize());
139 size = add_size(size, SyncScanShmemSize());
140 size = add_size(size, AsyncShmemSize());
141 #ifdef EXEC_BACKEND
142 size = add_size(size, ShmemBackendArraySize());
143 #endif
144
145 /* freeze the addin request size and include it */
146 addin_request_allowed = false;
147 size = add_size(size, total_addin_request);
148
149 /* might as well round it off to a multiple of a typical page size */
150 size = add_size(size, 8192 - (size % 8192));
151
152 elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
153
154 /*
155 * Create the shmem segment
156 */
157 seghdr = PGSharedMemoryCreate(size, port, &shim);
158
159 InitShmemAccess(seghdr);
160
161 /*
162 * Create semaphores
163 */
164 numSemas = ProcGlobalSemas();
165 numSemas += SpinlockSemas();
166 PGReserveSemaphores(numSemas, port);
167 }
168 else
169 {
170 /*
171 * We are reattaching to an existing shared memory segment. This
172 * should only be reached in the EXEC_BACKEND case.
173 */
174 #ifndef EXEC_BACKEND
175 elog(PANIC, "should be attached to shared memory already");
176 #endif
177 }
178
179 /*
180 * Set up shared memory allocation mechanism
181 */
182 if (!IsUnderPostmaster)
183 InitShmemAllocation();
184
185 /*
186 * Now initialize LWLocks, which do shared memory allocation and are
187 * needed for InitShmemIndex.
188 */
189 CreateLWLocks();
190
191 /*
192 * Set up shmem.c index hashtable
193 */
194 InitShmemIndex();
195
196 /*
197 * Set up xlog, clog, and buffers
198 */
199 XLOGShmemInit();
200 CLOGShmemInit();
201 CommitTsShmemInit();
202 SUBTRANSShmemInit();
203 MultiXactShmemInit();
204 InitBufferPool();
205
206 /*
207 * Set up lock manager
208 */
209 InitLocks();
210
211 /*
212 * Set up predicate lock manager
213 */
214 InitPredicateLocks();
215
216 /*
217 * Set up process table
218 */
219 if (!IsUnderPostmaster)
220 InitProcGlobal();
221 CreateSharedProcArray();
222 CreateSharedBackendStatus();
223 TwoPhaseShmemInit();
224 BackgroundWorkerShmemInit();
225
226 /*
227 * Set up shared-inval messaging
228 */
229 CreateSharedInvalidationState();
230
231 /*
232 * Set up interprocess signaling mechanisms
233 */
234 PMSignalShmemInit();
235 ProcSignalShmemInit();
236 CheckpointerShmemInit();
237 AutoVacuumShmemInit();
238 ReplicationSlotsShmemInit();
239 ReplicationOriginShmemInit();
240 WalSndShmemInit();
241 WalRcvShmemInit();
242
243 /*
244 * Set up other modules that need some shared memory space
245 */
246 SnapMgrInit();
247 BTreeShmemInit();
248 SyncScanShmemInit();
249 AsyncShmemInit();
250
251 #ifdef EXEC_BACKEND
252
253 /*
254 * Alloc the win32 shared backend array
255 */
256 if (!IsUnderPostmaster)
257 ShmemBackendArrayAllocation();
258 #endif
259
260 /* Initialize dynamic shared memory facilities. */
261 if (!IsUnderPostmaster)
262 dsm_postmaster_startup(shim);
263
264 /*
265 * Now give loadable modules a chance to set up their shmem allocations
266 */
267 if (shmem_startup_hook)
268 shmem_startup_hook();
269 }
270