PG数据库内核分析学习笔记_MultiXact日志管理器
MultiXact日志是PG系统用来记录组合事务ID的一种日志. 由于PG采用了多版本并发控制, 因此同一个元组相关的事务ID可能有多个, 为了在加锁(行共享锁)的时候统一操作, PG将与该元组相关联的多个事务ID组合起来用一个MultiXactID代替来管理.同CLOG, SubTrans日志一样, MultiXact日志也是利用SLRU缓冲区来实现.
typedef uint32 TransactionId;
/* MultiXactId must be equivalent to TransactionId, to fit in t_xmax */
typedef TransactionId MultiXactId;
1 MultiXact日志管理器相关数据结构
MultiXact是一个多对一的映射关系, 需要在事务ID数组中标记哪一段映射到一个MultiXactID(如图). 所以在映射的过程中需要存储两种信息, 即需要标识一段事务ID的偏移量(Offset), 还需要记录这段偏移量的大小(NMembers).
图 MultiXactID组合关系

由于需要对MultiXactID的分配进行维护, 于是定义了数据结构MultiXactStateData(数据结构如图所示).
数据结构 MultiXactStateData
// multixact.c
/*
* MultiXact state shared across all backends. All this state is protected
* by MultiXactGenLock. (We also use MultiXactOffsetControlLock and
* MultiXactMemberControlLock to guard accesses to the two sets of SLRU
* buffers. For concurrency's sake, we avoid holding more than one of these
* locks at a time.)
*/
typedef struct MultiXactStateData
{
/* 下一个可分配的 MultiXactId */
MultiXactId nextMXact;
/* 下一个对应MultiXactId的起始偏移量 */
MultiXactOffset nextOffset;
/* Have we completed multixact startup? */
bool finishedStartup;
/*
* Oldest multixact that is still potentially referenced by a relation.
* Anything older than this should not be consulted. These values are
* updated by vacuum.
*/
MultiXactId oldestMultiXactId;
Oid oldestMultiXactDB;
/*
* Oldest multixact offset that is potentially referenced by a multixact
* referenced by a relation. We don't always know this value, so there's
* a flag here to indicate whether or not we currently do.
*/
MultiXactOffset oldestOffset;
bool oldestOffsetKnown;<

本文深入探讨了PostgreSQL数据库中的MultiXact日志管理机制,详细介绍了其核心数据结构MultiXactStateData及mXactCacheEnt的作用,阐述了MultiXactID的映射原理和存储方式,并解释了如何通过SLRU缓冲区实现MultiXact日志的高效管理。
最低0.47元/天 解锁文章
568

被折叠的 条评论
为什么被折叠?



