辅助进程类别
在postgresql中,有很多辅助进程,各个辅助进程完成特定的功能,支撑数据库系统运行和管理工作,辅助进程的类别定义如下:
/*
* Auxiliary-process type identifiers. These used to be in bootstrap.h
* but it seems saner to have them here, with the ProcessingMode stuff.
* The MyAuxProcType global is defined and set in bootstrap.c.
*/
typedef enum
{
NotAnAuxProcess = -1,
CheckerProcess = 0,
BootstrapProcess,
StartupProcess, //启动辅助进程,完成启动前的数据回放等工作
BgWriterProcess, //后台写进程,将脏页写入磁盘
CheckpointerProcess, //检查点进程
WalWriterProcess, //WAL日志写进程
WalReceiverProcess,
NUM_AUXPROCTYPES /* Must be last! */
} AuxProcType;
辅助进程启动方式
一般在主进程postmastermain中,启动辅助进程,各个辅助进程功能不同,但是典型的启动方式类似,如下图所示,StartChildProcess
作为多个辅助进程的启动入口,内部通过fork()
创建子进程,并封装子进程参数,在AuxiliaryProcessMain
接口中根据参数不同,执行对应子进程分支。
#define StartupDataBase() StartChildProcess(StartupProcess)
#define StartBackgroundWriter() StartChildProcess(BgWriterProcess)
#define StartCheckpointer() StartChildProcess(CheckpointerProcess)
#define StartWalWriter() StartChildProcess(WalWriterProcess)
#define StartWalReceiver() StartChildProcess(WalReceiverProcess)
捕获.PNG
AuxiliaryProcessMain
AuxiliaryProcessMain作为辅助进程的入口,主要依序完成一下功能:
- 获取基本信息,如进程id,时间戳,可执行文件路径等;
- 遍历解析接口参数,使用接口
getopt
,根据解析出的参数进行配置; - 设置辅助进程在ps命令下的显示名称
init_ps_display
; - 对子进程的信号量,共享内存等资源进行配置
BaseInit()
,InitAuxiliaryProcess()
; - 针对解析出的子进程类型仅从分支处理,关键代码如下图所示,参数类型通过-x参数传递,在此处分支执行后,各个辅助进程正式开始各自的任务,至于各个辅助进程的具体内容,今后再做详细介绍。
switch (MyAuxProcType) //根据参数解析出的辅助进程类型
{
case CheckerProcess:
/* don't set signals, they're useless here */
CheckerModeMain();
proc_exit(1); /* should never return */
case BootstrapProcess:
/*
* There was a brief instant during which mode was Normal; this is
* okay. We need to be in bootstrap mode during BootStrapXLOG for
* the sake of multixact initialization.
*/
SetProcessingMode(BootstrapProcessing);
bootstrap_signals();
BootStrapXLOG();
BootstrapModeMain();
proc_exit(1); /* should never return */
case StartupProcess:
/* don't set signals, startup process has its own agenda */
StartupProcessMain();
proc_exit(1); /* should never return */
case BgWriterProcess:
/* don't set signals, bgwriter has its own agenda */
BackgroundWriterMain();
proc_exit(1); /* should never return */
case CheckpointerProcess:
/* don't set signals, checkpointer has its own agenda */
CheckpointerMain();
proc_exit(1); /* should never return */
case WalWriterProcess:
/* don't set signals, walwriter has its own agenda */
InitXLOGAccess();
WalWriterMain();
proc_exit(1); /* should never return */
case WalReceiverProcess:
/* don't set signals, walreceiver has its own agenda */
WalReceiverMain();
proc_exit(1); /* should never return */
default:
elog(PANIC, "unrecognized process type: %d", (int) MyAuxProcType);
proc_exit(1);
}