Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PostgreSQL pg_ctl start timeout analysis

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

First, problems

Pg_ctl start startup Times wrong exit: pg_ctl:server did not start in time. What is the overtime time? From when to which stage is overtime?

Second, analysis: the printing position of this information can be seen from the following code snippet do_start function.

1. After pg_ctl start calls start_postmaster to start the main process of PG, check the postmaster.pid file every other 0.1ms to see if it has been written to ready/standby.

2. A total of 600 times will be checked, that is, after starting the main process, wait for 60 seconds at most. If the ready/standby is not written, print the above log and exit.

3. The default waiting time is 60s. If pg_ctl start-t specifies the waiting time, the waiting time is the specified time.

When is the postmaster.pid file written to ready/standby

1. If it is a host, regardless of whether hot standby is set up or not

1) when the startup process resumes and exits, call the proc_exit function to send a SIGCHLD signal to the main process and exit

2) after the main process receives the signal, the signal processing function reaper calls AddToDataDirLockFile to write ready to the postmaster.pid file

2. If there is a recovery.cnf file in the data directory and hot standby is set, it does not reach the consistent location before the actual recovery.

1) the startup process sends the PMSIGNAL_RECOVERY_STARTED signal to the main process, and the main process calls the signal processing function sigusr1_handler to pmState=PM_RECOVERY

2) each time the next xlog is read, the CheckRecoveryConsistency function is called to check the consistency:

2.1According to the consistency state, the starup process sends a PMSIGNAL_BEGIN_HOT_STANDBY signal to the main process. After receiving the signal, the main process calls sigusr1_handler- > AddToDataDirLockFile to write ready to the postmaster.pid file.

3. If there is a recovery.cnf file in the data directory and hot standby is set, it does not reach the consistent location before the actual recovery.

1) the startup process sends the PMSIGNAL_RECOVERY_STARTED signal to the main process, and the main process calls the signal processing function sigusr1_handler to pmState=PM_RECOVERY

2) each time the next xlog is read, the CheckRecoveryConsistency function is called to check the consistency. If you do not enter the consistent state

3) the recovery of the local log is complete, and the CheckRecoveryConsistency function is also called to check the consistency when switching log sources

3.1According to the consistency state, the starup process sends a PMSIGNAL_BEGIN_HOT_STANDBY signal to the main process. After receiving the signal, the main process calls sigusr1_handler- > AddToDataDirLockFile to write ready to the postmaster.pid file.

4. If there is a recovery.cnf file in the data directory and hot standby is set, it will reach the consistent location before the actual recovery.

1) the startup process sends the PMSIGNAL_RECOVERY_STARTED signal to the main process, and the main process calls the signal processing function sigusr1_handler to pmState=PM_RECOVERY

2) the CheckRecoveryConsistency function checks the consistency and sends the PMSIGNAL_BEGIN_HOT_STANDBY signal to the main process. After receiving the signal, the main process calls sigusr1_handler- > AddToDataDirLockFile to write ready to the postmaster.pid file.

5. If there is a recovery.cnf file in the data directory for the slave, hot standby is not set.

1) the startup process sends PMSIGNAL_RECOVERY_STARTED signals to the main process

2) after receiving the signal, the main process sends it to postmaster. PmState=PM_RECOVERY

IV. Code analysis

1. Pg_ctl start process

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

2. Server main process and signal processing function

PostmasterMain- > pqsignal_no_restart (SIGUSR1, sigusr1_handler); / * message from child process * / pqsignal_no_restart (SIGCHLD, reaper); / * handle child termination * /. StartupXLOG ();... Proc_exit (0); / / exit function sends SIGCHLD signal reaper- > / / process termination or stop signal AddToDataDirLockFile (LOCK_FILE_LINE_PM_STATUS, PM_STATUS_READY); postmaster process receives signal: sigusr1_handler- > if (CheckPostmasterSignal (PMSIGNAL_RECOVERY_STARTED) & & pmState = = PM_STARTUP & & Shutdown = = NoShutdown) {CheckpointerPID = StartCheckpointer (); BgWriterPID = StartBackgroundWriter () If (XLogArchivingAlways ()) PgArchPID = pgarch_start (); / / hot_standby configures TRUE / / in the postgresql.conf file to allow connection if (! EnableHotStandby) {/ / write standby to the postmaster.pid file during recovery, indicating up but not allowing connection to 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 (); / / write ready to the postmaster.pid file, allowing connections to AddToDataDirLockFile (LOCK_FILE_LINE_PM_STATUS, PM_STATUS_READY); pmState = PM_HOT_STANDBY;}.

3. Startup process

StartupXLOG- > ReadCheckpointRecord if (ArchiveRecoveryRequested & & IsUnderPostmaster) {/ / if you have recovery.conf files, then ArchiveRecoveryRequested is TRUE / / if you have recovery.conf files, ArchiveRecoveryRequested is TRUE PublishStartupProcessInformation (); SetForwardFsyncRequests (); / / send PMSIGNAL_RECOVERY_STARTED signal SendPostmasterSignal (PMSIGNAL_RECOVERY_STARTED) to the master process; 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); |--}. After playing back a record, CheckRecoveryConsistency is called every time the next record is read.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report