Skip to content

Commit b406478

Browse files
committed
process startup: Always call Init[Auxiliary]Process() before BaseInit().
For EXEC_BACKEND InitProcess()/InitAuxiliaryProcess() needs to have been called well before we call BaseInit(), as SubPostmasterMain() needs LWLocks to work. Having the order of initialization differ between platforms makes it unnecessarily hard to understand the system and to add initialization points for new subsystems without a lot of duplication. To be able to change the order, BaseInit() cannot trigger CreateSharedMemoryAndSemaphores() anymore - obviously that needs to have happened before we can call InitProcess(). It seems cleaner to create shared memory explicitly in single user/bootstrap mode anyway. After this change the separation of bufmgr initialization into InitBufferPoolAccess() / InitBufferPoolBackend() is not meaningful anymore so the latter is removed. Author: Andres Freund <andres@anarazel.de> Reviewed-By: Kyotaro Horiguchi <horikyota.ntt@gmail.com> Discussion: https://postgr.es/m/20210802164124.ufo5buo4apl6yuvs@alap3.anarazel.de
1 parent 0de13bb commit b406478

File tree

8 files changed

+39
-83
lines changed

8 files changed

+39
-83
lines changed

src/backend/bootstrap/bootstrap.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ static IndexList *ILHead = NULL;
178178
/*
179179
* In shared memory checker mode, all we really want to do is create shared
180180
* memory and semaphores (just to prove we can do it with the current GUC
181-
* settings). Since, in fact, that was already done by BaseInit(),
182-
* we have nothing more to do here.
181+
* settings). Since, in fact, that was already done by
182+
* CreateSharedMemoryAndSemaphores(), we have nothing more to do here.
183183
*/
184184
static void
185185
CheckerModeMain(void)
@@ -324,7 +324,7 @@ BootstrapModeMain(int argc, char *argv[], bool check_only)
324324

325325
InitializeMaxBackends();
326326

327-
BaseInit();
327+
CreateSharedMemoryAndSemaphores();
328328

329329
/*
330330
* XXX: It might make sense to move this into its own function at some
@@ -338,6 +338,13 @@ BootstrapModeMain(int argc, char *argv[], bool check_only)
338338
abort();
339339
}
340340

341+
/*
342+
* Do backend-like initialization for bootstrap mode
343+
*/
344+
InitProcess();
345+
346+
BaseInit();
347+
341348
bootstrap_signals();
342349
BootStrapXLOG();
343350

@@ -348,11 +355,6 @@ BootstrapModeMain(int argc, char *argv[], bool check_only)
348355
if (pg_link_canary_is_frontend())
349356
elog(ERROR, "backend is incorrectly linked to frontend functions");
350357

351-
/*
352-
* Do backend-like initialization for bootstrap mode
353-
*/
354-
InitProcess();
355-
356358
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL, false);
357359

358360
/* Initialize stuff for bootstrap-file processing */

src/backend/postmaster/autovacuum.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,6 @@ AutoVacLauncherMain(int argc, char *argv[])
469469
pqsignal(SIGFPE, FloatExceptionHandler);
470470
pqsignal(SIGCHLD, SIG_DFL);
471471

472-
/* Early initialization */
473-
BaseInit();
474-
475472
/*
476473
* Create a per-backend PGPROC struct in shared memory, except in the
477474
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do
@@ -482,6 +479,9 @@ AutoVacLauncherMain(int argc, char *argv[])
482479
InitProcess();
483480
#endif
484481

482+
/* Early initialization */
483+
BaseInit();
484+
485485
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL, false);
486486

487487
SetProcessingMode(NormalProcessing);
@@ -1547,9 +1547,6 @@ AutoVacWorkerMain(int argc, char *argv[])
15471547
pqsignal(SIGFPE, FloatExceptionHandler);
15481548
pqsignal(SIGCHLD, SIG_DFL);
15491549

1550-
/* Early initialization */
1551-
BaseInit();
1552-
15531550
/*
15541551
* Create a per-backend PGPROC struct in shared memory, except in the
15551552
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do
@@ -1560,6 +1557,9 @@ AutoVacWorkerMain(int argc, char *argv[])
15601557
InitProcess();
15611558
#endif
15621559

1560+
/* Early initialization */
1561+
BaseInit();
1562+
15631563
/*
15641564
* If an exception is encountered, processing resumes here.
15651565
*

src/backend/postmaster/auxprocess.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ AuxiliaryProcessMain(AuxProcType auxtype)
9090
SetProcessingMode(BootstrapProcessing);
9191
IgnoreSystemIndexes = true;
9292

93-
BaseInit();
94-
9593
/*
9694
* As an auxiliary process, we aren't going to do the full InitPostgres
9795
* pushups, but there are a couple of things that need to get lit up even
@@ -106,6 +104,8 @@ AuxiliaryProcessMain(AuxProcType auxtype)
106104
InitAuxiliaryProcess();
107105
#endif
108106

107+
BaseInit();
108+
109109
/*
110110
* Assign the ProcSignalSlot for an auxiliary process. Since it doesn't
111111
* have a BackendId, the slot is statically allocated based on the
@@ -118,9 +118,6 @@ AuxiliaryProcessMain(AuxProcType auxtype)
118118
*/
119119
ProcSignalInit(MaxBackends + MyAuxProcType + 1);
120120

121-
/* finish setting up bufmgr.c */
122-
InitBufferPoolBackend();
123-
124121
/*
125122
* Auxiliary processes don't run transactions, but they may need a
126123
* resource owner anyway to manage buffer pins acquired outside

src/backend/postmaster/bgworker.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -837,14 +837,6 @@ StartBackgroundWorker(void)
837837
*/
838838
if (worker->bgw_flags & BGWORKER_SHMEM_ACCESS)
839839
{
840-
/*
841-
* Early initialization. Some of this could be useful even for
842-
* background workers that aren't using shared memory, but they can
843-
* call the individual startup routines for those subsystems if
844-
* needed.
845-
*/
846-
BaseInit();
847-
848840
/*
849841
* Create a per-backend PGPROC struct in shared memory, except in the
850842
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must
@@ -854,6 +846,14 @@ StartBackgroundWorker(void)
854846
#ifndef EXEC_BACKEND
855847
InitProcess();
856848
#endif
849+
850+
/*
851+
* Early initialization. Some of this could be useful even for
852+
* background workers that aren't using shared memory, but they can
853+
* call the individual startup routines for those subsystems if
854+
* needed.
855+
*/
856+
BaseInit();
857857
}
858858

859859
/*

src/backend/storage/buffer/bufmgr.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,11 +2582,6 @@ AtEOXact_Buffers(bool isCommit)
25822582
* This is called during backend startup (whether standalone or under the
25832583
* postmaster). It sets up for this backend's access to the already-existing
25842584
* buffer pool.
2585-
*
2586-
* NB: this is called before InitProcess(), so we do not have a PGPROC and
2587-
* cannot do LWLockAcquire; hence we can't actually access stuff in
2588-
* shared memory yet. We are only initializing local data here.
2589-
* (See also InitBufferPoolBackend)
25902585
*/
25912586
void
25922587
InitBufferPoolAccess(void)
@@ -2600,20 +2595,12 @@ InitBufferPoolAccess(void)
26002595

26012596
PrivateRefCountHash = hash_create("PrivateRefCount", 100, &hash_ctl,
26022597
HASH_ELEM | HASH_BLOBS);
2603-
}
26042598

2605-
/*
2606-
* InitBufferPoolBackend --- second-stage initialization of a new backend
2607-
*
2608-
* This is called after we have acquired a PGPROC and so can safely get
2609-
* LWLocks. We don't currently need to do anything at this stage ...
2610-
* except register a shmem-exit callback. AtProcExit_Buffers needs LWLock
2611-
* access, and thereby has to be called at the corresponding phase of
2612-
* backend shutdown.
2613-
*/
2614-
void
2615-
InitBufferPoolBackend(void)
2616-
{
2599+
/*
2600+
* AtProcExit_Buffers needs LWLock access, and thereby has to be called at
2601+
* the corresponding phase of backend shutdown.
2602+
*/
2603+
Assert(MyProc != NULL);
26172604
on_shmem_exit(AtProcExit_Buffers, 0);
26182605
}
26192606

src/backend/tcop/postgres.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4050,10 +4050,9 @@ PostgresMain(int argc, char *argv[],
40504050

40514051
/* Initialize MaxBackends (if under postmaster, was done already) */
40524052
InitializeMaxBackends();
4053-
}
40544053

4055-
/* Early initialization */
4056-
BaseInit();
4054+
CreateSharedMemoryAndSemaphores();
4055+
}
40574056

40584057
/*
40594058
* Create a per-backend PGPROC struct in shared memory, except in the
@@ -4068,6 +4067,9 @@ PostgresMain(int argc, char *argv[],
40684067
InitProcess();
40694068
#endif
40704069

4070+
/* Early initialization */
4071+
BaseInit();
4072+
40714073
/* We need to allow SIGINT, etc during the initial transaction */
40724074
PG_SETMASK(&UnBlockSig);
40734075

src/backend/utils/init/postinit.c

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ static HeapTuple GetDatabaseTuple(const char *dbname);
6767
static HeapTuple GetDatabaseTupleByOid(Oid dboid);
6868
static void PerformAuthentication(Port *port);
6969
static void CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connections);
70-
static void InitCommunication(void);
7170
static void ShutdownPostgres(int code, Datum arg);
7271
static void StatementTimeoutHandler(void);
7372
static void LockTimeoutHandler(void);
@@ -417,31 +416,6 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect
417416
}
418417

419418

420-
421-
/* --------------------------------
422-
* InitCommunication
423-
*
424-
* This routine initializes stuff needed for ipc, locking, etc.
425-
* it should be called something more informative.
426-
* --------------------------------
427-
*/
428-
static void
429-
InitCommunication(void)
430-
{
431-
/*
432-
* initialize shared memory and semaphores appropriately.
433-
*/
434-
if (!IsUnderPostmaster) /* postmaster already did this */
435-
{
436-
/*
437-
* We're running a postgres bootstrap process or a standalone backend,
438-
* so we need to set up shmem.
439-
*/
440-
CreateSharedMemoryAndSemaphores();
441-
}
442-
}
443-
444-
445419
/*
446420
* pg_split_opts -- split a string of options and append it to an argv array
447421
*
@@ -536,11 +510,11 @@ InitializeMaxBackends(void)
536510
void
537511
BaseInit(void)
538512
{
513+
Assert(MyProc != NULL);
514+
539515
/*
540-
* Attach to shared memory and semaphores, and initialize our
541-
* input/output/debugging file descriptors.
516+
* Initialize our input/output/debugging file descriptors.
542517
*/
543-
InitCommunication();
544518
DebugFileOpen();
545519

546520
/* Do local initialization of file, storage and buffer managers */
@@ -624,11 +598,6 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
624598
RegisterTimeout(CLIENT_CONNECTION_CHECK_TIMEOUT, ClientCheckTimeoutHandler);
625599
}
626600

627-
/*
628-
* bufmgr needs another initialization call too
629-
*/
630-
InitBufferPoolBackend();
631-
632601
/*
633602
* Initialize local process's access to XLOG.
634603
*/

src/include/storage/bufmgr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
194194

195195
extern void InitBufferPool(void);
196196
extern void InitBufferPoolAccess(void);
197-
extern void InitBufferPoolBackend(void);
198197
extern void AtEOXact_Buffers(bool isCommit);
199198
extern void PrintBufferLeakWarning(Buffer buffer);
200199
extern void CheckPointBuffers(int flags);

0 commit comments

Comments
 (0)