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

这一节 pg 初始化进程表,通过 CreateSharedProcArray 例程实现,该例程中初始化的相关结构主要是为了支持维护一个所有活跃backend 进程的PGPROC 结构的未排序数组。 尽管多处用到此数组,但主要是作为确定当前运行进程的事务集合的一种方式。

 

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

 

初始化 SharedProcArray 方法调用流程图

 

2 初始化 xlog 相关结构

话说 main()-> ->PostmasterMain()-> ->reset_shared() -> CreateSharedMemoryAndSemaphores()> ->CreateSharedProcArray() ,调用 ShmemInitStruct() 在其中 调用 hash_search() 在哈希表索引 "ShmemIndex" 中查找 "Proc Array " ,如果没有,就在 shmemIndex 中给 "Proc Array " 分一个 HashElement ShmemIndexEnt entry ,在其中的 Entry 中写上 "Proc Array " 。返回 ShmemInitStruct() ,再调用 ShmemAlloc() 在共享内存上给 "Proc Array " 相关结构(见下面“ Proc Array 相关结构图” )分配空间,设置 entry (在这儿及ShmemIndexEnt 类型变量)的成员 location 指向该空间, size 成员记录该空间大小 最后返回 InitProcGlobal () ,让 ProcArrayStruct * 类型 全局变量 procArray 指向 所分配内存 ,设置 PROC_HDR 结构类型的成员值。

相关结构定义见下面:

/* Our shared memory area */

typedef struct ProcArrayStruct

{

       int                 numProcs;           /* number of valid procs entries */

       int                 maxProcs;           /* allocated size of procs array */

 

       /*

         * Known assigned XIDs handling

         */

       int                 maxKnownAssignedXids;       /* allocated size of array */

       int                 numKnownAssignedXids;       /* currrent # of valid entries */

       int                 tailKnownAssignedXids;   /* index of oldest valid element */

       int                 headKnownAssignedXids;     /* index of newest element, + 1 */

       slock_t          known_assigned_xids_lck;            /* protects head/tail pointers */

 

       /*

         * Highest subxid that has been removed from KnownAssignedXids array to

         * prevent overflow; or InvalidTransactionId if none.  We track this for

         * similar reasons to tracking overflowing cached subxids in PGPROC

         * entries.  Must hold exclusive ProcArrayLock to change this, and shared

         * lock to read it.

         */

       TransactionId lastOverflowedXid;

 

       /*

         * We declare procs[] as 1 entry because C wants a fixed-size array, but

         * actually it is maxProcs entries long.

         */

       PGPROC         *procs[1];              /* VARIABLE LENGTH ARRAY */

} ProcArrayStruct;

 

static ProcArrayStruct *procArray;

 

初始化完 ProcArray 相关结构 的共享内存结构图

 

       为了精简上图,把创建 shmem 的哈希表索引 "ShmemIndex" 时创建的 HCTL 结构删掉了,这个结构的作用是记录创建可扩展哈希表的相关信息,不过这个结构在 "ShmemIndex" 创建完成后也会由于出了对象作用域而消失。增加了左边灰色底的部分,描述 共享内存 /shmem 里各变量物理布局概览,由下往上,由低地址到高地址。 图中黄色的索引项就是本节新增加的索引项。

 

  Proc Array 相关结构图

 

 

猜你喜欢

转载自beigang.iteye.com/blog/1447063