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

Commit 5b00786

Browse files
committed
Pass MyPMChildSlot as an explicit argument to child process
All the other global variables passed from postmaster to child have the same value in all the processes, while MyPMChildSlot is more like a parameter to each child process. Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://www.postgresql.org/message-id/a102f15f-eac4-4ff2-af02-f9ff209ec66f@iki.fi
1 parent a78af04 commit 5b00786

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

src/backend/postmaster/launch_backend.c

+24-13
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ typedef int InheritableSocket;
8888
typedef struct
8989
{
9090
char DataDir[MAXPGPATH];
91-
int MyPMChildSlot;
9291
#ifndef WIN32
9392
unsigned long UsedShmemSegID;
9493
#else
@@ -130,6 +129,8 @@ typedef struct
130129
char my_exec_path[MAXPGPATH];
131130
char pkglib_path[MAXPGPATH];
132131

132+
int MyPMChildSlot;
133+
133134
/*
134135
* These are only used by backend processes, but are here because passing
135136
* a socket needs some special handling on Windows. 'client_sock' is an
@@ -151,13 +152,16 @@ typedef struct
151152
static void read_backend_variables(char *id, char **startup_data, size_t *startup_data_len);
152153
static void restore_backend_variables(BackendParameters *param);
153154

154-
static bool save_backend_variables(BackendParameters *param, ClientSocket *client_sock,
155+
static bool save_backend_variables(BackendParameters *param, int child_slot,
156+
ClientSocket *client_sock,
155157
#ifdef WIN32
156158
HANDLE childProcess, pid_t childPid,
157159
#endif
158160
char *startup_data, size_t startup_data_len);
159161

160-
static pid_t internal_forkexec(const char *child_kind, char *startup_data, size_t startup_data_len, ClientSocket *client_sock);
162+
static pid_t internal_forkexec(const char *child_kind, int child_slot,
163+
char *startup_data, size_t startup_data_len,
164+
ClientSocket *client_sock);
161165

162166
#endif /* EXEC_BACKEND */
163167

@@ -215,11 +219,12 @@ PostmasterChildName(BackendType child_type)
215219
* appropriate, and fds and other resources that we've inherited from
216220
* postmaster that are not needed in a child process have been closed.
217221
*
218-
* 'startup_data' is an optional contiguous chunk of data that is passed to
219-
* the child process.
222+
* 'child_slot' is the PMChildFlags array index reserved for the child
223+
* process. 'startup_data' is an optional contiguous chunk of data that is
224+
* passed to the child process.
220225
*/
221226
pid_t
222-
postmaster_child_launch(BackendType child_type,
227+
postmaster_child_launch(BackendType child_type, int child_slot,
223228
char *startup_data, size_t startup_data_len,
224229
ClientSocket *client_sock)
225230
{
@@ -228,7 +233,7 @@ postmaster_child_launch(BackendType child_type,
228233
Assert(IsPostmasterEnvironment && !IsUnderPostmaster);
229234

230235
#ifdef EXEC_BACKEND
231-
pid = internal_forkexec(child_process_kinds[child_type].name,
236+
pid = internal_forkexec(child_process_kinds[child_type].name, child_slot,
232237
startup_data, startup_data_len, client_sock);
233238
/* the child process will arrive in SubPostmasterMain */
234239
#else /* !EXEC_BACKEND */
@@ -256,6 +261,7 @@ postmaster_child_launch(BackendType child_type,
256261
*/
257262
MemoryContextSwitchTo(TopMemoryContext);
258263

264+
MyPMChildSlot = child_slot;
259265
if (client_sock)
260266
{
261267
MyClientSocket = palloc(sizeof(ClientSocket));
@@ -282,7 +288,8 @@ postmaster_child_launch(BackendType child_type,
282288
* - fork():s, and then exec():s the child process
283289
*/
284290
static pid_t
285-
internal_forkexec(const char *child_kind, char *startup_data, size_t startup_data_len, ClientSocket *client_sock)
291+
internal_forkexec(const char *child_kind, int child_slot,
292+
char *startup_data, size_t startup_data_len, ClientSocket *client_sock)
286293
{
287294
static unsigned long tmpBackendFileNum = 0;
288295
pid_t pid;
@@ -302,7 +309,7 @@ internal_forkexec(const char *child_kind, char *startup_data, size_t startup_dat
302309
*/
303310
paramsz = SizeOfBackendParameters(startup_data_len);
304311
param = palloc0(paramsz);
305-
if (!save_backend_variables(param, client_sock, startup_data, startup_data_len))
312+
if (!save_backend_variables(param, child_slot, client_sock, startup_data, startup_data_len))
306313
{
307314
pfree(param);
308315
return -1; /* log made by save_backend_variables */
@@ -391,7 +398,8 @@ internal_forkexec(const char *child_kind, char *startup_data, size_t startup_dat
391398
* file is complete.
392399
*/
393400
static pid_t
394-
internal_forkexec(const char *child_kind, char *startup_data, size_t startup_data_len, ClientSocket *client_sock)
401+
internal_forkexec(const char *child_kind, int child_slot,
402+
char *startup_data, size_t startup_data_len, ClientSocket *client_sock)
395403
{
396404
int retry_count = 0;
397405
STARTUPINFO si;
@@ -472,7 +480,9 @@ internal_forkexec(const char *child_kind, char *startup_data, size_t startup_dat
472480
return -1;
473481
}
474482

475-
if (!save_backend_variables(param, client_sock, pi.hProcess, pi.dwProcessId, startup_data, startup_data_len))
483+
if (!save_backend_variables(param, child_slot, client_sock,
484+
pi.hProcess, pi.dwProcessId,
485+
startup_data, startup_data_len))
476486
{
477487
/*
478488
* log made by save_backend_variables, but we have to clean up the
@@ -684,7 +694,8 @@ static void read_inheritable_socket(SOCKET *dest, InheritableSocket *src);
684694

685695
/* Save critical backend variables into the BackendParameters struct */
686696
static bool
687-
save_backend_variables(BackendParameters *param, ClientSocket *client_sock,
697+
save_backend_variables(BackendParameters *param,
698+
int child_slot, ClientSocket *client_sock,
688699
#ifdef WIN32
689700
HANDLE childProcess, pid_t childPid,
690701
#endif
@@ -701,7 +712,7 @@ save_backend_variables(BackendParameters *param, ClientSocket *client_sock,
701712

702713
strlcpy(param->DataDir, DataDir, MAXPGPATH);
703714

704-
param->MyPMChildSlot = MyPMChildSlot;
715+
param->MyPMChildSlot = child_slot;
705716

706717
#ifdef WIN32
707718
param->ShmemProtectiveRegion = ShmemProtectiveRegion;

src/backend/postmaster/postmaster.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -3374,8 +3374,7 @@ BackendStartup(ClientSocket *client_sock)
33743374
/* Hasn't asked to be notified about any bgworkers yet */
33753375
bn->bgworker_notify = false;
33763376

3377-
MyPMChildSlot = bn->child_slot;
3378-
pid = postmaster_child_launch(bn->bkend_type,
3377+
pid = postmaster_child_launch(bn->bkend_type, bn->child_slot,
33793378
(char *) &startup_data, sizeof(startup_data),
33803379
client_sock);
33813380
if (pid < 0)
@@ -3700,8 +3699,7 @@ StartChildProcess(BackendType type)
37003699
return NULL;
37013700
}
37023701

3703-
MyPMChildSlot = pmchild->child_slot;
3704-
pid = postmaster_child_launch(type, NULL, 0, NULL);
3702+
pid = postmaster_child_launch(type, pmchild->child_slot, NULL, 0, NULL);
37053703
if (pid < 0)
37063704
{
37073705
/* in parent, fork failed */
@@ -3878,8 +3876,8 @@ StartBackgroundWorker(RegisteredBgWorker *rw)
38783876
(errmsg_internal("starting background worker process \"%s\"",
38793877
rw->rw_worker.bgw_name)));
38803878

3881-
MyPMChildSlot = bn->child_slot;
3882-
worker_pid = postmaster_child_launch(B_BG_WORKER, (char *) &rw->rw_worker, sizeof(BackgroundWorker), NULL);
3879+
worker_pid = postmaster_child_launch(B_BG_WORKER, bn->child_slot,
3880+
(char *) &rw->rw_worker, sizeof(BackgroundWorker), NULL);
38833881
if (worker_pid == -1)
38843882
{
38853883
/* in postmaster, fork failed ... */

src/backend/postmaster/syslogger.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -694,14 +694,15 @@ SysLogger_Start(int child_slot)
694694
pfree(filename);
695695
}
696696

697-
MyPMChildSlot = child_slot;
698697
#ifdef EXEC_BACKEND
699698
startup_data.syslogFile = syslogger_fdget(syslogFile);
700699
startup_data.csvlogFile = syslogger_fdget(csvlogFile);
701700
startup_data.jsonlogFile = syslogger_fdget(jsonlogFile);
702-
sysloggerPid = postmaster_child_launch(B_LOGGER, (char *) &startup_data, sizeof(startup_data), NULL);
701+
sysloggerPid = postmaster_child_launch(B_LOGGER, child_slot,
702+
(char *) &startup_data, sizeof(startup_data), NULL);
703703
#else
704-
sysloggerPid = postmaster_child_launch(B_LOGGER, NULL, 0, NULL);
704+
sysloggerPid = postmaster_child_launch(B_LOGGER, child_slot,
705+
NULL, 0, NULL);
705706
#endif /* EXEC_BACKEND */
706707

707708
if (sysloggerPid == -1)

src/include/postmaster/postmaster.h

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ extern PGDLLIMPORT struct ClientSocket *MyClientSocket;
108108

109109
/* prototypes for functions in launch_backend.c */
110110
extern pid_t postmaster_child_launch(BackendType child_type,
111+
int child_slot,
111112
char *startup_data,
112113
size_t startup_data_len,
113114
struct ClientSocket *client_sock);

0 commit comments

Comments
 (0)