@@ -58,6 +58,8 @@ shmem_startup_hook_type shmem_startup_hook = NULL;
58
58
59
59
static Size total_addin_request = 0 ;
60
60
61
+ static void CreateOrAttachShmemStructs (void );
62
+
61
63
/*
62
64
* RequestAddinShmemSpace
63
65
* Request that extra shmem space be allocated for use by
@@ -156,9 +158,106 @@ CalculateShmemSize(int *num_semaphores)
156
158
return size ;
157
159
}
158
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 );
175
+ Assert (IsUnderPostmaster );
176
+
177
+ CreateOrAttachShmemStructs ();
178
+
179
+ /*
180
+ * Now give loadable modules a chance to set up their shmem allocations
181
+ */
182
+ if (shmem_startup_hook )
183
+ shmem_startup_hook ();
184
+ }
185
+ #endif
186
+
159
187
/*
160
188
* CreateSharedMemoryAndSemaphores
161
189
* Creates and initializes shared memory and semaphores.
190
+ */
191
+ void
192
+ CreateSharedMemoryAndSemaphores (void )
193
+ {
194
+ PGShmemHeader * shim ;
195
+ PGShmemHeader * seghdr ;
196
+ Size size ;
197
+ int numSemas ;
198
+
199
+ Assert (!IsUnderPostmaster );
200
+
201
+ /* Compute the size of the shared-memory block */
202
+ size = CalculateShmemSize (& numSemas );
203
+ elog (DEBUG3 , "invoking IpcMemoryCreate(size=%zu)" , size );
204
+
205
+ /*
206
+ * Create the shmem segment
207
+ */
208
+ seghdr = PGSharedMemoryCreate (size , & shim );
209
+
210
+ /*
211
+ * Make sure that huge pages are never reported as "unknown" while the
212
+ * server is running.
213
+ */
214
+ Assert (strcmp ("unknown" ,
215
+ GetConfigOption ("huge_pages_status" , false, false)) != 0 );
216
+
217
+ InitShmemAccess (seghdr );
218
+
219
+ /*
220
+ * Create semaphores
221
+ */
222
+ PGReserveSemaphores (numSemas );
223
+
224
+ /*
225
+ * If spinlocks are disabled, initialize emulation layer (which depends on
226
+ * semaphores, so the order is important here).
227
+ */
228
+ #ifndef HAVE_SPINLOCKS
229
+ SpinlockSemaInit ();
230
+ #endif
231
+
232
+ /*
233
+ * Set up shared memory allocation mechanism
234
+ */
235
+ InitShmemAllocation ();
236
+
237
+ /* Initialize subsystems */
238
+ CreateOrAttachShmemStructs ();
239
+
240
+ #ifdef EXEC_BACKEND
241
+
242
+ /*
243
+ * Alloc the win32 shared backend array
244
+ */
245
+ ShmemBackendArrayAllocation ();
246
+ #endif
247
+
248
+ /* Initialize dynamic shared memory facilities. */
249
+ dsm_postmaster_startup (shim );
250
+
251
+ /*
252
+ * Now give loadable modules a chance to set up their shmem allocations
253
+ */
254
+ if (shmem_startup_hook )
255
+ shmem_startup_hook ();
256
+ }
257
+
258
+ /*
259
+ * Initialize various subsystems, setting up their data structures in
260
+ * shared memory.
162
261
*
163
262
* This is called by the postmaster or by a standalone backend.
164
263
* It is also called by a backend forked from the postmaster in the
@@ -171,65 +270,9 @@ CalculateShmemSize(int *num_semaphores)
171
270
* check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
172
271
* This is a bit code-wasteful and could be cleaned up.)
173
272
*/
174
- void
175
- CreateSharedMemoryAndSemaphores (void )
273
+ static void
274
+ CreateOrAttachShmemStructs (void )
176
275
{
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
213
- SpinlockSemaInit ();
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 )
231
- InitShmemAllocation ();
232
-
233
276
/*
234
277
* Now initialize LWLocks, which do shared memory allocation and are
235
278
* needed for InitShmemIndex.
@@ -302,25 +345,6 @@ CreateSharedMemoryAndSemaphores(void)
302
345
AsyncShmemInit ();
303
346
StatsShmemInit ();
304
347
WaitEventExtensionShmemInit ();
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 )
317
- dsm_postmaster_startup (shim );
318
-
319
- /*
320
- * Now give loadable modules a chance to set up their shmem allocations
321
- */
322
- if (shmem_startup_hook )
323
- shmem_startup_hook ();
324
348
}
325
349
326
350
/*
0 commit comments