@@ -418,7 +418,19 @@ static void HandleChildCrash(int pid, int exitstatus, const char *procname);
418418static void LogChildExit (int lev , const char * procname ,
419419 int pid , int exitstatus );
420420static void PostmasterStateMachine (void );
421- static void BackendInitialize (Port * port );
421+
422+ /* Return value of canAcceptConnections() */
423+ typedef enum CAC_state
424+ {
425+ CAC_OK ,
426+ CAC_STARTUP ,
427+ CAC_SHUTDOWN ,
428+ CAC_RECOVERY ,
429+ CAC_NOTCONSISTENT ,
430+ CAC_TOOMANY ,
431+ } CAC_state ;
432+
433+ static void BackendInitialize (Port * port , CAC_state cac );
422434static void BackendRun (Port * port ) pg_attribute_noreturn ();
423435static void ExitPostmaster (int status ) pg_attribute_noreturn ();
424436static int ServerLoop (void );
@@ -477,7 +489,7 @@ typedef struct
477489} win32_deadchild_waitinfo ;
478490#endif /* WIN32 */
479491
480- static pid_t backend_forkexec (Port * port );
492+ static pid_t backend_forkexec (Port * port , CAC_state cac );
481493static pid_t internal_forkexec (int argc , char * argv [], Port * port , BackgroundWorker * worker );
482494
483495/* Type for a socket that can be inherited to a client process */
@@ -4087,6 +4099,7 @@ BackendStartup(Port *port)
40874099{
40884100 Backend * bn ; /* for backend cleanup */
40894101 pid_t pid ;
4102+ CAC_state cac ;
40904103
40914104 /*
40924105 * Create backend data structure. Better before the fork() so we can
@@ -4118,8 +4131,8 @@ BackendStartup(Port *port)
41184131 bn -> cancel_key = MyCancelKey ;
41194132
41204133 /* Pass down canAcceptConnections state */
4121- port -> canAcceptConnections = canAcceptConnections (BACKEND_TYPE_NORMAL );
4122- bn -> dead_end = (port -> canAcceptConnections != CAC_OK );
4134+ cac = canAcceptConnections (BACKEND_TYPE_NORMAL );
4135+ bn -> dead_end = (cac != CAC_OK );
41234136
41244137 /*
41254138 * Unless it's a dead_end child, assign it a child slot number
@@ -4133,7 +4146,7 @@ BackendStartup(Port *port)
41334146 bn -> bgworker_notify = false;
41344147
41354148#ifdef EXEC_BACKEND
4136- pid = backend_forkexec (port );
4149+ pid = backend_forkexec (port , cac );
41374150#else /* !EXEC_BACKEND */
41384151 pid = fork_process ();
41394152 if (pid == 0 ) /* child */
@@ -4145,7 +4158,7 @@ BackendStartup(Port *port)
41454158 ClosePostmasterPorts (false);
41464159
41474160 /* Perform additional initialization and collect startup packet */
4148- BackendInitialize (port );
4161+ BackendInitialize (port , cac );
41494162
41504163 /* And run the backend */
41514164 BackendRun (port );
@@ -4232,7 +4245,7 @@ report_fork_failure_to_client(Port *port, int errnum)
42324245 * but have not yet set up most of our local pointers to shmem structures.
42334246 */
42344247static void
4235- BackendInitialize (Port * port )
4248+ BackendInitialize (Port * port , CAC_state cac )
42364249{
42374250 int status ;
42384251 int ret ;
@@ -4367,7 +4380,7 @@ BackendInitialize(Port *port)
43674380 */
43684381 if (status == STATUS_OK )
43694382 {
4370- switch (port -> canAcceptConnections )
4383+ switch (cac )
43714384 {
43724385 case CAC_STARTUP :
43734386 ereport (FATAL ,
@@ -4506,15 +4519,19 @@ postmaster_forkexec(int argc, char *argv[])
45064519 * returns the pid of the fork/exec'd process, or -1 on failure
45074520 */
45084521static pid_t
4509- backend_forkexec (Port * port )
4522+ backend_forkexec (Port * port , CAC_state cac )
45104523{
4511- char * av [4 ];
4524+ char * av [5 ];
45124525 int ac = 0 ;
4526+ char cacbuf [10 ];
45134527
45144528 av [ac ++ ] = "postgres" ;
45154529 av [ac ++ ] = "--forkbackend" ;
45164530 av [ac ++ ] = NULL ; /* filled in by internal_forkexec */
45174531
4532+ snprintf (cacbuf , sizeof (cacbuf ), "%d" , (int ) cac );
4533+ av [ac ++ ] = cacbuf ;
4534+
45184535 av [ac ] = NULL ;
45194536 Assert (ac < lengthof (av ));
45204537
@@ -4921,7 +4938,10 @@ SubPostmasterMain(int argc, char *argv[])
49214938 /* Run backend or appropriate child */
49224939 if (strcmp (argv [1 ], "--forkbackend" ) == 0 )
49234940 {
4924- Assert (argc == 3 ); /* shouldn't be any more args */
4941+ CAC_state cac ;
4942+
4943+ Assert (argc == 4 );
4944+ cac = (CAC_state ) atoi (argv [3 ]);
49254945
49264946 /*
49274947 * Need to reinitialize the SSL library in the backend, since the
@@ -4955,7 +4975,7 @@ SubPostmasterMain(int argc, char *argv[])
49554975 * PGPROC slots, we have already initialized libpq and are able to
49564976 * report the error to the client.
49574977 */
4958- BackendInitialize (port );
4978+ BackendInitialize (port , cac );
49594979
49604980 /* Restore basic shared memory pointers */
49614981 InitShmemAccess (UsedShmemSegAddr );
0 commit comments