typedef enum DBState { DB_STARTUP = 0, DB_SHUTDOWNED, DB_SHUTDOWNED_IN_RECOVERY, DB_SHUTDOWNING, DB_IN_CRASH_RECOVERY, DB_IN_ARCHIVE_RECOVERY, DB_IN_PRODUCTION } DBState;
PostgreSQL啟動以及關(guān)閉或運行過程中的狀態(tài)包括以上七種。在pg_controldata獲取的內(nèi)容Database cluster state一欄顯示的是DB的狀態(tài)。其中:
西和網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、成都響應式網(wǎng)站建設公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)。
DB_STARTUP:表示數(shù)據(jù)庫正在啟動狀態(tài),實際上沒有使用該狀態(tài)。
DB_SHUTDOWNED:數(shù)據(jù)庫實例正常關(guān)閉(非standby)控制文件寫入的狀態(tài)就是這個狀態(tài)
DB_SHUTDOWNED_IN_RECOVERY:standby實例正常關(guān)閉,控制文件寫入的狀態(tài)是這個狀態(tài)。是由CreateRestartPoint修改該狀態(tài)。
DB_SHUTDOWNING:非standby實例在關(guān)閉時,做checkpoint:CreateCheckPoint,開始做時修改為該狀態(tài),做完后修改為DB_SHUTDOWNED狀態(tài)。
DB_IN_CRASH_RECOVERY:實例異常關(guān)閉,重啟后,恢復時需要將實例先置為該狀態(tài)
DB_IN_ARCHIVE_RECOVERY:standby實例重啟后置為該狀態(tài)。
DB_IN_PRODUCTION:非standby實例正常重啟后就是這個狀態(tài),standby是DB_IN_ARCHIVE_RECOVERY
分析
1、DB_STARTUP
initdb->BootStrapXLOG: memset(ControlFile, 0, sizeof(ControlFileData)); ... ControlFile->state = DB_SHUTDOWNED; ... WriteControlFile();
初始化時,首先將其狀態(tài)初始化為DB_STARTUP,然后立即置成DB_SHUTDOWNED并將其刷寫到磁盤。
2、StartupXLOG
StartupXLOG-> ReadControlFile(); ... readRecoveryCommandFile();-> |--... | for (item = head; item; item = item->next){ | if (strcmp(item->name, "restore_command") == 0){ | ... | }... | else if (strcmp(item->name, "standby_mode") == 0){ | if (!parse_bool(item->value, &StandbyModeRequested)) | }... | } | ... |-- ArchiveRecoveryRequested = true; ... if (ArchiveRecoveryRequested && (ControlFile->minRecoveryPoint != InvalidXLogRecPtr || ControlFile->backupEndRequired || ControlFile->backupEndPoint != InvalidXLogRecPtr || ControlFile->state == DB_SHUTDOWNED)){ InArchiveRecovery = true; if (StandbyModeRequested) StandbyMode = true; } ... record = ReadCheckpointRecord(xlogreader, checkPointLoc, 1, true); ... if (InRecovery){ if (InArchiveRecovery)//何時? ControlFile->state = DB_IN_ARCHIVE_RECOVERY; else ControlFile->state = DB_IN_CRASH_RECOVERY; ... UpdateControlFile(); replay... } ... LWLockAcquire(ControlFileLock, LW_EXCLUSIVE); ControlFile->state = DB_IN_PRODUCTION; UpdateControlFile(); LWLockRelease(ControlFileLock); ...
只要有recovery.conf文件,ArchiveRecoveryRequested即為TRUE->InArchiveRecovery = true,配置了standby_mode=on,那么StandbyMode=TRUE。這樣standby啟動后,ControlFile->state為DB_IN_ARCHIVE_RECOVERY狀態(tài)。
3、checkpoint
CheckpointerMain-> for (;;){ ... if (shutdown_requested){ ShutdownXLOG(0, 0);-> |--if (RecoveryInProgress()){ | CreateRestartPoint(CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_IMMEDIATE); | }else{ | CreateCheckPoint(CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_IMMEDIATE); | } |--... proc_exit(0); } ... if (do_checkpoint){ do_restartpoint = RecoveryInProgress(); ... if (flags & CHECKPOINT_END_OF_RECOVERY)//flags從哪來? do_restartpoint = false; ... if (!do_restartpoint){ CreateCheckPoint(flags); ckpt_performed = true; } else ckpt_performed = CreateRestartPoint(flags); } }
備機上做checkpoint調(diào)用CreateRestartPoint,主機做checkpoint調(diào)用CreateCheckPoint
CreateCheckPoint(int flags)-> if (flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY)) shutdown = true; else shutdown = false; ... if (shutdown){ LWLockAcquire(ControlFileLock, LW_EXCLUSIVE); ControlFile->state = DB_SHUTDOWNING; ControlFile->time = (pg_time_t) time(NULL); UpdateControlFile(); LWLockRelease(ControlFileLock); } ... LWLockAcquire(ControlFileLock, LW_EXCLUSIVE); if (shutdown) ControlFile->state = DB_SHUTDOWNED; ... UpdateControlFile(); LWLockRelease(ControlFileLock);
shutdown時,先將狀態(tài)置為DB_SHUTDOWNING,最后將狀態(tài)置為DB_SHUTDOWNED
CreateRestartPoint(int flags)-> LWLockAcquire(CheckpointLock, LW_EXCLUSIVE); SpinLockAcquire(&XLogCtl->info_lck); lastCheckPointRecPtr = XLogCtl->lastCheckPointRecPtr; lastCheckPointEndPtr = XLogCtl->lastCheckPointEndPtr; lastCheckPoint = XLogCtl->lastCheckPoint; SpinLockRelease(&XLogCtl->info_lck); if (!RecoveryInProgress()){ LWLockRelease(CheckpointLock); return false; } ... if (XLogRecPtrIsInvalid(lastCheckPointRecPtr) ||lastCheckPoint.redo <= ControlFile->checkPointCopy.redo){ UpdateMinRecoveryPoint(InvalidXLogRecPtr, true); if (flags & CHECKPOINT_IS_SHUTDOWN){ LWLockAcquire(ControlFileLock, LW_EXCLUSIVE); ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY; ControlFile->time = (pg_time_t) time(NULL); UpdateControlFile(); LWLockRelease(ControlFileLock); } LWLockRelease(CheckpointLock); return false; } ... LWLockAcquire(ControlFileLock, LW_EXCLUSIVE); if (ControlFile->state == DB_IN_ARCHIVE_RECOVERY && ControlFile->checkPointCopy.redo < lastCheckPoint.redo){ ... if (flags & CHECKPOINT_IS_SHUTDOWN) ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY; UpdateControlFile(); } LWLockRelease(ControlFileLock); ...
備機shutdown,將狀態(tài)置為DB_SHUTDOWNED_IN_RECOVERY