我的编程空间,编程开发者的网络收藏夹
学习永远不晚

PostgreSQL 源码解读(112)- WAL#8(XLogCtrl数据结构)

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

PostgreSQL 源码解读(112)- WAL#8(XLogCtrl数据结构)

本节简单介绍了XLOG全局(所有进程之间)共享的数据结构:XLogCtlData和XLogCtlInsert。在这两个结构体中,存储了REDO point/Lock等相关重要的信息.

一、数据结构

XLogCtlInsert
WAL插入记录时使用的共享数据结构


typedef struct XLogCtlInsert
{
    //包含CurrBytePos和PrevBytePos的lock
    slock_t     insertpos_lck;  

    
    uint64      CurrBytePos;
    uint64      PrevBytePos;

    
    char        pad[PG_CACHE_LINE_SIZE];

    
    //插入时的当前redo point
    XLogRecPtr  RedoRecPtr;     
    //为PITR强制执行full-page写?
    bool        forcePageWrites;    
    //是否全页写?
    bool        fullPageWrites;

    
    ExclusiveBackupState exclusiveBackupState;
    int         nonExclusiveBackups;
    XLogRecPtr  lastBackupStart;

    
    WALInsertLockPadded *WALInsertLocks;
} XLogCtlInsert;

XLogCtl
XLOG的所有共享内存状态信息


typedef struct XLogCtlData
{
    XLogCtlInsert Insert;//插入控制器

    
    //------ 通过info_lck锁保护
    XLogwrtRqst LogwrtRqst;
    //Insert->RedoRecPtr最近的拷贝
    XLogRecPtr  RedoRecPtr;     
    //最后的checkpoint的nextXID & epoch
    uint32      ckptXidEpoch;   
    TransactionId ckptXid;
    //最新异步提交/回滚的LSN
    XLogRecPtr  asyncXactLSN;   
    //slot需要的最"老"的LSN
    XLogRecPtr  replicationSlotMinLSN;  
    //最后移除/回收的XLOG段
    XLogSegNo   lastRemovedSegNo;   

    
    //---- "伪装"的LSN计数器,用于不需要记录日志的关系.通过ulsn_lck锁保护
    XLogRecPtr  unloggedLSN;
    slock_t     ulsn_lck;

    
    //---- 切换后最新的xlog段的时间线和LSN,通过WALWriteLock锁保护
    pg_time_t   lastSegSwitchTime;
    XLogRecPtr  lastSegSwitchLSN;

    
    XLogwrtResult LogwrtResult;

    
    XLogRecPtr  InitializedUpTo;

    
    //未写入的XLOG pages的缓存
    char       *pages;          
    //ptr-s的第一个字节 + XLOG_BLCKSZ
    XLogRecPtr *xlblocks;       
    //已分配的xlog缓冲的索引最高值
    int         XLogCacheBlck;  

    
    TimeLineID  ThisTimeLineID;
    TimeLineID  PrevTimeLineID;

    
    bool        SharedRecoveryInProgress;

    
    bool        SharedHotStandbyActive;

    
    bool        WalWriterSleeping;

    
    Latch       recoveryWakeupLatch;

    
    XLogRecPtr  lastCheckPointRecPtr;
    XLogRecPtr  lastCheckPointEndPtr;
    CheckPoint  lastCheckPoint;

    
    XLogRecPtr  lastReplayedEndRecPtr;
    TimeLineID  lastReplayedTLI;
    XLogRecPtr  replayEndRecPtr;
    TimeLineID  replayEndTLI;
    
    //最后的COMMIT/ABORT回放(或正在回放)记录的时间戳
    TimestampTz recoveryLastXTime;

    
    TimestampTz currentChunkStartTime;
    
    //是否请求暂停恢复
    bool        recoveryPause;

    
    XLogRecPtr  lastFpwDisableRecPtr;
    //锁结构
    slock_t     info_lck;       
} XLogCtlData;

static XLogCtlData *XLogCtl = NULL;

二、跟踪分析

跟踪任意一个后台进程,打印全局变量XLogCtl.

(gdb) p XLogCtl
$6 = (XLogCtlData *) 0x7f391e00ea80
(gdb) p *XLogCtl
$7 = {Insert = {insertpos_lck = 0 '\000', CurrBytePos = 5494680728, PrevBytePos = 5494680616, 
    pad = '\000' <repeats 127 times>, RedoRecPtr = 5510830896, forcePageWrites = false, fullPageWrites = true, 
    exclusiveBackupState = EXCLUSIVE_BACKUP_NONE, nonExclusiveBackups = 0, lastBackupStart = 0, 
    WALInsertLocks = 0x7f391e013100}, LogwrtRqst = {Write = 5510831008, Flush = 5510831008}, RedoRecPtr = 5510830896, 
  ckptXidEpoch = 0, ckptXid = 2036, asyncXactLSN = 5510830896, replicationSlotMinLSN = 0, lastRemovedSegNo = 0, 
  unloggedLSN = 1, ulsn_lck = 0 '\000', lastSegSwitchTime = 1545962218, lastSegSwitchLSN = 5507670464, LogwrtResult = {
    Write = 5510831008, Flush = 5510831008}, InitializedUpTo = 5527601152, pages = 0x7f391e014000 "\230\320\006", 
  xlblocks = 0x7f391e00f088, XLogCacheBlck = 2047, ThisTimeLineID = 1, PrevTimeLineID = 1, 
  archiveCleanupCommand = '\000' <repeats 1023 times>, SharedRecoveryInProgress = false, SharedHotStandbyActive = false, 
  WalWriterSleeping = true, recoveryWakeupLatch = {is_set = 0, is_shared = true, owner_pid = 0}, lastCheckPointRecPtr = 0, 
  lastCheckPointEndPtr = 0, lastCheckPoint = {redo = 0, ThisTimeLineID = 0, PrevTimeLineID = 0, fullPageWrites = false, 
    nextXidEpoch = 0, nextXid = 0, nextOid = 0, nextMulti = 0, nextMultiOffset = 0, oldestXid = 0, oldestXidDB = 0, 
    oldestMulti = 0, oldestMultiDB = 0, time = 0, oldestCommitTsXid = 0, newestCommitTsXid = 0, oldestActiveXid = 0}, 
  lastReplayedEndRecPtr = 0, lastReplayedTLI = 0, replayEndRecPtr = 0, replayEndTLI = 0, recoveryLastXTime = 0, 
  currentChunkStartTime = 0, recoveryPause = false, lastFpwDisableRecPtr = 0, info_lck = 0 '\000'}
(gdb) 

其中:
1.XLogCtl->Insert是XLogCtlInsert结构体变量.
2.RedoRecPtr为5510830896 -> 1/48789B30,该值与pg_control文件中的REDO location相对应.

[xdb@localhost ~]$ pg_controldata|grep REDO
Latest checkpoint's REDO location:    1/48789B30
Latest checkpoint's REDO WAL file:    000000010000000100000048

3.ThisTimeLineID&PrevTimeLineID,时间线ID,值为1.
其他相关信息可对照结构体定义阅读.

三、参考资料

PostgreSQL 源码解读(4)- 插入数据#3(heap_insert)
PG Source Code

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

PostgreSQL 源码解读(112)- WAL#8(XLogCtrl数据结构)

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

编程热搜

目录