clog 就是 commit log
用来记录事物最终状态的日志,放在数据库目录的pg_clog下面,
PG中,每一个事务的事务号是一个32的整数,有三个是比较特殊的
/* ---------------- * Special transaction ID values * * BootstrapTransactionId is the XID for "bootstrap" operations,and * FrozenTransactionId is used for very old tuples. Both should * always be considered valid. * * FirstNormalTransactionId is the first "normal" transaction id. * Note: if you need to change it,you must change pg_class.h as well. * ---------------- */ #define InvalidTransactionId ((TransactionId) 0) #define BootstrapTransactionId ((TransactionId) 1) #define FrozenTransactionId ((TransactionId) 2) #define FirstNormalTransactionId ((TransactionId) 3) #define MaxTransactionId ((TransactionId) 0xFFFFFFFF)
有四种状态,每个状态只需要两位就可以表示,
#define TRANSACTION_STATUS_IN_PROGRESS 0x00 #define TRANSACTION_STATUS_COMMITTED 0x01 #define TRANSACTION_STATUS_ABORTED 0x02 #define TRANSACTION_STATUS_SUB_COMMITTED 0x03
这样当事务提交时,就可以把事务的状态存在一个独立的日志文件中,文件中每两位在文件中的位置对应事务ID
实际的实现中,是通过一个LRU算法,从共享内存分出8个页面来实现的。