PostgreSQL Source Code  git master
launch_backend.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "access/xlog.h"
#include "common/file_utils.h"
#include "libpq/libpq-be.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "nodes/queryjumble.h"
#include "port.h"
#include "postmaster/autovacuum.h"
#include "postmaster/auxprocess.h"
#include "postmaster/bgworker_internals.h"
#include "postmaster/bgwriter.h"
#include "postmaster/fork_process.h"
#include "postmaster/pgarch.h"
#include "postmaster/postmaster.h"
#include "postmaster/startup.h"
#include "postmaster/syslogger.h"
#include "postmaster/walsummarizer.h"
#include "postmaster/walwriter.h"
#include "replication/slotsync.h"
#include "replication/walreceiver.h"
#include "storage/dsm.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/pg_shmem.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "tcop/backend_startup.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/datetime.h"
#include "utils/guc.h"
#include "utils/injection_point.h"
#include "utils/memutils.h"
#include "utils/timestamp.h"
Include dependency graph for launch_backend.c:

Go to the source code of this file.

Data Structures

struct  child_process_kind
 

Functions

const char * PostmasterChildName (BackendType child_type)
 
pid_t postmaster_child_launch (BackendType child_type, char *startup_data, size_t startup_data_len, ClientSocket *client_sock)
 

Variables

static child_process_kind child_process_kinds []
 

Function Documentation

◆ postmaster_child_launch()

pid_t postmaster_child_launch ( BackendType  child_type,
char *  startup_data,
size_t  startup_data_len,
ClientSocket client_sock 
)

Definition at line 228 of file launch_backend.c.

231 {
232  pid_t pid;
233 
235 
236 #ifdef EXEC_BACKEND
237  pid = internal_forkexec(child_process_kinds[child_type].name,
238  startup_data, startup_data_len, client_sock);
239  /* the child process will arrive in SubPostmasterMain */
240 #else /* !EXEC_BACKEND */
241  pid = fork_process();
242  if (pid == 0) /* child */
243  {
244  /* Close the postmaster's sockets */
245  ClosePostmasterPorts(child_type == B_LOGGER);
246 
247  /* Detangle from postmaster */
249 
250  /* Detach shared memory if not needed. */
251  if (!child_process_kinds[child_type].shmem_attach)
252  {
253  dsm_detach_all();
255  }
256 
257  /*
258  * Enter the Main function with TopMemoryContext. The startup data is
259  * allocated in PostmasterContext, so we cannot release it here yet.
260  * The Main function will do it after it's done handling the startup
261  * data.
262  */
264 
265  if (client_sock)
266  {
268  memcpy(MyClientSocket, client_sock, sizeof(ClientSocket));
269  }
270 
271  /*
272  * Run the appropriate Main function
273  */
274  child_process_kinds[child_type].main_fn(startup_data, startup_data_len);
275  pg_unreachable(); /* main_fn never returns */
276  }
277 #endif /* EXEC_BACKEND */
278  return pid;
279 }
#define Assert(condition)
Definition: c.h:858
#define pg_unreachable()
Definition: c.h:296
void dsm_detach_all(void)
Definition: dsm.c:775
pid_t fork_process(void)
Definition: fork_process.c:32
struct ClientSocket * MyClientSocket
Definition: globals.c:49
bool IsUnderPostmaster
Definition: globals.c:119
bool IsPostmasterEnvironment
Definition: globals.c:118
static child_process_kind child_process_kinds[]
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void * palloc(Size size)
Definition: mcxt.c:1317
@ B_LOGGER
Definition: miscadmin.h:364
void InitPostmasterChild(void)
Definition: miscinit.c:95
void ClosePostmasterPorts(bool am_syslogger)
Definition: postmaster.c:1817
MemoryContextSwitchTo(old_ctx)
void(* main_fn)(char *startup_data, size_t startup_data_len) pg_attribute_noreturn()
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:970
const char * name

References Assert, B_LOGGER, child_process_kinds, ClosePostmasterPorts(), dsm_detach_all(), fork_process(), InitPostmasterChild(), IsPostmasterEnvironment, IsUnderPostmaster, child_process_kind::main_fn, MemoryContextSwitchTo(), MyClientSocket, name, palloc(), pg_unreachable, PGSharedMemoryDetach(), and TopMemoryContext.

Referenced by BackendStartup(), do_start_bgworker(), StartChildProcess(), and SysLogger_Start().

◆ PostmasterChildName()

const char* PostmasterChildName ( BackendType  child_type)

Definition at line 211 of file launch_backend.c.

212 {
213  return child_process_kinds[child_type].name;
214 }

References child_process_kinds, and child_process_kind::name.

Referenced by StartChildProcess().

Variable Documentation

◆ child_process_kinds

child_process_kind child_process_kinds[]
static
Initial value:
= {
[B_INVALID] = {"invalid", NULL, false},
[B_BACKEND] = {"backend", BackendMain, true},
[B_AUTOVAC_LAUNCHER] = {"autovacuum launcher", AutoVacLauncherMain, true},
[B_AUTOVAC_WORKER] = {"autovacuum worker", AutoVacWorkerMain, true},
[B_BG_WORKER] = {"bgworker", BackgroundWorkerMain, true},
[B_WAL_SENDER] = {"wal sender", NULL, true},
[B_SLOTSYNC_WORKER] = {"slot sync worker", ReplSlotSyncWorkerMain, true},
[B_STANDALONE_BACKEND] = {"standalone backend", NULL, false},
[B_ARCHIVER] = {"archiver", PgArchiverMain, true},
[B_BG_WRITER] = {"bgwriter", BackgroundWriterMain, true},
[B_CHECKPOINTER] = {"checkpointer", CheckpointerMain, true},
[B_STARTUP] = {"startup", StartupProcessMain, true},
[B_WAL_RECEIVER] = {"wal_receiver", WalReceiverMain, true},
[B_WAL_SUMMARIZER] = {"wal_summarizer", WalSummarizerMain, true},
[B_WAL_WRITER] = {"wal_writer", WalWriterMain, true},
[B_LOGGER] = {"syslogger", SysLoggerMain, false},
}
void AutoVacWorkerMain(char *startup_data, size_t startup_data_len)
Definition: autovacuum.c:1360
void AutoVacLauncherMain(char *startup_data, size_t startup_data_len) pg_attribute_noreturn()
void StartupProcessMain(char *startup_data, size_t startup_data_len)
Definition: startup.c:216
void BackendMain(char *startup_data, size_t startup_data_len)
void BackgroundWorkerMain(char *startup_data, size_t startup_data_len)
Definition: bgworker.c:717
void BackgroundWriterMain(char *startup_data, size_t startup_data_len)
Definition: bgwriter.c:87
void CheckpointerMain(char *startup_data, size_t startup_data_len)
Definition: checkpointer.c:173
@ B_WAL_SUMMARIZER
Definition: miscadmin.h:357
@ B_WAL_WRITER
Definition: miscadmin.h:358
@ B_WAL_RECEIVER
Definition: miscadmin.h:356
@ B_CHECKPOINTER
Definition: miscadmin.h:354
@ B_WAL_SENDER
Definition: miscadmin.h:339
@ B_STARTUP
Definition: miscadmin.h:355
@ B_BG_WORKER
Definition: miscadmin.h:338
@ B_INVALID
Definition: miscadmin.h:332
@ B_STANDALONE_BACKEND
Definition: miscadmin.h:342
@ B_BG_WRITER
Definition: miscadmin.h:353
@ B_BACKEND
Definition: miscadmin.h:335
@ B_ARCHIVER
Definition: miscadmin.h:352
@ B_AUTOVAC_LAUNCHER
Definition: miscadmin.h:336
@ B_SLOTSYNC_WORKER
Definition: miscadmin.h:340
@ B_AUTOVAC_WORKER
Definition: miscadmin.h:337
void PgArchiverMain(char *startup_data, size_t startup_data_len)
Definition: pgarch.c:215
void ReplSlotSyncWorkerMain(char *startup_data, size_t startup_data_len)
Definition: slotsync.c:1331
void SysLoggerMain(char *startup_data, size_t startup_data_len)
Definition: syslogger.c:165
void WalReceiverMain(char *startup_data, size_t startup_data_len)
Definition: walreceiver.c:183
void WalSummarizerMain(char *startup_data, size_t startup_data_len)
void WalWriterMain(char *startup_data, size_t startup_data_len)
Definition: walwriter.c:89

Definition at line 181 of file launch_backend.c.

Referenced by postmaster_child_launch(), and PostmasterChildName().