第 9 课 PostgreSQL 4种进程启动方式
时间:2019-05-23 13:28 来源:linux.it.net.cn 作者:IT
Postgres在启动后,可分别以一下四种形式启动进程:
-
-
SubPostmasterMain
-
-
AuxiliaryProcessMain
-
-
PostgresMain
-
-
PostmasterMain
SubPostmasterMain(--fork)
-
-
指明由postmaster派生
-
-
设置进程ID
-
-
初始化内存池
-
-
处理输入参数
-
-
运行相应的backend或子进程
–forkbackend或–forkboot
1) 关联到共享内存
2) 初始化共享内存访问(UsedShmemSegAddr)
3) 初始化AuxiliaryProcess
4) 创建共享内存和信号量
5) 启动AuxiliaryProcessMain
–forkavlauncher
1) 关联到共享内存
2) AutovacuumLauncherIAm()
3) 初始化共享内存访问(UsedShmemSegAddr)
4) 初始化AuxiliaryProcess
5) 创建共享内存和信号量
6) 启动AutoVacLauncherMain
–forkavworker
1) 关联到共享内存
2) AutovacuumLauncherIAm()
3) 初始化共享内存访问(UsedShmemSegAddr)
4) 初始化AuxiliaryProcess
5) 创建共享内存和信号量
6) 启动AutoVacWorkerMain
–forkarch
1) 启动PgArchiverMain
–forkcol
1) 启动PgstatCollectorMain
–forklog
1) 启动SysLoggerMain
AuxiliaryProcessMain(--boot)
-
设置进程ID
-
初始化内存池
-
设置路径、时间等变量
-
初始化GUC选项,处理输入参数
-
以BootstrapProcessing模式初始化一个backend:ipc, lock, file, storage, buffer
-
设置信号处理句柄
-
以NormalProcessing状态针对不同auxType分别进行以下处理
a) CheckerProcess
1) 启动CheckerModeMain
b) BooststrapProcess
1) BootstrapXLOG
2) 启动XLOG
3) 启动BootstrapModeMain
c) StartupProcess
1) 启动XLOG
2) 加载FreeSpaceMap
3) BuildFlatFiles(false)
d) BgWriterProcess
1) 初始化XLOG访问
2) 启动BackgroundWriterMain
e) WalWriterProcess
1) 初始化XLOG访问
2) 启动WalWriterMain
PostgresMain(--single)
-
设置进程ID
-
初始化内存池
-
设置路径、时间等变量
-
初始化GUC选项,处理输入参数和其他startup packet中的参数
-
设置信号处理句柄
-
初始化一个backend(无论它是否由postmaster生成):ipc, lock, file, storage, buffer
-
启动XLOG
-
加载FreeSpaceMap
-
初始化进程
-
初始化表缓存和系统目录访问
-
处理预加载的库
-
转到MessageContext内存池
-
进入查询处理主循环
PostmasterMain
-
设置进程ID
-
初始化内存池
-
设置路径、时间等变量
-
初始化GUC选项,处理输入参数并载入hba和ident
-
设置共享内存和信号量,初始化共享数据结构
-
设置信号处理句柄
-
启动守护进程:
(1) syslogger:收集其他其他进程的日志输出,写入到文件
(2) stats daemon:通过UDP获取各backend的运行时统计信息
(3) autovacuum launcher:定期进行表空间的自动清理
-
由参数forkboot启动一个backend
-
绑定到TCP socket,监听连接请求
PostgreSQL版本使用的多线程
启动之后的线程:
10 Thread 0x7fffe478e700 (LWP 42580) "kingbase" (Exiting) 0x00007ffff56c5bf7 in madvise () from /lib64/libc.so.6
9 Thread 0x7fffe498f700 (LWP 42569) "kingbase" 0x00007ffff56c0bcd in poll () from /lib64/libc.so.6
8 Thread 0x7fffe4fa7700 (LWP 42438) "kingbase" 0x00007ffff56c2943 in select () from /lib64/libc.so.6
7 Thread 0x7fffe4b90700 (LWP 42437) "kingbase" 0x00007ffff56c2943 in select () from /lib64/libc.so.6
5 Thread 0x7fffebef6700 (LWP 42327) "kingbase" 0x00007ffff569242d in nanosleep () from /lib64/libc.so.6
4 Thread 0x7fffec0f7700 (LWP 42326) "kingbase" 0x00007ffff56c2943 in select () from /lib64/libc.so.6
3 Thread 0x7fffec514700 (LWP 42323) "kingbase" 0x00007ffff56c2941 in select () from /lib64/libc.so.6
2 Thread 0x7fffec715700 (LWP 42322) "kingbase" 0x00007ffff64c4e91 in sigwait () from /lib64/libpthread.so.0
* 1 Thread 0x7ffff7fd0140 (LWP 42318) "kingbase" 0x00007ffff56c2943 in select () from /lib64/libc.so.6
子线程创建调用
#0 ThreadCreate (start=0x8915a1 <MainStarter>, args=0x18ddd70) at kos_signal.c:877
#1 0x0000000000891835 in internal_forkexec (argc=6, argv=0x7fffffffdd80, port=0x7fffffffdae0) at postmaster.c:5116
#2 0x00000000008914ff in postmaster_forkexec (argc=6, argv=0x7fffffffdd80) at postmaster.c:5029
#3 0x0000000000892571 in StartChildProcess (xlop=2) at postmaster.c:5854
#4 0x000000000088d002 in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2376
#5 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288
#0 ThreadCreate (start=0x8915a1 <MainStarter>, args=0x18ddd70) at kos_signal.c:877
#1 0x0000000000891835 in internal_forkexec (argc=6, argv=0x7fffffffd540, port=0x7fffffffd2a0) at postmaster.c:5116
#2 0x00000000008914ff in postmaster_forkexec (argc=6, argv=0x7fffffffd540) at postmaster.c:5029
#3 0x0000000000892571 in StartChildProcess (xlop=3) at postmaster.c:5854
#4 0x000000000088f880 in reaper (postgres_signal_arg=17) at postmaster.c:3936
#5 0x000000000087d31f in pg_dispatch_queued_signals () at kos_signal.c:499
#6 0x000000000087c5d1 in sigusr2_handler (postgres_signal_arg=12) at kos_signal.c:148
#7 <signal handler called>
#8 0x00007ffff56c2943 in select () from /lib64/libc.so.6
#9 0x000000000088d5d0 in ServerLoop () at postmaster.c:2696
#10 0x000000000088d086 in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2413
#11 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288
#0 ThreadCreate (start=0x8915a1 <MainStarter>, args=0x18975a0) at kos_signal.c:877
#1 0x0000000000891835 in internal_forkexec (argc=6, argv=0x7fffffffd540, port=0x7fffffffd2a0) at postmaster.c:5116
#2 0x00000000008914ff in postmaster_forkexec (argc=6, argv=0x7fffffffd540) at postmaster.c:5029
#3 0x0000000000892571 in StartChildProcess (xlop=4) at postmaster.c:5854
#4 0x000000000088f8d3 in reaper (postgres_signal_arg=17) at postmaster.c:3949
#5 0x000000000087d31f in pg_dispatch_queued_signals () at kos_signal.c:499
#6 0x000000000087c5d1 in sigusr2_handler (postgres_signal_arg=12) at kos_signal.c:148
#7 <signal handler called>
#8 0x00007ffff56c2943 in select () from /lib64/libc.so.6
#9 0x000000000088d5d0 in ServerLoop () at postmaster.c:2696
#10 0x000000000088d086 in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2413
#11 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288
#0 ThreadCreate (start=0x8915a1 <MainStarter>, args=0x18dde70) at kos_signal.c:877
#1 0x0000000000891835 in internal_forkexec (argc=3, argv=0x7fffffffd520, port=0x7fffffffd2b0) at postmaster.c:5116
#2 0x00000000008914ff in postmaster_forkexec (argc=3, argv=0x7fffffffd520) at postmaster.c:5029
#3 0x0000000000886c42 in pgstat_forkexec () at pgstat.c:508
#4 0x0000000000886d01 in pgstat_start () at pgstat.c:568
#5 0x000000000088f915 in reaper (postgres_signal_arg=17) at postmaster.c:3953
#6 0x000000000087d31f in pg_dispatch_queued_signals () at kos_signal.c:499
#7 0x000000000087c5d1 in sigusr2_handler (postgres_signal_arg=12) at kos_signal.c:148
#8 <signal handler called>
#9 0x00007ffff56c2943 in select () from /lib64/libc.so.6
#10 0x000000000088d5d0 in ServerLoop () at postmaster.c:2696
#11 0x000000000088d086 in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2413
#12 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288
#0 ThreadCreate (start=0x8915a1 <MainStarter>, args=0x186cfd0) at kos_signal.c:877
#1 0x0000000000891835 in internal_forkexec (argc=3, argv=0x7fffffffdbe0, port=0x7fffffffd970) at postmaster.c:5116
#2 0x00000000008914ff in postmaster_forkexec (argc=3, argv=0x7fffffffdbe0) at postmaster.c:5029
#3 0x0000000000882d68 in autovac_forkexec () at autovacuum.c:522
#4 0x0000000000882c1f in autovac_start () at autovacuum.c:459
#5 0x000000000088d8a0 in ServerLoop () at postmaster.c:2845
#6 0x000000000088d086 in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2413
#7 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288
共享内存初始化过程
- 主线程首次初始化
(gdb) bt
#0 InitBufTable (size=11664) at buf_table.c:60
#1 0x00000000008fa224 in StrategyInitialize (init=1 '\001') at freelist.c:342
#2 0x00000000008f1672 in InitBufferPool () at buf_init.c:148
#3 0x000000000091f124 in CreateSharedMemoryAndSemaphores (makePrivate=0 '\000', port=44444) at ipci.c:219
#4 0x000000000088ee5c in reset_shared (port=44444) at postmaster.c:3565
#5 0x000000000088cbcf in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2134
#6 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288
(gdb)
$4 = (HTAB *) 0x18b0f20
- 子线程使用共享内存初始化
#0 InitBufTable (size=11664) at buf_table.c:60
#1 0x00000000008fa224 in StrategyInitialize (init=0 '\000') at freelist.c:342
#2 0x00000000008f1672 in InitBufferPool () at buf_init.c:148
#3 0x000000000091f124 in CreateSharedMemoryAndSemaphores (makePrivate=0 '\000', port=0) at ipci.c:219
#4 0x0000000000891bef in SubPostmasterMain (argc=6, argv=0x18ddd70) at postmaster.c:5388
#5 0x0000000000891644 in MainStarter (params=0x18ddd70) at postmaster.c:5070
#6 0x00007ffff64bddc5 in start_thread () from /lib64/libpthread.so.0
#7 0x00007ffff56cb21d in clone () from /lib64/libc.so.6
$8 = (HTAB *) 0x7fffe0022390
- 子线程使用共享内存初始化
#0 InitBufTable (size=11664) at buf_table.c:60
#1 0x00000000008fa224 in StrategyInitialize (init=0 '\000') at freelist.c:342
#2 0x00000000008f1672 in InitBufferPool () at buf_init.c:148
#3 0x000000000091f124 in CreateSharedMemoryAndSemaphores (makePrivate=0 '\000', port=0) at ipci.c:219
#4 0x0000000000891bef in SubPostmasterMain (argc=6, argv=0x18ddd70) at postmaster.c:5388
#5 0x0000000000891644 in MainStarter (params=0x18ddd70) at postmaster.c:5070
#6 0x00007ffff64bddc5 in start_thread () from /lib64/libpthread.so.0
#7 0x00007ffff56cb21d in clone () from /lib64/libc.so.6
$10 = (HTAB *) 0x7fffe00243a0
- 子线程使用共享内存初始化
#0 InitBufTable (size=11664) at buf_table.c:60
#1 0x00000000008fa224 in StrategyInitialize (init=0 '\000') at freelist.c:342
#2 0x00000000008f1672 in InitBufferPool () at buf_init.c:148
#3 0x000000000091f124 in CreateSharedMemoryAndSemaphores (makePrivate=0 '\000', port=0) at ipci.c:219
#4 0x0000000000891bef in SubPostmasterMain (argc=6, argv=0x18975a0) at postmaster.c:5388
#5 0x0000000000891644 in MainStarter (params=0x18975a0) at postmaster.c:5070
#6 0x00007ffff64bddc5 in start_thread () from /lib64/libpthread.so.0
#7 0x00007ffff56cb21d in clone () from /lib64/libc.so.6
$12 = (HTAB *) 0x7fffd8022390
- 子线程使用共享内存初始化.....
(责任编辑:IT)
Postgres在启动后,可分别以一下四种形式启动进程:
SubPostmasterMain(--fork)
AuxiliaryProcessMain(--boot)
PostgresMain(--single)
PostmasterMain
PostgreSQL版本使用的多线程启动之后的线程: 10 Thread 0x7fffe478e700 (LWP 42580) "kingbase" (Exiting) 0x00007ffff56c5bf7 in madvise () from /lib64/libc.so.6 9 Thread 0x7fffe498f700 (LWP 42569) "kingbase" 0x00007ffff56c0bcd in poll () from /lib64/libc.so.6 8 Thread 0x7fffe4fa7700 (LWP 42438) "kingbase" 0x00007ffff56c2943 in select () from /lib64/libc.so.6 7 Thread 0x7fffe4b90700 (LWP 42437) "kingbase" 0x00007ffff56c2943 in select () from /lib64/libc.so.6 5 Thread 0x7fffebef6700 (LWP 42327) "kingbase" 0x00007ffff569242d in nanosleep () from /lib64/libc.so.6 4 Thread 0x7fffec0f7700 (LWP 42326) "kingbase" 0x00007ffff56c2943 in select () from /lib64/libc.so.6 3 Thread 0x7fffec514700 (LWP 42323) "kingbase" 0x00007ffff56c2941 in select () from /lib64/libc.so.6 2 Thread 0x7fffec715700 (LWP 42322) "kingbase" 0x00007ffff64c4e91 in sigwait () from /lib64/libpthread.so.0 * 1 Thread 0x7ffff7fd0140 (LWP 42318) "kingbase" 0x00007ffff56c2943 in select () from /lib64/libc.so.6 子线程创建调用#0 ThreadCreate (start=0x8915a1 <MainStarter>, args=0x18ddd70) at kos_signal.c:877 #1 0x0000000000891835 in internal_forkexec (argc=6, argv=0x7fffffffdd80, port=0x7fffffffdae0) at postmaster.c:5116 #2 0x00000000008914ff in postmaster_forkexec (argc=6, argv=0x7fffffffdd80) at postmaster.c:5029 #3 0x0000000000892571 in StartChildProcess (xlop=2) at postmaster.c:5854 #4 0x000000000088d002 in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2376 #5 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288 #0 ThreadCreate (start=0x8915a1 <MainStarter>, args=0x18ddd70) at kos_signal.c:877 #1 0x0000000000891835 in internal_forkexec (argc=6, argv=0x7fffffffd540, port=0x7fffffffd2a0) at postmaster.c:5116 #2 0x00000000008914ff in postmaster_forkexec (argc=6, argv=0x7fffffffd540) at postmaster.c:5029 #3 0x0000000000892571 in StartChildProcess (xlop=3) at postmaster.c:5854 #4 0x000000000088f880 in reaper (postgres_signal_arg=17) at postmaster.c:3936 #5 0x000000000087d31f in pg_dispatch_queued_signals () at kos_signal.c:499 #6 0x000000000087c5d1 in sigusr2_handler (postgres_signal_arg=12) at kos_signal.c:148 #7 <signal handler called> #8 0x00007ffff56c2943 in select () from /lib64/libc.so.6 #9 0x000000000088d5d0 in ServerLoop () at postmaster.c:2696 #10 0x000000000088d086 in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2413 #11 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288 #0 ThreadCreate (start=0x8915a1 <MainStarter>, args=0x18975a0) at kos_signal.c:877 #1 0x0000000000891835 in internal_forkexec (argc=6, argv=0x7fffffffd540, port=0x7fffffffd2a0) at postmaster.c:5116 #2 0x00000000008914ff in postmaster_forkexec (argc=6, argv=0x7fffffffd540) at postmaster.c:5029 #3 0x0000000000892571 in StartChildProcess (xlop=4) at postmaster.c:5854 #4 0x000000000088f8d3 in reaper (postgres_signal_arg=17) at postmaster.c:3949 #5 0x000000000087d31f in pg_dispatch_queued_signals () at kos_signal.c:499 #6 0x000000000087c5d1 in sigusr2_handler (postgres_signal_arg=12) at kos_signal.c:148 #7 <signal handler called> #8 0x00007ffff56c2943 in select () from /lib64/libc.so.6 #9 0x000000000088d5d0 in ServerLoop () at postmaster.c:2696 #10 0x000000000088d086 in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2413 #11 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288 #0 ThreadCreate (start=0x8915a1 <MainStarter>, args=0x18dde70) at kos_signal.c:877 #1 0x0000000000891835 in internal_forkexec (argc=3, argv=0x7fffffffd520, port=0x7fffffffd2b0) at postmaster.c:5116 #2 0x00000000008914ff in postmaster_forkexec (argc=3, argv=0x7fffffffd520) at postmaster.c:5029 #3 0x0000000000886c42 in pgstat_forkexec () at pgstat.c:508 #4 0x0000000000886d01 in pgstat_start () at pgstat.c:568 #5 0x000000000088f915 in reaper (postgres_signal_arg=17) at postmaster.c:3953 #6 0x000000000087d31f in pg_dispatch_queued_signals () at kos_signal.c:499 #7 0x000000000087c5d1 in sigusr2_handler (postgres_signal_arg=12) at kos_signal.c:148 #8 <signal handler called> #9 0x00007ffff56c2943 in select () from /lib64/libc.so.6 #10 0x000000000088d5d0 in ServerLoop () at postmaster.c:2696 #11 0x000000000088d086 in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2413 #12 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288 #0 ThreadCreate (start=0x8915a1 <MainStarter>, args=0x186cfd0) at kos_signal.c:877 #1 0x0000000000891835 in internal_forkexec (argc=3, argv=0x7fffffffdbe0, port=0x7fffffffd970) at postmaster.c:5116 #2 0x00000000008914ff in postmaster_forkexec (argc=3, argv=0x7fffffffdbe0) at postmaster.c:5029 #3 0x0000000000882d68 in autovac_forkexec () at autovacuum.c:522 #4 0x0000000000882c1f in autovac_start () at autovacuum.c:459 #5 0x000000000088d8a0 in ServerLoop () at postmaster.c:2845 #6 0x000000000088d086 in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2413 #7 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288 共享内存初始化过程- 主线程首次初始化 (gdb) bt #0 InitBufTable (size=11664) at buf_table.c:60 #1 0x00000000008fa224 in StrategyInitialize (init=1 '\001') at freelist.c:342 #2 0x00000000008f1672 in InitBufferPool () at buf_init.c:148 #3 0x000000000091f124 in CreateSharedMemoryAndSemaphores (makePrivate=0 '\000', port=44444) at ipci.c:219 #4 0x000000000088ee5c in reset_shared (port=44444) at postmaster.c:3565 #5 0x000000000088cbcf in PostmasterMain (argc=5, argv=0x186ae60) at postmaster.c:2134 #6 0x00000000007c574f in main (argc=5, argv=0x186ae60) at main.c:288 (gdb) $4 = (HTAB *) 0x18b0f20 - 子线程使用共享内存初始化 #0 InitBufTable (size=11664) at buf_table.c:60 #1 0x00000000008fa224 in StrategyInitialize (init=0 '\000') at freelist.c:342 #2 0x00000000008f1672 in InitBufferPool () at buf_init.c:148 #3 0x000000000091f124 in CreateSharedMemoryAndSemaphores (makePrivate=0 '\000', port=0) at ipci.c:219 #4 0x0000000000891bef in SubPostmasterMain (argc=6, argv=0x18ddd70) at postmaster.c:5388 #5 0x0000000000891644 in MainStarter (params=0x18ddd70) at postmaster.c:5070 #6 0x00007ffff64bddc5 in start_thread () from /lib64/libpthread.so.0 #7 0x00007ffff56cb21d in clone () from /lib64/libc.so.6 $8 = (HTAB *) 0x7fffe0022390 - 子线程使用共享内存初始化 #0 InitBufTable (size=11664) at buf_table.c:60 #1 0x00000000008fa224 in StrategyInitialize (init=0 '\000') at freelist.c:342 #2 0x00000000008f1672 in InitBufferPool () at buf_init.c:148 #3 0x000000000091f124 in CreateSharedMemoryAndSemaphores (makePrivate=0 '\000', port=0) at ipci.c:219 #4 0x0000000000891bef in SubPostmasterMain (argc=6, argv=0x18ddd70) at postmaster.c:5388 #5 0x0000000000891644 in MainStarter (params=0x18ddd70) at postmaster.c:5070 #6 0x00007ffff64bddc5 in start_thread () from /lib64/libpthread.so.0 #7 0x00007ffff56cb21d in clone () from /lib64/libc.so.6 $10 = (HTAB *) 0x7fffe00243a0 - 子线程使用共享内存初始化 #0 InitBufTable (size=11664) at buf_table.c:60 #1 0x00000000008fa224 in StrategyInitialize (init=0 '\000') at freelist.c:342 #2 0x00000000008f1672 in InitBufferPool () at buf_init.c:148 #3 0x000000000091f124 in CreateSharedMemoryAndSemaphores (makePrivate=0 '\000', port=0) at ipci.c:219 #4 0x0000000000891bef in SubPostmasterMain (argc=6, argv=0x18975a0) at postmaster.c:5388 #5 0x0000000000891644 in MainStarter (params=0x18975a0) at postmaster.c:5070 #6 0x00007ffff64bddc5 in start_thread () from /lib64/libpthread.so.0 #7 0x00007ffff56cb21d in clone () from /lib64/libc.so.6 $12 = (HTAB *) 0x7fffd8022390 - 子线程使用共享内存初始化..... (责任编辑:IT) |