---
title: "PostgreSQL启动过程中的那些事七：初始化共享内存和信号二十：shmem中初始化堆同步扫描、pg子进程相关结构"
author: "BeiGang"
date: "2012-05-14T21:16:46Z"
tags: ["PostgreSQL", "backend结构数组", "共享内存", "堆同步扫描"]
categories: ["数据库"]
slug: "postgresql-shmem-pg"
draft: false
lastmod: "2012-05-14T21:18:22Z"
id: 1528671
url: "https://beigang.iteye.com/blog/1528671"
---

这一节 pg 初始化堆扫描同步支持用到的相关结构。

 

       堆扫描同步是当多个进程在同一个表上做顺序扫描（ sequential scan ）， pg 尝试保持他们同步以减少整体 I/O 需求。这个目标是读每一个页到共享内存仅一次，且使所有参与扫描这个页的进程在这个页被替换出共享内存前处理这个页。

 

活跃 backend 进程列表，这被用来跟踪 pg 有多少个孩子和在需要的时候给他们发送信号。

 

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

 

![](http://dl.iteye.com/upload/attachment/0068/1194/4427f4dc-513c-3984-9c98-a8f667a03cdc.bmp)

 

初始化同步扫描和 Backend 结构数组方法调用流程图

 

**2** **初始化同步扫描相关结构**

话说 main()-&gt; … -&gt;PostmasterMain()-&gt; … -&gt;reset\_shared() -&gt; CreateSharedMemoryAndSemaphores()&gt;…-&gt; SyncScanShmemInit () ，调用 ShmemInitStruct() ， 在其中 调用 hash\_search() 在哈希表索引 "ShmemIndex" 中查找 " Sync Scan Locations List " ，如果没有，就在 shmemIndex 中给 " Sync Scan Locations List " 分一个 HashElement 和 ShmemIndexEnt （ entry ） ，在其中的 Entry 中写上 " Sync Scan Locations List " 。返回 ShmemInitStruct() ，再调用 ShmemAlloc() 在共享内存上给 " Sync Scan Locations List " 相关结构（见下面“ Sync Scan Locations List 相关结构图” ）分配空间，设置 entry （在这儿及ShmemIndexEnt 类型变量）的成员 location 指向该空间， size 成员记录该空间大小 ， 最后返回 SyncScanShmemInit () ，让 ss\_scan\_locations\_t * 类型静态 全局变量 scan\_locations 指向 所分配内存 ，初始化 ss\_scan\_locations\_t 结构类型的成员值。

接着 main()-&gt; … -&gt;PostmasterMain()-&gt; … -&gt;reset\_shared() -&gt; CreateSharedMemoryAndSemaphores() -&gt;…-&gt; ShmemBackendArrayAllocation () ， 调用 ShmemAlloc() 在共享内存上分配 2 * MaxBackends 个 Backend 结构数组 见下面“ Backend 结构数组图” ） ， 让 Backend * 类型静态 全局变量 ShmemBackendArray 指向 所分配内存 ，用memset 把所分配内存置0 。

相关结构定义和图见下面：

 

**typedef** **struct** ss\_scan\_location\_t

{

    RelFileNode relfilenode ; /* identity of a relation \*/

    BlockNumber location ;       /* last-reported location in the relation \*/

} ss\_scan\_location\_t ;

 

**typedef** **struct** ss\_lru\_item\_t

{

    **struct** ss\_lru\_item\_t * prev ;

    **struct** ss\_lru\_item\_t * next ;

    ss\_scan\_location\_t location ;

} ss\_lru\_item\_t ;

 

**typedef** **struct** ss\_scan\_locations\_t

{

    ss\_lru\_item\_t * head ;

    ss\_lru\_item\_t * tail ;

    ss\_lru\_item\_t items \[1];     /* SYNC\_SCAN\_NELEM items \*/

} ss\_scan\_locations\_t ;

 

**#define** SizeOfScanLocations (N) offsetof(ss\_scan\_locations\_t, items\[N])

 

/* Pointer to struct in shared memory \*/

**static** ss\_scan\_locations\_t \*scan\_locations;

 

/* prototypes for internal functions \*/

**static** BlockNumber **ss\_search** ( RelFileNode relfilenode,

         BlockNumber location, bool set);

 

 

**typedef** **struct** bkend

{

    pid\_t      pid;          /* process id of backend \*/

    **long**        cancel\_key;       /* cancel key for cancels for this backend \*/

    **int**         child\_slot;       /* PMChildSlot for this backend, if any \*/

    bool       is\_autovacuum;    /* is it an autovacuum process? \*/

    bool       dead\_end;     /* is it going to send an error and quit? \*/

    Dlelem     elem;         /* list link in BackendList \*/

} Backend;

 

**static** Backend \*ShmemBackendArray;

   
![](http://dl.iteye.com/upload/attachment/0068/1202/7d3b2921-40c0-3165-8bf9-98bf3a72313d.bmp)

  初始化完 Sync Scan Locations List 相关结构 的共享内存结构图

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

 

![](http://dl.iteye.com/upload/attachment/0068/1196/8d555d30-c392-3330-82c4-f7a4182ba6b8.bmp)

Sync Scan Locations List 相关结构图

 

 

![](http://dl.iteye.com/upload/attachment/0068/1198/bbf42b74-7013-3c39-89c3-464546332a18.bmp)

Backend 结构数组图