PostgreSQL启动过程中的那些事七:初始化共享内存和信号二十:shmem中初始化堆同步扫描、pg子进程相关结构

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

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

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

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

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

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

        话说main()->…->PostmasterMain()->…->reset_shared() ->CreateSharedMemoryAndSemaphores()>…->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()->…->PostmasterMain()->…->reset_shared() ->CreateSharedMemoryAndSemaphores() ->…-> ShmemBackendArrayAllocation(),调用ShmemAlloc()在共享内存上分配2 *MaxBackendsBackend结构数组见下面“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 structin shared memory */

static ss_scan_locations_t *scan_locations;

 

/* prototypes for internalfunctions */

static BlockNumber ss_search(RelFileNode relfilenode,

         BlockNumber location, bool set);

typedef struct bkend

{

    pid_t      pid;          /* process id of backend */

    long       cancel_key;       /* cancelkey for cancels for this backend */

    int        child_slot;       /*PMChildSlot for this backend, if any */

    bool       is_autovacuum;    /* is itan autovacuum process? */

    bool       dead_end;     /* is itgoing to send an error and quit? */

    Dlelem     elem;         /* list link in BackendList */

} Backend;

static Backend *ShmemBackendArray;

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

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

Sync Scan Locations List相关结构图

Backend结构数组图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值