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 standby checkpoint

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

Share

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

If the database is shut down abnormally, and there is no time or opportunity to do checkpoint when the database is closed, you need to recover from the beginning of the last consistency check.

PostgreSQL slave checkpoint cannot generate checkpoint WAL, because if you write such a type of checkpoint, the received WAL will be disrupted, then the log will be confused and playback will be problematic.

So the question is, does the standby support checkpoint? What did his checkpoint do?

In order to shorten the recovery time of PostgreSQL, checkpoint, or CreateRestartPoint, is also supported on the standby. But the checkpoint record site of its pg_control file is the checkpoint record location in the WAL transmitted from the host computer.

1. Standby playback

StartupXLOG do {... RmgrTable [record-> xl_rmid] .RM _ redo (xlogreader); / / playback. Record = ReadRecord (xlogreader, InvalidXLogRecPtr, LOG, false); / / read a xlog} while (record! = NULL)

2. Playback function

Voidxlog_redo (XLogReaderState * record) {... Else if (info = = XLOG_CHECKPOINT_SHUTDOWN) {... Memcpy (& checkPoint, XLogRecGetData (record), sizeof (CheckPoint)); RecoveryRestartPoint (& checkPoint);} else if (info = = XLOG_CHECKPOINT_ONLINE) {... Memcpy (& checkPoint, XLogRecGetData (record), sizeof (CheckPoint)); RecoveryRestartPoint (& checkPoint);}.

3 、 RecoveryRestartPoint

Static voidRecoveryRestartPoint (const CheckPoint * checkPoint) {... SpinLockAcquire (& XLogCtl- > info_lck); XLogCtl- > lastCheckPointRecPtr = ReadRecPtr;//ReadRecPtr is the position after reading the checkpoint record XLogCtl- > lastCheckPointEndPtr = EndRecPtr; XLogCtl- > lastCheckPoint = * checkPoint; SpinLockRelease (& XLogCtl- > info_lck);}

4. ReadRecPtr assignment

ReadRecord for (;;) {char * errormsg; record = XLogReadRecord (xlogreader, RecPtr, & errormsg); ReadRecPtr = xlogreader- > ReadRecPtr; EndRecPtr = xlogreader- > EndRecPtr;.}

5. Standby createcheckpoint

BoolCreateRestartPoint (int flags) {LWLockAcquire (CheckpointLock, LW_EXCLUSIVE); / * Get a local copy of the last safe checkpoint record. * / SpinLockAcquire (& XLogCtl- > info_lck); lastCheckPointRecPtr = XLogCtl- > lastCheckPointRecPtr;//checkpoint comes from XLogCtl- > lastCheckPointRecPtr lastCheckPointEndPtr = XLogCtl- > lastCheckPointEndPtr; lastCheckPoint = XLogCtl- > lastCheckPoint; SpinLockRelease (& XLogCtl- > info_lck); If (XLogRecPtrIsInvalid (lastCheckPointRecPtr) | | lastCheckPoint.redo checkPointCopy.redo) {/ / after the last checkpoint record is played back, the standby machine manually executes the checkpoint command 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){ ControlFile->

The location of prevCheckPoint = ControlFile- > checkPoint; ControlFile- > checkPoint = lastCheckPointRecPtr;//checkpoint ControlFile- > checkPointCopy = lastCheckPoint; ControlFile- > time = (pg_time_t) time (NULL); If (flags & CHECKPOINT_IS_SHUTDOWN) ControlFile- > state = DB_SHUTDOWNED_IN_RECOVERY; UpdateControlFile ();}. Return true;}

6. Standby shutdown

VoidShutdownXLOG (int code, Datum arg) {/ * * Signal walsenders to move to stopping state. * / WalSndInitStopping (); / * Wait for WAL senders to be in stopping state. This prevents commands * from writing new WAL. * / WalSndWaitStopping (); if (RecoveryInProgress ()) / / write checkpoint CreateRestartPoint (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_IMMEDIATE); else {/ * * If archiving is enabled, rotate the last XLOG file so that all the * remaining records are archived (postmaster wakes up the archiver * process one more time at the end of shutdown). The checkpoint * record will go to the next XLOG file and won't be archived (yet). * / if (XLogArchivingActive () & & XLogArchiveCommandSet ()) RequestXLogSwitch (false); CreateCheckPoint (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_IMMEDIATE);} ShutdownCLOG (); ShutdownCommitTs (); ShutdownSUBTRANS (); ShutdownMultiXact ();}

7. Summary

The PostgreSQL slave can also write checkpoints to avoid all WAL after the last checkpoint (generated by the main database and played back in the WAL) every time the slave is restarted. But the checkpoint loci he recorded were passed from the main library. In this way, there will be a problem. If the host has not done checkpoint for a long time, even if the slave is shut down normally, when it is rebooted, it will start to recover from the previous checkpoint, which will take a long time to recover. Moreover, repeated rebooting will also require repeated recovery from the last checkpoint.

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