postgresql检查点

1 Checkpoint作用
1.1将事务提交的修改写进disk(写脏数据);保证数据库的完整性和一致性。
1.2缩短恢复时间

2 checkpoint所用结构体
typedef struct CheckPoint
{
XLogRecPtr redo;//创建检查点时记录xlog插入位置
TimeLineID ThisTimeLineID;
uint32 nextXidEpoch;
TransactionId nextXid;
Oid nextOid;
MultiXactId nextMulti; MultiXactOffset nextMultiOffset;
TransactionId oldestXid;
Oid oldestXidDB; pg_time_t time;

TransactionId oldestActiveXid;
} CheckPoint;

XLogRecPtr //记录文件内的位置(文件名和文件内偏移)
typedef struct XLogRecPtr
{
uint32 xlogid; /* log file #, 0 based */
uint32 xrecoff; /* byte offset of location in log file */
} XLogRecPtr;

3 checkpoint's position
the checkpoint's position is saved in the file pg_control and log
when pg_control corrupt
To deal with the case where pg_control is corrupt, we should support the possibility of scanning existing log segments in reverse order — newest to oldest — in order to find the latest checkpoint

4checkpoint执行控制
4.1,数据量达到checkpoint_segments(=3)*16M时,系统自动触发;
4.2,时间间隔达到checkpoint_timeout参数值时;
4.3,用户发出checkpoint命令时。

5 创建一个检查点
5.1是recptr是pg_contrl中checkpoint中的recptr进行赋值(如果pg_control中关于checkpoint的记录是正确的话)。
5.2如果pg_control中recptr的记录是不正确的,则checkpoint.recptr = {0}。

// redo保存了xlog的当前插入transaction log位置。
5.2.1INSERT_RECPTR(checkPoint.redo, Insert, Insert->curridx);
其中Insert为:XLogCtlInsert *Insert = &XLogCtl->Insert;
5.2.2checkPoint.nextXid = ShmemVariableCache->nextXid;
5.2.3checkPoint.oldestXid = ShmemVariableCache->oldestXid;
5.2.4checkPoint.oldestXidDB = ShmemVariableCache->oldestXidDB;
6 Flush all data in shared memory to disk, and fsync
实现函数static void
CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
{
CheckPointCLOG();
CheckPointSUBTRANS();
CheckPointMultiXact();
CheckPointPredicate();
// CheckPointRelationMap();
CheckPointBuffers(flags); /* performs all required fsyncs */
/* We deliberately delay 2PC checkpointing as long as possible */
CheckPointTwoPhase(checkPointRedo);
}
保证redo之前的日志记录的数据修改都刷新到数据库磁盘文件中。

Xlog文件随时间的变化 current insertion point
----------------------------------------------------------------->
数据是一致性 | 数据一致性未知
CheckPoint中的redo
创建checkPoint时,首先获取redo位置,然后保证redo之前的xlog记录的修改都刷新到数据磁盘文件中。这样保证的checkPoint左的数据库文件一致性。如果在checkPoint后需要recovery,只需要redo位置向右replay xlog。

4 Now insert the checkpoint record into XLOG.
4.1 rdata.data = (char *) (&checkPoint);
rdata.len = sizeof(checkPoint);
rdata.buffer = InvalidBuffer;
rdata.next = NULL;

4.2 insert
recptr = XLogInsert(RM_XLOG_ID,
shutdown ? XLOG_CHECKPOINT_SHUTDOWN :
XLOG_CHECKPOINT_ONLINE,
&rdata);

4.3 flush to disk
Ensure that all XLOG data through the given position is flushed to disk.
XLogFlush(recptr);

结束一个检查点
把一些统计信息写到LOG(= 15)中。

欢迎批评指正~_~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值