@@ -418,7 +418,19 @@ static void HandleChildCrash(int pid, int exitstatus, const char *procname);
418
418
static void LogChildExit (int lev , const char * procname ,
419
419
int pid , int exitstatus );
420
420
static 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 );
422
434
static void BackendRun (Port * port ) pg_attribute_noreturn ();
423
435
static void ExitPostmaster (int status ) pg_attribute_noreturn ();
424
436
static int ServerLoop (void );
@@ -477,7 +489,7 @@ typedef struct
477
489
} win32_deadchild_waitinfo ;
478
490
#endif /* WIN32 */
479
491
480
- static pid_t backend_forkexec (Port * port );
492
+ static pid_t backend_forkexec (Port * port , CAC_state cac );
481
493
static pid_t internal_forkexec (int argc , char * argv [], Port * port , BackgroundWorker * worker );
482
494
483
495
/* Type for a socket that can be inherited to a client process */
@@ -4087,6 +4099,7 @@ BackendStartup(Port *port)
4087
4099
{
4088
4100
Backend * bn ; /* for backend cleanup */
4089
4101
pid_t pid ;
4102
+ CAC_state cac ;
4090
4103
4091
4104
/*
4092
4105
* Create backend data structure. Better before the fork() so we can
@@ -4118,8 +4131,8 @@ BackendStartup(Port *port)
4118
4131
bn -> cancel_key = MyCancelKey ;
4119
4132
4120
4133
/* 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 );
4123
4136
4124
4137
/*
4125
4138
* Unless it's a dead_end child, assign it a child slot number
@@ -4133,7 +4146,7 @@ BackendStartup(Port *port)
4133
4146
bn -> bgworker_notify = false;
4134
4147
4135
4148
#ifdef EXEC_BACKEND
4136
- pid = backend_forkexec (port );
4149
+ pid = backend_forkexec (port , cac );
4137
4150
#else /* !EXEC_BACKEND */
4138
4151
pid = fork_process ();
4139
4152
if (pid == 0 ) /* child */
@@ -4145,7 +4158,7 @@ BackendStartup(Port *port)
4145
4158
ClosePostmasterPorts (false);
4146
4159
4147
4160
/* Perform additional initialization and collect startup packet */
4148
- BackendInitialize (port );
4161
+ BackendInitialize (port , cac );
4149
4162
4150
4163
/* And run the backend */
4151
4164
BackendRun (port );
@@ -4232,7 +4245,7 @@ report_fork_failure_to_client(Port *port, int errnum)
4232
4245
* but have not yet set up most of our local pointers to shmem structures.
4233
4246
*/
4234
4247
static void
4235
- BackendInitialize (Port * port )
4248
+ BackendInitialize (Port * port , CAC_state cac )
4236
4249
{
4237
4250
int status ;
4238
4251
int ret ;
@@ -4367,7 +4380,7 @@ BackendInitialize(Port *port)
4367
4380
*/
4368
4381
if (status == STATUS_OK )
4369
4382
{
4370
- switch (port -> canAcceptConnections )
4383
+ switch (cac )
4371
4384
{
4372
4385
case CAC_STARTUP :
4373
4386
ereport (FATAL ,
@@ -4506,15 +4519,19 @@ postmaster_forkexec(int argc, char *argv[])
4506
4519
* returns the pid of the fork/exec'd process, or -1 on failure
4507
4520
*/
4508
4521
static pid_t
4509
- backend_forkexec (Port * port )
4522
+ backend_forkexec (Port * port , CAC_state cac )
4510
4523
{
4511
- char * av [4 ];
4524
+ char * av [5 ];
4512
4525
int ac = 0 ;
4526
+ char cacbuf [10 ];
4513
4527
4514
4528
av [ac ++ ] = "postgres" ;
4515
4529
av [ac ++ ] = "--forkbackend" ;
4516
4530
av [ac ++ ] = NULL ; /* filled in by internal_forkexec */
4517
4531
4532
+ snprintf (cacbuf , sizeof (cacbuf ), "%d" , (int ) cac );
4533
+ av [ac ++ ] = cacbuf ;
4534
+
4518
4535
av [ac ] = NULL ;
4519
4536
Assert (ac < lengthof (av ));
4520
4537
@@ -4921,7 +4938,10 @@ SubPostmasterMain(int argc, char *argv[])
4921
4938
/* Run backend or appropriate child */
4922
4939
if (strcmp (argv [1 ], "--forkbackend" ) == 0 )
4923
4940
{
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 ]);
4925
4945
4926
4946
/*
4927
4947
* Need to reinitialize the SSL library in the backend, since the
@@ -4955,7 +4975,7 @@ SubPostmasterMain(int argc, char *argv[])
4955
4975
* PGPROC slots, we have already initialized libpq and are able to
4956
4976
* report the error to the client.
4957
4977
*/
4958
- BackendInitialize (port );
4978
+ BackendInitialize (port , cac );
4959
4979
4960
4980
/* Restore basic shared memory pointers */
4961
4981
InitShmemAccess (UsedShmemSegAddr );
0 commit comments