PostgreSQL pg_ctl start超时分析
一、问题
pg_ctl start启动时报错退出:pg_ctl:server did not start in time。超时时间是多少?从什么时候到哪个阶段算超时? 二、分析:该信息打印位置,从后面代码段do_start函数中可以看出 三、什么时候postmaster.pid文件写入ready/standby 四、代码分析 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(_(" donen")); print_msg(_("server startedn")); break; case POSTMASTER_STILL_STARTING: print_msg(_(" stopped waitingn")); write_stderr(_("%s: server did not start in timen"),progname); exit(1); break; case POSTMASTER_FAILED: print_msg(_(" stopped waitingn")); write_stderr(_("%s: could not start servern" "Examine the log output.n"),progname); exit(1); break; } }else print_msg(_("server startingn")); 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; 2、server主进程及信号处理函数 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; } ... 3、Startup进程 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |