初步学习pg_control文件之三

接前文,初步学习pg_control文件之二

继续学习:

研究 DBState,先研究 DB_IN_PRODUCTION ,看它如何出现:

它出现在启动Postmaster时运行的函数处:

/*                                            
 * This must be called ONCE during postmaster or standalone-backend startup
 */                                            
void                                            
StartupXLOG(void)                                            
{                                            
    …                                                                
    /*                                        
     * Read control file and check XLOG status looks valid.  
     * Note: in most control paths, *ControlFile is already valid and we need   
     * not do ReadControlFile() here, but might as well do it to be sure. 
     */                                        
    ReadControlFile();                                        
                                            
    if (ControlFile->state < DB_SHUTDOWNED ||                                        
        ControlFile->state > DB_IN_PRODUCTION ||                                    
        !XRecOffIsValid(ControlFile->checkPoint.xrecoff))                                    
        ereport(FATAL,(errmsg("control file contains invalid data")));                            
                                            
    if (ControlFile->state == DB_SHUTDOWNED)                                        
        ereport(LOG, errmsg("database system was shut down at %s", str_time(ControlFile->time))));                    
    else if (ControlFile->state == DB_SHUTDOWNED_IN_RECOVERY)                                        
        ereport(LOG,(errmsg("database system was shut down in recovery at %s", 
str_time(ControlFile
->time))));
else if (ControlFile->state == DB_SHUTDOWNING) ereport(LOG,(errmsg("database system shutdown was interrupted; last known up at %s",
str_time(ControlFile
->time)))); else if (ControlFile->state == DB_IN_CRASH_RECOVERY) ereport(LOG, (errmsg("database system was interrupted while in recovery at %s", str_time(ControlFile->time)), errhint("This probably means that some data is corrupted and" you will have to use the last backup for recovery.))); else if (ControlFile->state == DB_IN_ARCHIVE_RECOVERY) ereport(LOG, (errmsg("database system was interrupted while in recovery at log time %s", str_time(ControlFile->checkPointCopy.time)), errhint("If this has occurred more than once some data might be corrupted and you might need to choose an earlier recovery target."))); else if (ControlFile->state == DB_IN_PRODUCTION) ereport(LOG,(errmsg("database system was interrupted; last known up at %s", str_time(ControlFile->time)))); /* This is just to allow attaching to startup process with a debugger */ #ifdef XLOG_REPLAY_DELAY if (ControlFile->state != DB_SHUTDOWNED) pg_usleep(60000000L); #endif /* * Check whether we need to force recovery from WAL. If it appears to * have been a clean shutdown and we did not have a recovery.conf file, * then assume no recovery needed. */ if (XLByteLT(checkPoint.redo, RecPtr)) { … } else if (ControlFile->state != DB_SHUTDOWNED) InRecovery = true; else if (InArchiveRecovery) { /* force recovery due to presence of recovery.conf */ InRecovery = true; } /* REDO */ if (InRecovery) { … /* * Update pg_control to show that we are recovering and to show the * selected checkpoint as the place we are starting from. We also mark * pg_control with any minimum recovery stop point obtained from a * backup history file. */ if (InArchiveRecovery) ControlFile->state = DB_IN_ARCHIVE_RECOVERY; else { ereport(LOG, (errmsg("database system was not properly shut down; " automatic recovery in progress))); ControlFile->state = DB_IN_CRASH_RECOVERY; } … } … /* * Okay, we're officially UP. */ InRecovery = false; LWLockAcquire(ControlFileLock, LW_EXCLUSIVE); ControlFile->state = DB_IN_PRODUCTION; ControlFile->time = (pg_time_t) time(NULL); UpdateControlFile(); LWLockRelease(ControlFileLock); … }

可以说,只要是正常启动了,那么就是DB_IN_PRODUCTION状态。

 

你可能感兴趣的:(数据库)