1、Checkpoint原理浅析杨向博 DBACheckpoint 定义Checkpoint 触发条件Checkpoint 会做什么Checkpoint skipped机制Checkpoint 过程记录Checkpoint 定义Checkpointsarepointsinthesequenceoftransactionsatwhichitisguaranteedthattheheapandindexdatafileshavebeenupdatedwithallinformationwrittenbeforethatcheckpoint.Atcheckpointtime,alldirtydatapa
2、gesareflushedtodiskandaspecialcheckpointrecordiswrittentothelogfile.(ThechangerecordswerepreviouslyflushedtotheWALfiles.)Intheeventofacrash,thecrashrecoveryprocedurelooksatthelatestcheckpointrecordtodeterminethepointinthelog(knownastheredorecord)fromwhichitshouldstarttheREDOoperation.Anychangesmadet
3、odatafilesbeforethatpointareguaranteedtobealreadyondisk.Hence,afteracheckpoint,logsegmentsprecedingtheonecontainingtheredorecordarenolongerneededandcanberecycledorremoved.(WhenWALarchivingisbeingdone,thelogsegmentsmustbearchivedbeforebeingrecycledorremoved.)简单来说,checkpoint就是一个事务顺序的记录点。checkpoint主要是进
4、行刷脏页,redo时会参考checkpoint进行日志回放。除了刷脏之外还会更新一些位点信息,清理一些不再需要的wal。Checkpoint 触发条件在PostgreSQL中Checkpoint是由checkpointer进程执行的。Checkpointer进程的主流程是一个无条件的for循环,在未触发checkpoint时一直在WaitLatch中sleep,也就是在epoll_wait中观察list链表,查看是否有事件句柄已经就绪(某个条件在触发checkpoint);如果已经存在就绪事件,则wakeup(通过SetLatch中writepipe的方式wakeup),执行checkpoin
5、t。Checkpoint 触发条件触发checkpoint的Flags:/*ThesedirectlyaffectthebehaviorofCreateCheckPointandsubsidiaries*/#defineCHECKPOINT_IS_SHUTDOWN0 x0001/*Checkpointisforshutdown*/#defineCHECKPOINT_END_OF_RECOVERY.0 x0002/*Likeshutdowncheckpoint,but*issuedatendofWALrecovery*/#defineCHECKPOINT_IMMEDIATE0 x0004/*Do
6、itwithoutdelays*/#defineCHECKPOINT_FORCE0 x0008/*Forceevenifnoactivity*/#defineCHECKPOINT_FLUSH_ALL0 x0010/*Flushallpages,includingthose*belongingtounloggedtables*/*TheseareimportanttoRequestCheckpoint*/#defineCHECKPOINT_WAIT0 x0020/*Waitforcompletion*/#defineCHECKPOINT_REQUESTED0 x0040/*Checkpointr