Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d162c3a

Browse files
committed
Pass CAC as an argument to the backend process
We used to smuggle it to the child process in the Port struct, but it seems better to pass it down as a separate argument. This paves the way for the next commit, which moves the initialization of the Port struct to the backend process, after forking. Reviewed-by: Tristan Partin, Andres Freund Discussion: https://www.postgresql.org/message-id/7a59b073-5b5b-151e-7ed3-8b01ff7ce9ef@iki.fi
1 parent 73f7fb2 commit d162c3a

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,19 @@ static void HandleChildCrash(int pid, int exitstatus, const char *procname);
418418
static void LogChildExit(int lev, const char *procname,
419419
int pid, int exitstatus);
420420
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);
422434
static void BackendRun(Port *port) pg_attribute_noreturn();
423435
static void ExitPostmaster(int status) pg_attribute_noreturn();
424436
static 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);
481493
static 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
*/
42344247
static 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
*/
45084521
static 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);

src/include/libpq/libpq-be.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,6 @@ typedef struct
5858
#include "libpq/pqcomm.h"
5959

6060

61-
typedef enum CAC_state
62-
{
63-
CAC_OK,
64-
CAC_STARTUP,
65-
CAC_SHUTDOWN,
66-
CAC_RECOVERY,
67-
CAC_NOTCONSISTENT,
68-
CAC_TOOMANY,
69-
} CAC_state;
70-
71-
7261
/*
7362
* GSSAPI specific state information
7463
*/
@@ -156,7 +145,6 @@ typedef struct Port
156145
int remote_hostname_resolv; /* see above */
157146
int remote_hostname_errcode; /* see above */
158147
char *remote_port; /* text rep of remote port */
159-
CAC_state canAcceptConnections; /* postmaster connection status */
160148

161149
/*
162150
* Information that needs to be saved from the startup packet and passed

0 commit comments

Comments
 (0)