1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-27 22:56:53 +03:00

Refactor how InitProcess is called

The order of process initialization steps is now more consistent
between !EXEC_BACKEND and EXEC_BACKEND modes. InitProcess() is called
at the same place in either mode. We can now also move the
AttachSharedMemoryStructs() call into InitProcess() itself. This
reduces the number of "#ifdef EXEC_BACKEND" blocks.

Reviewed-by: Tristan Partin, Andres Freund, Alexander Lakhin
Discussion: https://www.postgresql.org/message-id/7a59b073-5b5b-151e-7ed3-8b01ff7ce9ef@iki.fi
This commit is contained in:
Heikki Linnakangas 2023-12-03 16:39:18 +02:00
parent 388491f1e5
commit fd5e8b440d
5 changed files with 35 additions and 63 deletions

View File

@ -476,14 +476,10 @@ AutoVacLauncherMain(int argc, char *argv[])
pqsignal(SIGCHLD, SIG_DFL); pqsignal(SIGCHLD, SIG_DFL);
/* /*
* Create a per-backend PGPROC struct in shared memory, except in the * Create a per-backend PGPROC struct in shared memory. We must do this
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do * before we can use LWLocks or access any shared memory.
* this before we can use LWLocks (and in the EXEC_BACKEND case we already
* had to do some stuff with LWLocks).
*/ */
#ifndef EXEC_BACKEND
InitProcess(); InitProcess();
#endif
/* Early initialization */ /* Early initialization */
BaseInit(); BaseInit();
@ -1548,14 +1544,10 @@ AutoVacWorkerMain(int argc, char *argv[])
pqsignal(SIGCHLD, SIG_DFL); pqsignal(SIGCHLD, SIG_DFL);
/* /*
* Create a per-backend PGPROC struct in shared memory, except in the * Create a per-backend PGPROC struct in shared memory. We must do this
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do * before we can use LWLocks or access any shared memory.
* this before we can use LWLocks (and in the EXEC_BACKEND case we already
* had to do some stuff with LWLocks).
*/ */
#ifndef EXEC_BACKEND
InitProcess(); InitProcess();
#endif
/* Early initialization */ /* Early initialization */
BaseInit(); BaseInit();

View File

@ -97,12 +97,9 @@ AuxiliaryProcessMain(AuxProcType auxtype)
*/ */
/* /*
* Create a PGPROC so we can use LWLocks. In the EXEC_BACKEND case, this * Create a PGPROC so we can use LWLocks and access shared memory.
* was already done by SubPostmasterMain().
*/ */
#ifndef EXEC_BACKEND
InitAuxiliaryProcess(); InitAuxiliaryProcess();
#endif
BaseInit(); BaseInit();

View File

@ -810,14 +810,10 @@ BackgroundWorkerMain(void)
PG_exception_stack = &local_sigjmp_buf; PG_exception_stack = &local_sigjmp_buf;
/* /*
* Create a per-backend PGPROC struct in shared memory, except in the * Create a per-backend PGPROC struct in shared memory. We must do this
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do * before we can use LWLocks or access any shared memory.
* this before we can use LWLocks (and in the EXEC_BACKEND case we already
* had to do some stuff with LWLocks).
*/ */
#ifndef EXEC_BACKEND
InitProcess(); InitProcess();
#endif
/* /*
* Early initialization. * Early initialization.

View File

@ -4100,15 +4100,6 @@ BackendStartup(Port *port)
/* Perform additional initialization and collect startup packet */ /* Perform additional initialization and collect startup packet */
BackendInitialize(port); BackendInitialize(port);
/*
* Create a per-backend PGPROC struct in shared memory. We must do
* this before we can use LWLocks. In the !EXEC_BACKEND case (here)
* this could be delayed a bit further, but EXEC_BACKEND needs to do
* stuff with LWLocks before PostgresMain(), so we do it here as well
* for symmetry.
*/
InitProcess();
/* And run the backend */ /* And run the backend */
BackendRun(port); BackendRun(port);
} }
@ -4419,6 +4410,12 @@ BackendInitialize(Port *port)
static void static void
BackendRun(Port *port) BackendRun(Port *port)
{ {
/*
* Create a per-backend PGPROC struct in shared memory. We must do this
* before we can use LWLocks or access any shared memory.
*/
InitProcess();
/* /*
* Make sure we aren't in PostmasterContext anymore. (We can't delete it * Make sure we aren't in PostmasterContext anymore. (We can't delete it
* just yet, though, because InitPostgres will need the HBA data.) * just yet, though, because InitPostgres will need the HBA data.)
@ -4918,12 +4915,6 @@ SubPostmasterMain(int argc, char *argv[])
/* Restore basic shared memory pointers */ /* Restore basic shared memory pointers */
InitShmemAccess(UsedShmemSegAddr); InitShmemAccess(UsedShmemSegAddr);
/* Need a PGPROC to run AttachSharedMemoryStructs */
InitProcess();
/* Attach process to shared data structures */
AttachSharedMemoryStructs();
/* And run the backend */ /* And run the backend */
BackendRun(port); /* does not return */ BackendRun(port); /* does not return */
} }
@ -4936,12 +4927,6 @@ SubPostmasterMain(int argc, char *argv[])
/* Restore basic shared memory pointers */ /* Restore basic shared memory pointers */
InitShmemAccess(UsedShmemSegAddr); InitShmemAccess(UsedShmemSegAddr);
/* Need a PGPROC to run AttachSharedMemoryStructs */
InitAuxiliaryProcess();
/* Attach process to shared data structures */
AttachSharedMemoryStructs();
auxtype = atoi(argv[3]); auxtype = atoi(argv[3]);
AuxiliaryProcessMain(auxtype); /* does not return */ AuxiliaryProcessMain(auxtype); /* does not return */
} }
@ -4950,12 +4935,6 @@ SubPostmasterMain(int argc, char *argv[])
/* Restore basic shared memory pointers */ /* Restore basic shared memory pointers */
InitShmemAccess(UsedShmemSegAddr); InitShmemAccess(UsedShmemSegAddr);
/* Need a PGPROC to run AttachSharedMemoryStructs */
InitProcess();
/* Attach process to shared data structures */
AttachSharedMemoryStructs();
AutoVacLauncherMain(argc - 2, argv + 2); /* does not return */ AutoVacLauncherMain(argc - 2, argv + 2); /* does not return */
} }
if (strcmp(argv[1], "--forkavworker") == 0) if (strcmp(argv[1], "--forkavworker") == 0)
@ -4963,12 +4942,6 @@ SubPostmasterMain(int argc, char *argv[])
/* Restore basic shared memory pointers */ /* Restore basic shared memory pointers */
InitShmemAccess(UsedShmemSegAddr); InitShmemAccess(UsedShmemSegAddr);
/* Need a PGPROC to run AttachSharedMemoryStructs */
InitProcess();
/* Attach process to shared data structures */
AttachSharedMemoryStructs();
AutoVacWorkerMain(argc - 2, argv + 2); /* does not return */ AutoVacWorkerMain(argc - 2, argv + 2); /* does not return */
} }
if (strcmp(argv[1], "--forkbgworker") == 0) if (strcmp(argv[1], "--forkbgworker") == 0)
@ -4979,12 +4952,6 @@ SubPostmasterMain(int argc, char *argv[])
/* Restore basic shared memory pointers */ /* Restore basic shared memory pointers */
InitShmemAccess(UsedShmemSegAddr); InitShmemAccess(UsedShmemSegAddr);
/* Need a PGPROC to run AttachSharedMemoryStructs */
InitProcess();
/* Attach process to shared data structures */
AttachSharedMemoryStructs();
MyBgworkerEntry = worker; MyBgworkerEntry = worker;
BackgroundWorkerMain(); BackgroundWorkerMain();
} }

View File

@ -291,7 +291,7 @@ InitProcGlobal(void)
} }
/* /*
* InitProcess -- initialize a per-process data structure for this backend * InitProcess -- initialize a per-process PGPROC entry for this backend
*/ */
void void
InitProcess(void) InitProcess(void)
@ -461,6 +461,16 @@ InitProcess(void)
*/ */
InitLWLockAccess(); InitLWLockAccess();
InitDeadLockChecking(); InitDeadLockChecking();
#ifdef EXEC_BACKEND
/*
* Initialize backend-local pointers to all the shared data structures.
* (We couldn't do this until now because it needs LWLocks.)
*/
if (IsUnderPostmaster)
AttachSharedMemoryStructs();
#endif
} }
/* /*
@ -487,7 +497,7 @@ InitProcessPhase2(void)
} }
/* /*
* InitAuxiliaryProcess -- create a per-auxiliary-process data structure * InitAuxiliaryProcess -- create a PGPROC entry for an auxiliary process
* *
* This is called by bgwriter and similar processes so that they will have a * This is called by bgwriter and similar processes so that they will have a
* MyProc value that's real enough to let them wait for LWLocks. The PGPROC * MyProc value that's real enough to let them wait for LWLocks. The PGPROC
@ -621,6 +631,16 @@ InitAuxiliaryProcess(void)
* acquired in aux processes.) * acquired in aux processes.)
*/ */
InitLWLockAccess(); InitLWLockAccess();
#ifdef EXEC_BACKEND
/*
* Initialize backend-local pointers to all the shared data structures.
* (We couldn't do this until now because it needs LWLocks.)
*/
if (IsUnderPostmaster)
AttachSharedMemoryStructs();
#endif
} }
/* /*