Checkpoints and the Active Log

原文:http://msdn.microsoft.com/en-us/library/ms179355(SQL.90).aspx

检查点在数据库中的执行过程:

  1. 在日志文件中写入LOP_BEGIN_CKPT表示checkpoint开始、
  2. 记录MinLSN,MinLSN=min(LSN of the start of the checkpoint,LSN of the start of the oldest active transaction)
  3. 如果数据库使用的是简单恢复模式,则截断(truncate)MinLSN之前的所有VLFs。如果是完整恢复或者是大容量日志模式,不会截断任何事务日志。
  4. 将dirty log 和dirty page 写入磁盘。对于dirty page 来说,即使其中存在未提交的事务,也要写入的磁盘。因为已经记录了MinLSN,所以即使脏页写入了磁盘,也可以回滚。
  5. 在日志文件中写入LOP_END_CKPT表示checkpoint结束

The section of the log file from the MinLSN to the last-written log record is called the active portion of the log, or the active log. This is the section of the log required to do a full recovery of the database. No part of the active log can ever be truncated. All log records must be truncated from the parts of the log before the MinLSN.

首先,Active Log是指从MinLSN开始,到当前最新的Log Record。而Active Log是不会被truncate的,Truncate日志必须从MinLSN之前开始。如果是simple模式,那么在发生checkpoint的时候MinLSN之前的所有日志被truncate掉。但是在full模式下,发生checkpoint的时候并不truncate日志,它只会在backup transaction log完成之后,truncate掉MinLSN之前的所有log。总而言之,如果发生truncate,那么MinLSN之前的所有日志必定全部被truncate。

The following illustration shows a simplified version of the end-of-a-transaction log with two active transactions. Checkpoint records have been compacted to a single record.

LSN 148 is the last record in the transaction log. At the time that the recorded checkpoint at LSN 147 was processed, Tran 1 had been committed and Tran 2 was the only active transaction. That makes the first log record for Tran 2 the oldest log record for a transaction active at the time of the last checkpoint. This makes LSN 142, the Begin transaction record for Tran 2, the MinLSN.

如上所述,在最后一个checkpoint(LSN=147)的地方,我们发现Tran1已经提交了,只剩下Tran2没有提交,所以MinLSN就是Tran2的起点,也就是LSN=142的地方。

数据库的还原

如上图所示,假如数据库在LSN=148的地方发生了崩溃,我们需要恢复数据库,那么我们将执行如下操作。

  1. 首先进行完整还原。
  2. 接着事务日志还原,我们需要将数据库恢复到数据库崩溃的那一刻,执行前滚操作。从MinLSN开始redo操作,直到日志末尾
  3. 最后让数据库处于一致状态,也就是进行回滚操作。从日志末尾一致到MinLSN,回滚所有没有commit的事务,比如这里的Tran2.
原文地址:https://www.cnblogs.com/xwdreamer/p/2593474.html