`

PostgreSQL启动过程中的那些事七:初始化共享内存和信号二:shmem中初始化xlog

阅读更多

        pg 初始化完 shmem ,给其加上索引 "ShmemIndex" 后,接着就在 shmem 里初始化 xlog

1 先上个图,看一下函数调用过程梗概,中间略过部分细节


初始化 xlog 方法调用流程图

 

2 初始化 xlog 相关结构

话说 main()-> ->PostmasterMain()-> ->reset_shared() -> CreateSharedMemoryAndSemaphores()> ->XLOGSHmemInit() ,初始化控制文件 data/global/pg_control 相关数据结构及事务日志 xlog 相关数据结构,相关结构定义在下面。

 

typedef struct ControlFileData

{

       /*

         * Unique system identifier --- to ensure we match up xlog files with the

         * installation that produced them.

         */

       uint64           system_identifier;

 

       /*

         * Version identifier information.   Keep these fields at the same offset,

         * especially pg_control_version; they won't be real useful if they move

         * around.   (For historical reasons they must be 8 bytes into the file

         * rather than immediately at the front.)

         *

         * pg_control_version identifies the format of pg_control itself.

         * catalog_version_no identifies the format of the system catalogs.

         *

         * There are additional version identifiers in individual files; for

         * example, WAL logs contain per-page magic numbers that can serve as

         * version cues for the WAL log.

         */

       uint32           pg_control_version;         /* PG_CONTROL_VERSION */

       uint32           catalog_version_no;        /* see catversion.h */

 

       /*

         * System status data

         */

       DBState        state;                   /* see enum above */

       pg_time_t    time;                    /* time stamp of last pg_control update */

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

       XLogRecPtr  prevCheckPoint; /* previous check point record ptr */

 

       CheckPoint checkPointCopy; /* copy of last check point record */

 

       /*

         * These two values determine the minimum point we must recover up to

         * before starting up:

         *

         * minRecoveryPoint is updated to the latest replayed LSN whenever we

         * flush a data change during archive recovery. That guards against

         * starting archive recovery, aborting it, and restarting with an earlier

         * stop location. If we've already flushed data changes from WAL record X

         * to disk, we mustn't start up until we reach X again. Zero when not

         * doing archive recovery.

         *

         * backupStartPoint is the redo pointer of the backup start checkpoint, if

         * we are recovering from an online backup and haven't reached the end of

         * backup yet. It is reset to zero when the end of backup is reached, and

         * we mustn't start up before that. A boolean would suffice otherwise, but

         * we use the redo pointer as a cross-check when we see an end-of-backup

         * record, to make sure the end-of-backup record corresponds the base

         * backup we're recovering from.

         */

       XLogRecPtr  minRecoveryPoint;

       XLogRecPtr  backupStartPoint;

 

       /*

         * Parameter settings that determine if the WAL can be used for archival

         * or hot standby.

         */

       int                 wal_level;

       int                 MaxConnections;

       int                 max_prepared_xacts;

       int                 max_locks_per_xact;

 

       /*

         * This data is used to check for hardware-architecture compatibility of

         * the database and the backend executable.  We need not check endianness

         * explicitly, since the pg_control version will surely look wrong to a

         * machine of different endianness, but we do need to worry about MAXALIGN

         * and floating-point format.  (Note: storage layout nominally also

         * depends on SHORTALIGN and INTALIGN, but in practice these are the same

         * on all architectures of interest.)

         *

         * Testing just one double value is not a very bulletproof test for

         * floating-point compatibility, but it will catch most cases.

         */

       uint32           maxAlign;           /* alignment requirement for tuples */

       double         floatFormat;       /* constant 1234567.0 */

#define FLOATFORMAT_VALUE      1234567.0

 

       /*

         * This data is used to make sure that configuration of this database is

         * compatible with the backend executable.

         */

       uint32           blcksz;                 /* data block size for this DB */

       uint32           relseg_size;   /* blocks per segment of large relation */

 

       uint32           xlog_blcksz; /* block size within WAL files */

       uint32           xlog_seg_size;     /* size of each WAL segment */

 

       uint32           nameDataLen;  /* catalog name field width */

       uint32           indexMaxKeys;   /* max number of columns in an index */

 

       uint32           toast_max_chunk_size;   /* chunk size in TOAST tables */

 

       /* flag indicating internal format of timestamp, interval, time */

       bool             enableIntTimes; /* int64 storage enabled? */

 

       /* flags indicating pass-by-value status of various types */

       bool             float4ByVal; /* float4 pass-by-value? */

       bool             float8ByVal; /* float8, int8, etc pass-by-value? */

 

       /* CRC of all above ... MUST BE LAST! */

       pg_crc32     crc;

} ControlFileData;

 

/*

  * Body of CheckPoint XLOG records.  This is declared here because we keep

  * a copy of the latest one in pg_control for possible disaster recovery.

  * Changing this struct requires a PG_CONTROL_VERSION bump.

  */

typedef struct CheckPoint

{

       XLogRecPtr  redo;                   /* next RecPtr available when we began to

                                                          * create CheckPoint (i.e. REDO start point) */

       TimeLineID    ThisTimeLineID; /* current TLI */

       uint32           nextXidEpoch;   /* higher-order bits of nextXid */

       TransactionId nextXid;           /* next free XID */

       Oid               nextOid;             /* next free OID */

       MultiXactId nextMulti;            /* next free MultiXactId */

       MultiXactOffset nextMultiOffset;  /* next free MultiXact offset */

       TransactionId oldestXid;  /* cluster-wide minimum datfrozenxid */

       Oid               oldestXidDB;       /* database with minimum datfrozenxid */

       pg_time_t    time;                    /* time stamp of checkpoint */

 

       /*

         * Oldest XID still running. This is only needed to initialize hot standby

         * mode from an online checkpoint, so we only bother calculating this for

         * online checkpoints and only when wal_level is hot_standby. Otherwise

         * it's set to InvalidTransactionId.

         */

       TransactionId oldestActiveXid;

} CheckPoint;

 

/*

  * Total shared-memory state for XLOG.

  */

typedef struct XLogCtlData

{

    /* Protected by WALInsertLock: */

    XLogCtlInsert Insert;

 

    /* Protected by info_lck: */

    XLogwrtRqst LogwrtRqst;

    XLogwrtResult LogwrtResult;

    uint32      ckptXidEpoch;   /* nextXID & epoch of latest checkpoint */

    TransactionId ckptXid;

    XLogRecPtr  asyncXactLSN;   /* LSN of newest async commit/abort */

    uint32      lastRemovedLog; /* latest removed/recycled XLOG segment */

    uint32      lastRemovedSeg;

 

    /* Protected by WALWriteLock: */

    XLogCtlWrite Write;

 

    /*

      * These values do not change after startup, although the pointed-to pages

      * and xlblocks values certainly do.  Permission to read/write the pages

      * and xlblocks values depends on WALInsertLock and WALWriteLock.

      */

    char       *pages;          /* buffers for unwritten XLOG pages */

    XLogRecPtr *xlblocks;       /* 1st byte ptr-s + XLOG_BLCKSZ */

    int         XLogCacheBlck;  /* highest allocated xlog buffer index */

    TimeLineID  ThisTimeLineID;

    TimeLineID  RecoveryTargetTLI;

 

    /*

      * archiveCleanupCommand is read from recovery.conf but needs to be in

      * shared memory so that the bgwriter process can access it.

      */

    char        archiveCleanupCommand[MAXPGPATH];

 

    /*

      * SharedRecoveryInProgress indicates if we're still in crash or archive

      * recovery.  Protected by info_lck.

      */

    bool        SharedRecoveryInProgress;

 

    /*

      * SharedHotStandbyActive indicates if we're still in crash or archive

      * recovery.  Protected by info_lck.

      */

    bool        SharedHotStandbyActive;

 

    /*

      * recoveryWakeupLatch is used to wake up the startup process to continue

      * WAL replay, if it is waiting for WAL to arrive or failover trigger file

      * to appear.

      */

    Latch       recoveryWakeupLatch;

 

    /*

      * During recovery, we keep a copy of the latest checkpoint record here.

      * Used by the background writer when it wants to create a restartpoint.

      *

      * Protected by info_lck.

      */

    XLogRecPtr  lastCheckPointRecPtr;

    CheckPoint  lastCheckPoint;

 

    /* end+1 of the last record replayed (or being replayed) */

    XLogRecPtr  replayEndRecPtr;

    /* end+1 of the last record replayed */

    XLogRecPtr  recoveryLastRecPtr;

    /* timestamp of last COMMIT/ABORT record replayed (or being replayed) */

    TimestampTz recoveryLastXTime;

    /* Are we requested to pause recovery? */

    bool        recoveryPause;

 

    slock_t     info_lck;       /* locks shared variables shown above */

} XLogCtlData;

 

/*

  * Shared state data for XLogInsert.

  */

typedef struct XLogCtlInsert

{

    XLogwrtResult LogwrtResult; /* a recent value of LogwrtResult */

    XLogRecPtr  PrevRecord;     /* start of previously-inserted record */

    int         curridx;        /* current block index in cache */

    XLogPageHeader currpage;    /* points to header of block in cache */

    char       *currpos;        /* current insertion point in cache */

    XLogRecPtr  RedoRecPtr;     /* current redo point for insertions */

    bool        forcePageWrites;    /* forcing full-page writes for PITR? */

 

    /*

      * exclusiveBackup is true if a backup started with pg_start_backup() is

      * in progress, and nonExclusiveBackups is a counter indicating the number

      * of streaming base backups currently in progress. forcePageWrites is set

      * to true when either of these is non-zero. lastBackupStart is the latest

      * checkpoint redo location used as a starting point for an online backup.

      */

    bool        exclusiveBackup;

    int         nonExclusiveBackups;

    XLogRecPtr  lastBackupStart;

} XLogCtlInsert;

 

XLOGSHmemInit() 函数里,首先在 shmem 的哈希表索引 "ShmemIndex" 上给控制文件 pg_control 增加一个 HashElement ShmemIndexEnt entry ), shmem 里根据 ControlFileData 大小调用 ShmemAlloc() 分配内存空间,使 ShmemIndexEnt 的成员 location 指向该空间, size 成员记录该空间大小

XLOGSHmemInit() 调用 ShmemInitStruct() 在其中 调用 hash_search() 在哈希表索引 "ShmemIndex" 中查找 "XLOG Ctl" ,如果没有,就在 shmemIndex 中给 "XLOG Ctl" 分一个 HashElement ShmemIndexEnt entry ,在其中的 Entry 中写上 "XLOG Ctl" 。返回 ShmemInitStruct() ,再调用ShmemAlloc() 在共享内存上给"XLOG Ctl" 相关结构(见下面“ XLog 相关结构图” )分配空间,设置 entry (在这儿及ShmemIndexEnt 类型变量)的成员 location 指向该空间, size 成员记录该空间大小 最后返回 XLOGShmemInit() ,让 XLogCtlData * 类型静态 全局变量 XLogCtl 指向在shmem 里给"XLOG Ctl" 相关结构分配的内存地址,设置其中XLogCtlData 结构类型的成员值。 初始化完成后数据结构如下图。

 

初始化完 xlog 的内存结构图

       为了精简上图,把创建 shmem 的哈希表索引 "ShmemIndex" 时创建的 HCTL 结构删掉了,这个结构的作用是记录创建可扩展哈希表的相关信息。增加了左边灰色底的部分,描述 共享内存 /shmem 里各变量物理布局概览,由下往上,由低地址到高地址。其中的 "Control File" ControlFileDate "XLOG Ctl" xlog 的相关结构图下面分别给出,要不上面的图太大了。

 

 

 

 

控制文件结构图

       上图中 ControlFileData 结构中的 XLogRecPtr CheckPoint 不是指针,因此应该用右边的相应结构图代替,把这两个合进去有点费劲,将就着看吧。


XLog 相关结构图

  • 大小: 49.1 KB
  • 大小: 65.9 KB
  • 大小: 110.3 KB
  • 大小: 152.6 KB
0
0
分享到:
评论

相关推荐

    nacos-2.0.1 postgresql初始化脚本

    nacos-2.0.1 postgresql初始化脚本

    quartz-2.2.3版本的quartz初始化sql语句

    在Quartz 2.2.3版本中,初始化数据库是使用Quartz的关键步骤,因为Quartz依赖于一个持久化存储来保存作业和触发器的信息。这个过程通常涉及执行一系列SQL语句来创建必要的表结构。 Quartz的初始化SQL语句主要用于...

    Postgresql存储过程

    Postgresql存储过程详解 Postgresql存储过程是指在Postgresql数据库中定义的一组SQL语句的...Postgresql存储过程是Postgresql数据库中的一种强大工具,可以实现复杂的操作,提高数据库服务器的性能和应用程序的性能。

    8基础 5:初始化 MySQL 数据库并建立连接(3).md

    在本节内容中,我们将深入了解如何使用Go语言和GORM库初始化MySQL数据库,并建立与该数据库的连接。GORM是一个流行的Go语言ORM(对象关系映射)库,它允许开发者通过编程方式与数据库交互,而无需编写大量的SQL代码...

    关于PostGreSQL中的存储过程

    其中,存储过程是一个非常重要的概念,本文将对 PostgreSQL 中的存储过程进行详细的介绍和解释。 什么是存储过程 存储过程是一组为了完成特定任务而编写的 SQL 语句集合。它可以将复杂的操作封装起来,以便于重复...

    PostgreSQL中文手册9.2

    一、服务器进程的启动和关闭: 一、服务器进程的启动和关闭: 一、服务器进程的启动和关闭: 一、服务器进程的启动和关闭: 一、服务器进程的启动和关闭: 一、服务器进程的启动和关闭: . 50 PostgreSQL PostgreSQL...

    linux配置postgresql

    在 Linux 系统中,可以通过以下命令启动 PostgreSQL: ``` # su – postgres pg$ /usr/local/pgsql/bin/postmaster ``` 这将启动 PostgreSQL 数据库服务器,并允许用户访问数据库。 配置 PostgreSQL 数据库需要多个...

    postgresql-42.3.1-API文档-中文版.zip

    赠送jar包:postgresql-42.3.1.jar; 赠送原API文档:postgresql-42.3.1-javadoc.jar; 赠送源代码:postgresql-42.3.1-sources.jar;...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    在windows下手动初始化PostgreSQL数据库教程

    在初始化过程中,initdb会生成一系列的配置文件和数据库模板,并设置默认的最大连接数、共享缓冲区等参数。完成初始化之后,会得到一系列的成功消息,表明数据库系统已经准备就绪。 最后,使用pg_ctl工具来启动...

    Ubuntu 下源码安装Postgresql

    七、初始化数据库目录 使用 initdb 命令初始化数据库目录: ./initdb -D PGDATA/postgres 八、启动数据库 使用以下命令启动数据库: ./postgres -D PGDATA/postgres 九、配置开机启动 为了使数据库在开机时...

    windows环境下新版12.2postgreSQL的安装+初始化配置+启动

    总之,安装和配置Windows上的PostgreSQL 12.2涉及到下载安装文件、初始化数据库、设置服务启动和配置连接参数等步骤。遵循这些步骤,你就可以顺利地在Windows环境中搭建起一个功能齐全的PostgreSQL数据库系统了。 ...

    Linux下PostgreSQL安装与开机启动

    初始化数据库是设置PostgreSQL的重要步骤之一,它创建数据库集群。 ##### 操作命令: ```bash su postgres /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data ``` **解释:** - `su postgres`:切换到`...

    postgresql-42.2.5-API文档-中英对照版.zip

    赠送jar包:postgresql-42.2.5.jar; 赠送原API文档:postgresql-42.2.5-javadoc.jar;...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    postgresql 12、15离线安装包

    3. 使用初始化脚本创建数据库集群,这通常涉及选择数据存储位置、设置监听地址和端口、初始化用户等。 4. 启动PostgreSQL服务,检查是否能够正常运行。 5. 使用pgAdmin或其他工具连接到新安装的数据库,进行数据库的...

    PostgreSQL修炼之道 从小工到专家.pptx

    * PostgreSQL配置:包括设置数据库参数、内存管理和日志记录等方面。 数据类型与表达式 * 数据类型:包括整数、字符串、日期时间、布尔值等基本数据类型。 * 表达式:包括算术运算符、比较运算符、逻辑运算符和...

    postgresql8.2.3 中文文档

    1. **安装与配置**:这部分内容会指导用户如何在不同操作系统上安装 PostgreSQL,包括设置数据目录、初始化数据库集群、配置服务器参数以及启动和停止服务。 2. **SQL语言**:文档详细介绍了SQL的使用,包括数据...

    Postgresql-10安装包

    10. **阅读官方文档**:PostgreSQL 提供详细的官方文档,包括安装、配置和使用指南,强烈建议在安装和使用过程中参考。 **四、安装脚本的使用** 如果你从博主那里获取了安装脚本,通常这是一个包含上述步骤的自动...

    PostGreSQL安装部署系列:Centos 7.9 安装指定PostGreSQL-15版本数据库

    初始化数据库是PostgreSQL安装过程中必不可少的一步,可以通过执行以下命令完成: ```bash sudo /usr/pgsql-15/bin/postgresql-15-setup initdb ``` 初始化完成后,将会创建数据库目录并设置默认权限。 ##### 3.4...

    PostgreSQL 9.6 RPM包

    5. **初始化数据库**:安装完成后,你需要初始化PostgreSQL数据库集群。使用`initdb`命令,例如: ``` sudo /usr/pgsql-9.6/bin/initdb -D /var/lib/pgsql/9.6/data ``` 这将在指定目录创建数据库集群。 6. **...

    框架使用到的初始化脚本

    在IT行业中,数据库初始化脚本是项目启动前的关键步骤,特别是在使用像Spring MVC这样的MVC框架构建应用时。本文将详细解析标题“框架使用到的初始化脚本”以及描述中涉及的知识点,主要关注与MySQL数据库相关的初始...

Global site tag (gtag.js) - Google Analytics