Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 69d9033

Browse files
committed
Refactor CreateSharedMemoryAndSemaphores
For clarity, have separate functions for *creating* the shared memory and semaphores at postmaster or single-user backend startup, and for *attaching* to existing shared memory structures in EXEC_BACKEND case. CreateSharedMemoryAndSemaphores() is now called only at postmaster startup, and a new AttachSharedMemoryStructs() function is called at backend startup in EXEC_BACKEND mode. Reviewed-by: Tristan Partin, Andres Freund Discussion: https://www.postgresql.org/message-id/7a59b073-5b5b-151e-7ed3-8b01ff7ce9ef@iki.fi
1 parent b19890d commit 69d9033

File tree

5 files changed

+117
-89
lines changed

5 files changed

+117
-89
lines changed

src/backend/postmaster/postmaster.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -4917,11 +4917,11 @@ SubPostmasterMain(int argc, char *argv[])
49174917
/* Restore basic shared memory pointers */
49184918
InitShmemAccess(UsedShmemSegAddr);
49194919

4920-
/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
4920+
/* Need a PGPROC to run AttachSharedMemoryStructs */
49214921
InitProcess();
49224922

49234923
/* Attach process to shared data structures */
4924-
CreateSharedMemoryAndSemaphores();
4924+
AttachSharedMemoryStructs();
49254925

49264926
/* And run the backend */
49274927
BackendRun(&port); /* does not return */
@@ -4935,11 +4935,11 @@ SubPostmasterMain(int argc, char *argv[])
49354935
/* Restore basic shared memory pointers */
49364936
InitShmemAccess(UsedShmemSegAddr);
49374937

4938-
/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
4938+
/* Need a PGPROC to run AttachSharedMemoryStructs */
49394939
InitAuxiliaryProcess();
49404940

49414941
/* Attach process to shared data structures */
4942-
CreateSharedMemoryAndSemaphores();
4942+
AttachSharedMemoryStructs();
49434943

49444944
auxtype = atoi(argv[3]);
49454945
AuxiliaryProcessMain(auxtype); /* does not return */
@@ -4949,11 +4949,11 @@ SubPostmasterMain(int argc, char *argv[])
49494949
/* Restore basic shared memory pointers */
49504950
InitShmemAccess(UsedShmemSegAddr);
49514951

4952-
/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
4952+
/* Need a PGPROC to run AttachSharedMemoryStructs */
49534953
InitProcess();
49544954

49554955
/* Attach process to shared data structures */
4956-
CreateSharedMemoryAndSemaphores();
4956+
AttachSharedMemoryStructs();
49574957

49584958
AutoVacLauncherMain(argc - 2, argv + 2); /* does not return */
49594959
}
@@ -4962,11 +4962,11 @@ SubPostmasterMain(int argc, char *argv[])
49624962
/* Restore basic shared memory pointers */
49634963
InitShmemAccess(UsedShmemSegAddr);
49644964

4965-
/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
4965+
/* Need a PGPROC to run AttachSharedMemoryStructs */
49664966
InitProcess();
49674967

49684968
/* Attach process to shared data structures */
4969-
CreateSharedMemoryAndSemaphores();
4969+
AttachSharedMemoryStructs();
49704970

49714971
AutoVacWorkerMain(argc - 2, argv + 2); /* does not return */
49724972
}
@@ -4980,11 +4980,11 @@ SubPostmasterMain(int argc, char *argv[])
49804980
/* Restore basic shared memory pointers */
49814981
InitShmemAccess(UsedShmemSegAddr);
49824982

4983-
/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
4983+
/* Need a PGPROC to run AttachSharedMemoryStructs */
49844984
InitProcess();
49854985

49864986
/* Attach process to shared data structures */
4987-
CreateSharedMemoryAndSemaphores();
4987+
AttachSharedMemoryStructs();
49884988

49894989
/* Fetch MyBgworkerEntry from shared memory */
49904990
shmem_slot = atoi(argv[1] + 15);

src/backend/replication/walreceiver.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ WalReceiverMain(void)
193193
TimeLineID startpointTLI;
194194
TimeLineID primaryTLI;
195195
bool first_stream;
196-
WalRcvData *walrcv = WalRcv;
196+
WalRcvData *walrcv;
197197
TimestampTz now;
198198
char *err;
199199
char *sender_host = NULL;
@@ -203,6 +203,7 @@ WalReceiverMain(void)
203203
* WalRcv should be set up already (if we are a backend, we inherit this
204204
* by fork() or EXEC_BACKEND mechanism from the postmaster).
205205
*/
206+
walrcv = WalRcv;
206207
Assert(walrcv != NULL);
207208

208209
/*

src/backend/storage/ipc/ipci.c

+101-77
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ shmem_startup_hook_type shmem_startup_hook = NULL;
5858

5959
static Size total_addin_request = 0;
6060

61+
static void CreateOrAttachShmemStructs(void);
62+
6163
/*
6264
* RequestAddinShmemSpace
6365
* Request that extra shmem space be allocated for use by
@@ -156,9 +158,106 @@ CalculateShmemSize(int *num_semaphores)
156158
return size;
157159
}
158160

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+
159187
/*
160188
* CreateSharedMemoryAndSemaphores
161189
* 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.
162261
*
163262
* This is called by the postmaster or by a standalone backend.
164263
* It is also called by a backend forked from the postmaster in the
@@ -171,65 +270,9 @@ CalculateShmemSize(int *num_semaphores)
171270
* check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
172271
* This is a bit code-wasteful and could be cleaned up.)
173272
*/
174-
void
175-
CreateSharedMemoryAndSemaphores(void)
273+
static void
274+
CreateOrAttachShmemStructs(void)
176275
{
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-
233276
/*
234277
* Now initialize LWLocks, which do shared memory allocation and are
235278
* needed for InitShmemIndex.
@@ -302,25 +345,6 @@ CreateSharedMemoryAndSemaphores(void)
302345
AsyncShmemInit();
303346
StatsShmemInit();
304347
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();
324348
}
325349

326350
/*

src/backend/storage/lmgr/proc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ InitProcess(void)
468468
*
469469
* This is separate from InitProcess because we can't acquire LWLocks until
470470
* we've created a PGPROC, but in the EXEC_BACKEND case ProcArrayAdd won't
471-
* work until after we've done CreateSharedMemoryAndSemaphores.
471+
* work until after we've done AttachSharedMemoryStructs.
472472
*/
473473
void
474474
InitProcessPhase2(void)

src/include/storage/ipc.h

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ extern PGDLLIMPORT shmem_startup_hook_type shmem_startup_hook;
7979

8080
extern Size CalculateShmemSize(int *num_semaphores);
8181
extern void CreateSharedMemoryAndSemaphores(void);
82+
#ifdef EXEC_BACKEND
83+
extern void AttachSharedMemoryStructs(void);
84+
#endif
8285
extern void InitializeShmemGUCs(void);
8386

8487
#endif /* IPC_H */

0 commit comments

Comments
 (0)