1、問題
pg_ctl start啓動時報錯退出:pg_ctl:server did not start in time。超時時間是多少?從何時到哪一個階段算超時?sql
2、分析:該信息打印位置,從後面代碼段do_start函數中能夠看出
一、pg_ctl start調用start_postmaster啓動PG的主進程後,每隔0.1ms檢查一次postmaster.pid文件,是否已寫入ready/standby
二、總共會檢查600次,即從啓動主進程後,最多等待60s,若是沒有寫入ready/standby則打印上述日誌並退出
三、默認等待時間是60s,若是pg_ctl start -t指定等待時間,則等待時間爲該指定時間ide
3、何時postmaster.pid文件寫入ready/standby
一、若是是主機無論有沒有設置hot standby
1)當startup進程恢復完成退出時,調用proc_exit函數向主進程發送SIGCHLD信號並退出
2)主進程接收到信號後,signal處理函數reaper調用AddToDataDirLockFile向postmaster.pid文件寫入ready
二、若是是備機即data目錄下有recovery.cnf文件,且設置了hot standby,在實際恢復前沒有到達一致性位置
1)startup進程向主進程發送PMSIGNAL_RECOVERY_STARTED信號,主進程調用信號處理函數sigusr1_handler,將pmState=PM_RECOVERY
2)每次讀取下一個xlog前都會調用CheckRecoveryConsistency函數進行一致性檢查:
2.1 進入一致性狀態,starup進程向主進程發送PMSIGNAL_BEGIN_HOT_STANDBY信號,主進程接收到信號後調用sigusr1_handler->AddToDataDirLockFile向postmaster.pid文件寫入ready
三、若是是備機即data目錄下有recovery.cnf文件,且設置了hot standby,在實際恢復前沒有到達一致性位置
1)startup進程向主進程發送PMSIGNAL_RECOVERY_STARTED信號,主進程調用信號處理函數sigusr1_handler,將pmState=PM_RECOVERY
2)每次讀取下一個xlog前都會調用CheckRecoveryConsistency函數進行一致性檢查。若是沒有進入一致性狀態
3)本地日誌恢復完成,切換日誌源時一樣調用CheckRecoveryConsistency函數進行一致性檢查
3.1 進入一致性狀態,starup進程向主進程發送PMSIGNAL_BEGIN_HOT_STANDBY信號,主進程接收到信號後調用sigusr1_handler->AddToDataDirLockFile向postmaster.pid文件寫入ready
四、若是是備機即data目錄下有recovery.cnf文件,且設置了hot standby,在實際恢復前到達一致性位置
1)startup進程向主進程發送PMSIGNAL_RECOVERY_STARTED信號,主進程調用信號處理函數sigusr1_handler,將pmState=PM_RECOVERY
2)CheckRecoveryConsistency函數進行一致性檢查,向主進程發送PMSIGNAL_BEGIN_HOT_STANDBY信號,主進程接收到信號後調用sigusr1_handler->AddToDataDirLockFile向postmaster.pid文件寫入ready
五、若是是備機即data目錄下有recovery.cnf文件,沒有設置hot standby
1)startup進程向主進程發送PMSIGNAL_RECOVERY_STARTED信號
2)主進程接收到信號後,向postmaster.將pmState=PM_RECOVERY函數
4、代碼分析
一、pg_ctl start流程post
do_start-> pm_pid = start_postmaster(); if (do_wait){ print_msg(_("waiting for server to start...")); switch (wait_for_postmaster(pm_pid, false)){ case POSTMASTER_READY: print_msg(_(" done\n")); print_msg(_("server started\n")); break; case POSTMASTER_STILL_STARTING: print_msg(_(" stopped waiting\n")); write_stderr(_("%s: server did not start in time\n"), progname); exit(1); break; case POSTMASTER_FAILED: print_msg(_(" stopped waiting\n")); write_stderr(_("%s: could not start server\n" "Examine the log output.\n"), progname); exit(1); break; } }else print_msg(_("server starting\n"));
wait_for_postmaster-> for (i = 0; i < wait_seconds * WAITS_PER_SEC; i++){ if ((optlines = readfile(pid_file, &numlines)) != NULL && numlines >= LOCK_FILE_LINE_PM_STATUS){ pmpid = atol(optlines[LOCK_FILE_LINE_PID - 1]); pmstart = atol(optlines[LOCK_FILE_LINE_START_TIME - 1]); if (pmstart >= start_time - 2 && pmpid == pm_pid){ char *pmstatus = optlines[LOCK_FILE_LINE_PM_STATUS - 1]; if (strcmp(pmstatus, PM_STATUS_READY) == 0 || strcmp(pmstatus, PM_STATUS_STANDBY) == 0){ /* postmaster is done starting up */ free_readfile(optlines); return POSTMASTER_READY; } } } free_readfile(optlines); if (waitpid((pid_t) pm_pid, &exitstatus, WNOHANG) == (pid_t) pm_pid) return POSTMASTER_FAILED; pg_usleep(USEC_PER_SEC / WAITS_PER_SEC); } /* out of patience; report that postmaster is still starting up */ return POSTMASTER_STILL_STARTING;
二、server主進程及信號處理函數ui
PostmasterMain-> pqsignal_no_restart(SIGUSR1, sigusr1_handler); /* message from child process */ pqsignal_no_restart(SIGCHLD, reaper); /* handle child termination */ ... StartupXLOG(); ... proc_exit(0);//exit函數向主進程發送SIGCHLD信號
reaper->//進程終止或者中止的信號 AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_READY);
postmaster進程接收信號: sigusr1_handler-> if (CheckPostmasterSignal(PMSIGNAL_RECOVERY_STARTED) && pmState == PM_STARTUP && Shutdown == NoShutdown){ CheckpointerPID = StartCheckpointer(); BgWriterPID = StartBackgroundWriter(); if (XLogArchivingAlways()) PgArchPID = pgarch_start(); //hot_standby在postgresql.conf文件中配置TRUE //表示在恢復的時候容許鏈接 if (!EnableHotStandby){ //將standby寫入postmaster.pid文件,表示up但不容許鏈接 AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STANDBY); } pmState = PM_RECOVERY; } if (CheckPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY) && pmState == PM_RECOVERY && Shutdown == NoShutdown){ PgStatPID = pgstat_start(); //將ready寫入postmaster.pid文件,容許鏈接 AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_READY); pmState = PM_HOT_STANDBY; } ...
三、Startup進程rest
StartupXLOG-> ReadCheckpointRecord if (ArchiveRecoveryRequested && IsUnderPostmaster){//有recovery.conf文件則ArchiveRecoveryRequested爲TRUE //有recovery.conf文件則ArchiveRecoveryRequested爲TRUE PublishStartupProcessInformation(); SetForwardFsyncRequests(); //向master進程發送PMSIGNAL_RECOVERY_STARTED信號 SendPostmasterSignal(PMSIGNAL_RECOVERY_STARTED); bgwriterLaunched = true; } CheckRecoveryConsistency();-->... |-- if (standbyState == STANDBY_SNAPSHOT_READY && !LocalHotStandbyActive && | reachedConsistency && IsUnderPostmaster){ | SpinLockAcquire(&XLogCtl->info_lck); | XLogCtl->SharedHotStandbyActive = true; | SpinLockRelease(&XLogCtl->info_lck); | LocalHotStandbyActive = true; | SendPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY); |-- } ... 回放一個record後,每次讀取下一個record前都會調用CheckRecoveryConsistency