Skip to content

Commit fd5e8b4

Browse files
committed
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
1 parent 388491f commit fd5e8b4

File tree

5 files changed

+35
-63
lines changed

5 files changed

+35
-63
lines changed

src/backend/postmaster/autovacuum.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,10 @@ AutoVacLauncherMain(int argc, char *argv[])
476476
pqsignal(SIGCHLD, SIG_DFL);
477477

478478
/*
479-
* Create a per-backend PGPROC struct in shared memory, except in the
480-
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do
481-
* this before we can use LWLocks (and in the EXEC_BACKEND case we already
482-
* had to do some stuff with LWLocks).
479+
* Create a per-backend PGPROC struct in shared memory. We must do this
480+
* before we can use LWLocks or access any shared memory.
483481
*/
484-
#ifndef EXEC_BACKEND
485482
InitProcess();
486-
#endif
487483

488484
/* Early initialization */
489485
BaseInit();
@@ -1548,14 +1544,10 @@ AutoVacWorkerMain(int argc, char *argv[])
15481544
pqsignal(SIGCHLD, SIG_DFL);
15491545

15501546
/*
1551-
* Create a per-backend PGPROC struct in shared memory, except in the
1552-
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do
1553-
* this before we can use LWLocks (and in the EXEC_BACKEND case we already
1554-
* had to do some stuff with LWLocks).
1547+
* Create a per-backend PGPROC struct in shared memory. We must do this
1548+
* before we can use LWLocks or access any shared memory.
15551549
*/
1556-
#ifndef EXEC_BACKEND
15571550
InitProcess();
1558-
#endif
15591551

15601552
/* Early initialization */
15611553
BaseInit();

src/backend/postmaster/auxprocess.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,9 @@ AuxiliaryProcessMain(AuxProcType auxtype)
9797
*/
9898

9999
/*
100-
* Create a PGPROC so we can use LWLocks. In the EXEC_BACKEND case, this
101-
* was already done by SubPostmasterMain().
100+
* Create a PGPROC so we can use LWLocks and access shared memory.
102101
*/
103-
#ifndef EXEC_BACKEND
104102
InitAuxiliaryProcess();
105-
#endif
106103

107104
BaseInit();
108105

src/backend/postmaster/bgworker.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -810,14 +810,10 @@ BackgroundWorkerMain(void)
810810
PG_exception_stack = &local_sigjmp_buf;
811811

812812
/*
813-
* Create a per-backend PGPROC struct in shared memory, except in the
814-
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must do
815-
* this before we can use LWLocks (and in the EXEC_BACKEND case we already
816-
* had to do some stuff with LWLocks).
813+
* Create a per-backend PGPROC struct in shared memory. We must do this
814+
* before we can use LWLocks or access any shared memory.
817815
*/
818-
#ifndef EXEC_BACKEND
819816
InitProcess();
820-
#endif
821817

822818
/*
823819
* Early initialization.

src/backend/postmaster/postmaster.c

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4100,15 +4100,6 @@ BackendStartup(Port *port)
41004100
/* Perform additional initialization and collect startup packet */
41014101
BackendInitialize(port);
41024102

4103-
/*
4104-
* Create a per-backend PGPROC struct in shared memory. We must do
4105-
* this before we can use LWLocks. In the !EXEC_BACKEND case (here)
4106-
* this could be delayed a bit further, but EXEC_BACKEND needs to do
4107-
* stuff with LWLocks before PostgresMain(), so we do it here as well
4108-
* for symmetry.
4109-
*/
4110-
InitProcess();
4111-
41124103
/* And run the backend */
41134104
BackendRun(port);
41144105
}
@@ -4419,6 +4410,12 @@ BackendInitialize(Port *port)
44194410
static void
44204411
BackendRun(Port *port)
44214412
{
4413+
/*
4414+
* Create a per-backend PGPROC struct in shared memory. We must do this
4415+
* before we can use LWLocks or access any shared memory.
4416+
*/
4417+
InitProcess();
4418+
44224419
/*
44234420
* Make sure we aren't in PostmasterContext anymore. (We can't delete it
44244421
* just yet, though, because InitPostgres will need the HBA data.)
@@ -4918,12 +4915,6 @@ SubPostmasterMain(int argc, char *argv[])
49184915
/* Restore basic shared memory pointers */
49194916
InitShmemAccess(UsedShmemSegAddr);
49204917

4921-
/* Need a PGPROC to run AttachSharedMemoryStructs */
4922-
InitProcess();
4923-
4924-
/* Attach process to shared data structures */
4925-
AttachSharedMemoryStructs();
4926-
49274918
/* And run the backend */
49284919
BackendRun(port); /* does not return */
49294920
}
@@ -4936,12 +4927,6 @@ SubPostmasterMain(int argc, char *argv[])
49364927
/* Restore basic shared memory pointers */
49374928
InitShmemAccess(UsedShmemSegAddr);
49384929

4939-
/* Need a PGPROC to run AttachSharedMemoryStructs */
4940-
InitAuxiliaryProcess();
4941-
4942-
/* Attach process to shared data structures */
4943-
AttachSharedMemoryStructs();
4944-
49454930
auxtype = atoi(argv[3]);
49464931
AuxiliaryProcessMain(auxtype); /* does not return */
49474932
}
@@ -4950,25 +4935,13 @@ SubPostmasterMain(int argc, char *argv[])
49504935
/* Restore basic shared memory pointers */
49514936
InitShmemAccess(UsedShmemSegAddr);
49524937

4953-
/* Need a PGPROC to run AttachSharedMemoryStructs */
4954-
InitProcess();
4955-
4956-
/* Attach process to shared data structures */
4957-
AttachSharedMemoryStructs();
4958-
49594938
AutoVacLauncherMain(argc - 2, argv + 2); /* does not return */
49604939
}
49614940
if (strcmp(argv[1], "--forkavworker") == 0)
49624941
{
49634942
/* Restore basic shared memory pointers */
49644943
InitShmemAccess(UsedShmemSegAddr);
49654944

4966-
/* Need a PGPROC to run AttachSharedMemoryStructs */
4967-
InitProcess();
4968-
4969-
/* Attach process to shared data structures */
4970-
AttachSharedMemoryStructs();
4971-
49724945
AutoVacWorkerMain(argc - 2, argv + 2); /* does not return */
49734946
}
49744947
if (strcmp(argv[1], "--forkbgworker") == 0)
@@ -4979,12 +4952,6 @@ SubPostmasterMain(int argc, char *argv[])
49794952
/* Restore basic shared memory pointers */
49804953
InitShmemAccess(UsedShmemSegAddr);
49814954

4982-
/* Need a PGPROC to run AttachSharedMemoryStructs */
4983-
InitProcess();
4984-
4985-
/* Attach process to shared data structures */
4986-
AttachSharedMemoryStructs();
4987-
49884955
MyBgworkerEntry = worker;
49894956
BackgroundWorkerMain();
49904957
}

src/backend/storage/lmgr/proc.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ InitProcGlobal(void)
291291
}
292292

293293
/*
294-
* InitProcess -- initialize a per-process data structure for this backend
294+
* InitProcess -- initialize a per-process PGPROC entry for this backend
295295
*/
296296
void
297297
InitProcess(void)
@@ -461,6 +461,16 @@ InitProcess(void)
461461
*/
462462
InitLWLockAccess();
463463
InitDeadLockChecking();
464+
465+
#ifdef EXEC_BACKEND
466+
467+
/*
468+
* Initialize backend-local pointers to all the shared data structures.
469+
* (We couldn't do this until now because it needs LWLocks.)
470+
*/
471+
if (IsUnderPostmaster)
472+
AttachSharedMemoryStructs();
473+
#endif
464474
}
465475

466476
/*
@@ -487,7 +497,7 @@ InitProcessPhase2(void)
487497
}
488498

489499
/*
490-
* InitAuxiliaryProcess -- create a per-auxiliary-process data structure
500+
* InitAuxiliaryProcess -- create a PGPROC entry for an auxiliary process
491501
*
492502
* This is called by bgwriter and similar processes so that they will have a
493503
* MyProc value that's real enough to let them wait for LWLocks. The PGPROC
@@ -621,6 +631,16 @@ InitAuxiliaryProcess(void)
621631
* acquired in aux processes.)
622632
*/
623633
InitLWLockAccess();
634+
635+
#ifdef EXEC_BACKEND
636+
637+
/*
638+
* Initialize backend-local pointers to all the shared data structures.
639+
* (We couldn't do this until now because it needs LWLocks.)
640+
*/
641+
if (IsUnderPostmaster)
642+
AttachSharedMemoryStructs();
643+
#endif
624644
}
625645

626646
/*

0 commit comments

Comments
 (0)