Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit69d9033

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

File tree

5 files changed

+117
-89
lines changed

5 files changed

+117
-89
lines changed

‎src/backend/postmaster/postmaster.c

Lines changed: 10 additions & 10 deletions
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 runCreateSharedMemoryAndSemaphores */
4920+
/* Need a PGPROC to runAttachSharedMemoryStructs */
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 runCreateSharedMemoryAndSemaphores */
4938+
/* Need a PGPROC to runAttachSharedMemoryStructs */
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 runCreateSharedMemoryAndSemaphores */
4952+
/* Need a PGPROC to runAttachSharedMemoryStructs */
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 runCreateSharedMemoryAndSemaphores */
4965+
/* Need a PGPROC to runAttachSharedMemoryStructs */
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 runCreateSharedMemoryAndSemaphores */
4983+
/* Need a PGPROC to runAttachSharedMemoryStructs */
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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ WalReceiverMain(void)
193193
TimeLineIDstartpointTLI;
194194
TimeLineIDprimaryTLI;
195195
boolfirst_stream;
196-
WalRcvData*walrcv=WalRcv;
196+
WalRcvData*walrcv;
197197
TimestampTznow;
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

Lines changed: 101 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ shmem_startup_hook_type shmem_startup_hook = NULL;
5858

5959
staticSizetotal_addin_request=0;
6060

61+
staticvoidCreateOrAttachShmemStructs(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
returnsize;
157159
}
158160

161+
#ifdefEXEC_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+
Sizesize;
197+
intnumSemas;
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+
#ifndefHAVE_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+
#ifdefEXEC_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+
staticvoid
274+
CreateOrAttachShmemStructs(void)
176275
{
177-
PGShmemHeader*shim=NULL;
178-
179-
if (!IsUnderPostmaster)
180-
{
181-
PGShmemHeader*seghdr;
182-
Sizesize;
183-
intnumSemas;
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-
#ifndefHAVE_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-
#ifndefEXEC_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-
#ifdefEXEC_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

Lines changed: 1 addition & 1 deletion
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 doneCreateSharedMemoryAndSemaphores.
471+
* work until after we've doneAttachSharedMemoryStructs.
472472
*/
473473
void
474474
InitProcessPhase2(void)

‎src/include/storage/ipc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ extern PGDLLIMPORT shmem_startup_hook_type shmem_startup_hook;
7979

8080
externSizeCalculateShmemSize(int*num_semaphores);
8181
externvoidCreateSharedMemoryAndSemaphores(void);
82+
#ifdefEXEC_BACKEND
83+
externvoidAttachSharedMemoryStructs(void);
84+
#endif
8285
externvoidInitializeShmemGUCs(void);
8386

8487
#endif/* IPC_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp