初步学习pg_control文件之十

简介:

接前文 初步学习pg_control文件之九 看下面这个

XLogRecPtr    checkPoint;        /* last check point record ptr */

看看这个pointer究竟保留了什么

初始化的时候:

复制代码
/*                            
 * This func must be called ONCE on system install.  It creates pg_control                            
 * and the initial XLOG segment.                            
 */                            
void                            
BootStrapXLOG(void)                            
{                            
                            
    …                        
    /*                        
     * Set up information for the initial checkpoint record                        
     *                        
     * The initial checkpoint record is written to the beginning of the WAL                        
     * segment with logid=0 logseg=1. The very first WAL segment, 0/0, is not                        
     * used, so that we can use 0/0 to mean "before any valid WAL segment".                        
     */                        
    checkPoint.redo.xlogid = 0;                        
    checkPoint.redo.xrecoff = XLogSegSize + SizeOfXLogLongPHD;                        
                            
    ...                    
    /* Now create pg_control */                        
                            
    memset(ControlFile, 0, sizeof(ControlFileData));                        
                            
    /* Initialize pg_control status fields */                        
    ControlFile->system_identifier = sysidentifier;                        
    ControlFile->state = DB_SHUTDOWNED;                        
    ControlFile->time = checkPoint.time;                        
                            
    ControlFile->checkPoint = checkPoint.redo;                        
    ControlFile->checkPointCopy = checkPoint;                        
                            
    …                        
    WriteControlFile();                        
    …                        
}                            
复制代码

运行的时候:

复制代码
/*                                
 * ProcLastRecPtr points to the start of the last XLOG record inserted by the 
 * current backend.  It is updated for all inserts.  XactLastRecEnd points to
 * end+1 of the last record, and is reset when we end a top-level transaction, 
 * or start a new one; so it can be used to tell if the current transaction has 
 * created any XLOG records.                                
 */                                
static XLogRecPtr ProcLastRecPtr = {0, 0};                                
…                                
                                
                                
/*                                
 * Insert an XLOG record having the specified RMID and info bytes,                                
 * with the body of the record being the data chunk(s) described by                                
 * the rdata chain (see xlog.h for notes about rdata).                                
 *                                
 * Returns XLOG pointer to end of record (beginning of next record).                                
 * This can be used as LSN for data pages affected by the logged action.                                
 * (LSN is the XLOG point up to which the XLOG must be flushed to disk                                
 * before the data page can be written out.  This implements the basic                                
 * WAL rule "write the log before the data".)                                
 *                                
 * NB: this routine feels free to scribble on the XLogRecData structs,                                
 * though not on the data they reference.  This is OK since the XLogRecData                                
 * structs are always just temporaries in the calling code.                                
 */                                
XLogRecPtr                                
XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata)                                
{                                
    …                            
    /*                            
     * In bootstrap mode, we don't actually log anything but XLOG resources;                            
     * return a phony record pointer.                            
     */                            
    if (IsBootstrapProcessingMode() && rmid != RM_XLOG_ID)                            
    {                            
        RecPtr.xlogid = 0;                        
        RecPtr.xrecoff = SizeOfXLogLongPHD;        /* start of 1st chkpt record */                
        return RecPtr;                        
    }                            
                                
    …                            
    /* Record begin of record in appropriate places */                            
    ProcLastRecPtr = RecPtr;                            
    Insert->PrevRecord = RecPtr;                            
    …                            
}                                
复制代码

关闭的时候:

复制代码
/*                                
 * Perform a checkpoint --- either during shutdown, or on-the-fly                                
 *                                
 * flags is a bitwise OR of the following:                                
 *    CHECKPOINT_IS_SHUTDOWN: checkpoint is for database shutdown.                            
 *    CHECKPOINT_END_OF_RECOVERY: checkpoint is for end of WAL recovery.                            
 *    CHECKPOINT_IMMEDIATE: finish the checkpoint ASAP,                            
 *        ignoring checkpoint_completion_target parameter.                        
 *    CHECKPOINT_FORCE: force a checkpoint even if no XLOG activity has occured                            
 *        since the last one (implied by CHECKPOINT_IS_SHUTDOWN or                        
 *        CHECKPOINT_END_OF_RECOVERY).                        
 *                                
 * Note: flags contains other bits, of interest here only for logging purposes.                                
 * In particular note that this routine is synchronous and does not pay                                
 * attention to CHECKPOINT_WAIT.                                
 */                                
void                                
CreateCheckPoint(int flags)                                
{                                
    …                            
    /*                            
     * Update the control file.                            
     */                            
    LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);                            
    if (shutdown)                            
        ControlFile->state = DB_SHUTDOWNED;                        
    ControlFile->prevCheckPoint = ControlFile->checkPoint;                            
    ControlFile->checkPoint = ProcLastRecPtr;                            
    ControlFile->checkPointCopy = checkPoint;                            
    ControlFile->time = (pg_time_t) time(NULL);                            
    /* crash recovery should always recover to the end of WAL */                            
    MemSet(&ControlFile->minRecoveryPoint, 0, sizeof(XLogRecPtr));                            
    UpdateControlFile();                            
    LWLockRelease(ControlFileLock);                            
    …                            
}                                
复制代码

RecoveryMode或者Stand by 的时候:

复制代码
/*                                
 * Establish a restartpoint if possible.                                
 *                                
 * This is similar to CreateCheckPoint, but is used during WAL recovery                                
 * to establish a point from which recovery can roll forward without                                
 * replaying the entire recovery log.                                
 *                                
 * Returns true if a new restartpoint was established. We can only establish                                
 * a restartpoint if we have replayed a safe checkpoint record since last                                
 * restartpoint.                                
 */                                
bool                                
CreateRestartPoint(int flags)                                
{                                
    XLogRecPtr        lastCheckPointRecPtr;                    
    CheckPoint    lastCheckPoint;                        
    uint32        _logId;                    
    uint32        _logSeg;                    
    TimestampTz xtime;                            
                                
    /* use volatile pointer to prevent code rearrangement */                            
    volatile XLogCtlData *xlogctl = XLogCtl;                            
                                
    /*                            
     * Acquire CheckpointLock to ensure only one restartpoint or checkpoint                            
     * happens at a time.                            
     */                            
    LWLockAcquire(CheckpointLock, LW_EXCLUSIVE);                            
                                
    /* Get a local copy of the last safe checkpoint record. */                            
    SpinLockAcquire(&xlogctl->info_lck);                            
    lastCheckPointRecPtr = xlogctl->lastCheckPointRecPtr;                            
    memcpy(&lastCheckPoint, &XLogCtl->lastCheckPoint, sizeof(CheckPoint));                            
    SpinLockRelease(&xlogctl->info_lck);                            
                                
    …                            
    /*                            
     * Update pg_control, using current time.  Check that it still shows                            
     * IN_ARCHIVE_RECOVERY state and an older checkpoint, else do nothing;                            
     * this is a quick hack to make sure nothing really bad happens if somehow                            
     * we get here after the end-of-recovery checkpoint.                            
     */                            
    LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);                            
    if (ControlFile->state == DB_IN_ARCHIVE_RECOVERY &&                            
        XLByteLT(ControlFile->checkPointCopy.redo, lastCheckPoint.redo))                        
    {                            
        ControlFile->prevCheckPoint = ControlFile->checkPoint;                        
        ControlFile->checkPoint = lastCheckPointRecPtr;                        
        ControlFile->checkPointCopy = lastCheckPoint;                        
        ControlFile->time = (pg_time_t) time(NULL);                        
        if (flags & CHECKPOINT_IS_SHUTDOWN)                        
            ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY;                    
        UpdateControlFile();                        
    }                            
    LWLockRelease(ControlFileLock);                            
                                
    …                            
}                                
复制代码

启动的时候:

复制代码
/*                                
 * This must be called ONCE during postmaster or standalone-backend startup                                
 */                                
void                                
StartupXLOG(void)                                
{                                
    …                            
    /*                            
     * Read control file and check XLOG status looks valid.                            
     *                            
     * Note: in most control paths, *ControlFile is already valid and we need                            
     * not do ReadControlFile() here, but might as well do it to be sure.                            
     */                            
    ReadControlFile();                            
                                
    if (ControlFile->state < DB_SHUTDOWNED ||                            
        ControlFile->state > DB_IN_PRODUCTION ||                        
        !XRecOffIsValid(ControlFile->checkPoint.xrecoff))                        
        ereport(FATAL,                        
                (errmsg("control file contains invalid data")));                
                                
    …                            
    if (read_backup_label(&checkPointLoc, &backupEndRequired))                            
    {                            
        …                        
    }                            
    else                            
    {                                                            
        /*                        
         * Get the last valid checkpoint record.  If the latest one according                        
         * to pg_control is broken, try the next-to-last one.                        
         */                        
        checkPointLoc = ControlFile->checkPoint;                        
        RedoStartLSN = ControlFile->checkPointCopy.redo;                        
        record = ReadCheckpointRecord(checkPointLoc, 1);                        
        …                        
    }                            
    …                            
                                
    /* REDO */                            
    if (InRecovery)                            
    {                            
        …                        
        ControlFile->prevCheckPoint = ControlFile->checkPoint;                        
        ControlFile->checkPoint = checkPointLoc;                        
        ControlFile->checkPointCopy = checkPoint;                        
        …                        
    }                            
                                
    …                            
}                                
复制代码
目录
相关文章
|
8天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
7天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
348 130
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
19天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1332 8
|
7天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
336 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
6天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
18天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1422 87
|
6天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
7天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
265 82
2025年阿里云域名备案流程(新手图文详细流程)