1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-26 01:22:12 +03:00

Refactor postmaster child process launching

Introduce new postmaster_child_launch() function that deals with the
differences in EXEC_BACKEND mode.

Refactor the mechanism of passing information from the parent to child
process. Instead of using different command-line arguments when
launching the child process in EXEC_BACKEND mode, pass a
variable-length blob of startup data along with all the global
variables. The contents of that blob depend on the kind of child
process being launched. In !EXEC_BACKEND mode, we use the same blob,
but it's simply inherited from the parent to child process.

Reviewed-by: Tristan Partin, Andres Freund
Discussion: https://www.postgresql.org/message-id/7a59b073-5b5b-151e-7ed3-8b01ff7ce9ef@iki.fi
This commit is contained in:
Heikki Linnakangas
2024-03-18 11:35:08 +02:00
parent f1baed18bc
commit aafc05de1b
28 changed files with 676 additions and 1008 deletions

View File

@ -139,11 +139,6 @@ typedef struct RemoteSlot
ReplicationSlotInvalidationCause invalidated;
} RemoteSlot;
#ifdef EXEC_BACKEND
static pid_t slotsyncworker_forkexec(void);
#endif
NON_EXEC_STATIC void ReplSlotSyncWorkerMain(int argc, char *argv[]) pg_attribute_noreturn();
static void slotsync_failure_callback(int code, Datum arg);
/*
@ -1113,8 +1108,8 @@ wait_for_slot_activity(bool some_slot_updated)
* It connects to the primary server, fetches logical failover slots
* information periodically in order to create and sync the slots.
*/
NON_EXEC_STATIC void
ReplSlotSyncWorkerMain(int argc, char *argv[])
void
ReplSlotSyncWorkerMain(char *startup_data, size_t startup_data_len)
{
WalReceiverConn *wrconn = NULL;
char *dbname;
@ -1122,6 +1117,8 @@ ReplSlotSyncWorkerMain(int argc, char *argv[])
sigjmp_buf local_sigjmp_buf;
StringInfoData app_name;
Assert(startup_data_len == 0);
MyBackendType = B_SLOTSYNC_WORKER;
init_ps_display(NULL);
@ -1299,67 +1296,6 @@ ReplSlotSyncWorkerMain(int argc, char *argv[])
Assert(false);
}
/*
* Main entry point for slot sync worker process, to be called from the
* postmaster.
*/
int
StartSlotSyncWorker(void)
{
pid_t pid;
#ifdef EXEC_BACKEND
switch ((pid = slotsyncworker_forkexec()))
{
#else
switch ((pid = fork_process()))
{
case 0:
/* in postmaster child ... */
InitPostmasterChild();
/* Close the postmaster's sockets */
ClosePostmasterPorts(false);
ReplSlotSyncWorkerMain(0, NULL);
break;
#endif
case -1:
ereport(LOG,
(errmsg("could not fork slot sync worker process: %m")));
return 0;
default:
return (int) pid;
}
/* shouldn't get here */
return 0;
}
#ifdef EXEC_BACKEND
/*
* The forkexec routine for the slot sync worker process.
*
* Format up the arglist, then fork and exec.
*/
static pid_t
slotsyncworker_forkexec(void)
{
char *av[10];
int ac = 0;
av[ac++] = "postgres";
av[ac++] = "--forkssworker";
av[ac++] = NULL; /* filled in by postmaster_forkexec */
av[ac] = NULL;
Assert(ac < lengthof(av));
return postmaster_forkexec(ac, av);
}
#endif
/*
* Shut down the slot sync worker.
*/

View File

@ -63,6 +63,7 @@
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/auxprocess.h"
#include "postmaster/interrupt.h"
#include "replication/walreceiver.h"
#include "replication/walsender.h"
@ -179,7 +180,7 @@ ProcessWalRcvInterrupts(void)
/* Main entry point for walreceiver process */
void
WalReceiverMain(void)
WalReceiverMain(char *startup_data, size_t startup_data_len)
{
char conninfo[MAXCONNINFO];
char *tmp_conninfo;
@ -195,6 +196,11 @@ WalReceiverMain(void)
char *sender_host = NULL;
int sender_port = 0;
Assert(startup_data_len == 0);
MyBackendType = B_WAL_RECEIVER;
AuxiliaryProcessMainCommon();
/*
* WalRcv should be set up already (if we are a backend, we inherit this
* by fork() or EXEC_BACKEND mechanism from the postmaster).