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
/*
* In shared memory checker mode, all we really want to do is create shared
* memory and semaphores (just to prove we can do it with the current GUC
- * settings). Since, in fact, that was already done by BaseInit(),
- * we have nothing more to do here.
+ * settings). Since, in fact, that was already done by
+ * CreateSharedMemoryAndSemaphores(), we have nothing more to do here.
*/
static void
CheckerModeMain(void)
InitializeMaxBackends();
- BaseInit();
+ CreateSharedMemoryAndSemaphores();
/*
* XXX: It might make sense to move this into its own function at some
abort();
}
+ /*
+ * Do backend-like initialization for bootstrap mode
+ */
+ InitProcess();
+
+ BaseInit();
+
bootstrap_signals();
BootStrapXLOG();
if (pg_link_canary_is_frontend())
elog(ERROR, "backend is incorrectly linked to frontend functions");
- /*
- * Do backend-like initialization for bootstrap mode
- */
- InitProcess();
-
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL, false);
/* Initialize stuff for bootstrap-file processing */
pqsignal(SIGFPE, FloatExceptionHandler);
pqsignal(SIGCHLD, SIG_DFL);
- /* Early initialization */
- BaseInit();
-
/*
* Create a per-backend PGPROC struct in shared memory, except in the
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do
InitProcess();
#endif
+ /* Early initialization */
+ BaseInit();
+
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL, false);
SetProcessingMode(NormalProcessing);
pqsignal(SIGFPE, FloatExceptionHandler);
pqsignal(SIGCHLD, SIG_DFL);
- /* Early initialization */
- BaseInit();
-
/*
* Create a per-backend PGPROC struct in shared memory, except in the
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do
InitProcess();
#endif
+ /* Early initialization */
+ BaseInit();
+
/*
* If an exception is encountered, processing resumes here.
*
SetProcessingMode(BootstrapProcessing);
IgnoreSystemIndexes = true;
- BaseInit();
-
/*
* As an auxiliary process, we aren't going to do the full InitPostgres
* pushups, but there are a couple of things that need to get lit up even
InitAuxiliaryProcess();
#endif
+ BaseInit();
+
/*
* Assign the ProcSignalSlot for an auxiliary process. Since it doesn't
* have a BackendId, the slot is statically allocated based on the
*/
ProcSignalInit(MaxBackends + MyAuxProcType + 1);
- /* finish setting up bufmgr.c */
- InitBufferPoolBackend();
-
/*
* Auxiliary processes don't run transactions, but they may need a
* resource owner anyway to manage buffer pins acquired outside
*/
if (worker->bgw_flags & BGWORKER_SHMEM_ACCESS)
{
- /*
- * Early initialization. Some of this could be useful even for
- * background workers that aren't using shared memory, but they can
- * call the individual startup routines for those subsystems if
- * needed.
- */
- BaseInit();
-
/*
* Create a per-backend PGPROC struct in shared memory, except in the
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must
#ifndef EXEC_BACKEND
InitProcess();
#endif
+
+ /*
+ * Early initialization. Some of this could be useful even for
+ * background workers that aren't using shared memory, but they can
+ * call the individual startup routines for those subsystems if
+ * needed.
+ */
+ BaseInit();
}
/*
* This is called during backend startup (whether standalone or under the
* postmaster). It sets up for this backend's access to the already-existing
* buffer pool.
- *
- * NB: this is called before InitProcess(), so we do not have a PGPROC and
- * cannot do LWLockAcquire; hence we can't actually access stuff in
- * shared memory yet. We are only initializing local data here.
- * (See also InitBufferPoolBackend)
*/
void
InitBufferPoolAccess(void)
PrivateRefCountHash = hash_create("PrivateRefCount", 100, &hash_ctl,
HASH_ELEM | HASH_BLOBS);
-}
-/*
- * InitBufferPoolBackend --- second-stage initialization of a new backend
- *
- * This is called after we have acquired a PGPROC and so can safely get
- * LWLocks. We don't currently need to do anything at this stage ...
- * except register a shmem-exit callback. AtProcExit_Buffers needs LWLock
- * access, and thereby has to be called at the corresponding phase of
- * backend shutdown.
- */
-void
-InitBufferPoolBackend(void)
-{
+ /*
+ * AtProcExit_Buffers needs LWLock access, and thereby has to be called at
+ * the corresponding phase of backend shutdown.
+ */
+ Assert(MyProc != NULL);
on_shmem_exit(AtProcExit_Buffers, 0);
}
/* Initialize MaxBackends (if under postmaster, was done already) */
InitializeMaxBackends();
- }
- /* Early initialization */
- BaseInit();
+ CreateSharedMemoryAndSemaphores();
+ }
/*
* Create a per-backend PGPROC struct in shared memory, except in the
InitProcess();
#endif
+ /* Early initialization */
+ BaseInit();
+
/* We need to allow SIGINT, etc during the initial transaction */
PG_SETMASK(&UnBlockSig);
static HeapTuple GetDatabaseTupleByOid(Oid dboid);
static void PerformAuthentication(Port *port);
static void CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connections);
-static void InitCommunication(void);
static void ShutdownPostgres(int code, Datum arg);
static void StatementTimeoutHandler(void);
static void LockTimeoutHandler(void);
}
-
-/* --------------------------------
- * InitCommunication
- *
- * This routine initializes stuff needed for ipc, locking, etc.
- * it should be called something more informative.
- * --------------------------------
- */
-static void
-InitCommunication(void)
-{
- /*
- * initialize shared memory and semaphores appropriately.
- */
- if (!IsUnderPostmaster) /* postmaster already did this */
- {
- /*
- * We're running a postgres bootstrap process or a standalone backend,
- * so we need to set up shmem.
- */
- CreateSharedMemoryAndSemaphores();
- }
-}
-
-
/*
* pg_split_opts -- split a string of options and append it to an argv array
*
void
BaseInit(void)
{
+ Assert(MyProc != NULL);
+
/*
- * Attach to shared memory and semaphores, and initialize our
- * input/output/debugging file descriptors.
+ * Initialize our input/output/debugging file descriptors.
*/
- InitCommunication();
DebugFileOpen();
/* Do local initialization of file, storage and buffer managers */
RegisterTimeout(CLIENT_CONNECTION_CHECK_TIMEOUT, ClientCheckTimeoutHandler);
}
- /*
- * bufmgr needs another initialization call too
- */
- InitBufferPoolBackend();
-
/*
* Initialize local process's access to XLOG.
*/
extern void InitBufferPool(void);
extern void InitBufferPoolAccess(void);
-extern void InitBufferPoolBackend(void);
extern void AtEOXact_Buffers(bool isCommit);
extern void PrintBufferLeakWarning(Buffer buffer);
extern void CheckPointBuffers(int flags);