PostgreSQL启动过程中的那些事七:初始化共享内存和信号十九:shmem中初始化BTree相关结构 ...

       这一节 pg 初始化 BTree 用到的相关结构,通过 BTreeShmemInit 例程实现 。主要是初始化了一个 BTVacInfo 结构,并使用了面向过程C 语言编程的一个技巧,把这个结构中的固定长度数组 BTOneVacInfo vacuums[1] 扩充成 MaxBackends 个(根据默认值 100GUC 参数的设置得到)的 BTOneVacInfo 结构的数组,以供 BTree 使用。

pg 中相关 BTree 部分实现了 LehmanYao 的高并发 B-tree 管理算法( P. Lehman and S. Yao,Efficient Locking for Concurrent Operations on B-Trees, ACM Transactions on Database Systems, Vol 6, No. 4, December 1981, pp 650-670 )。还用了 LaninShasha 论文里所写的删除逻辑的简化版本( V. Lanin and D. Shasha, A Symmetric Concurrent B-Tree Algorithm, Proceedings of 1986 Fall Joint Computer Conference, pp 380-389 )。。

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


初始化 BTree 相关结构方法调用流程图

 

2 初始化 xlog 相关结构

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

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

typedef struct BTOneVacInfo

{

    LockRelId   relid ;        /* global identifier of an index */

    BTCycleId   cycleid ;      /* cycle ID for its active VACUUM */

} BTOneVacInfo ;

 

typedef struct BTVacInfo

{

    BTCycleId   cycle_ctr ;    /* cycle ID most recently assigned */

    int         num_vacuums /* number of currently active VACUUMs */

    int         max_vacuums /* allocated length of vacuums[] array */

    BTOneVacInfo vacuums [1]; /* VARIABLE LENGTH ARRAY */

} BTVacInfo ;

 

static BTVacInfo *btvacinfo;

 

初始化完 BTree Vacuum State 相关结构 的共享内存结构图

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

 

BTree Vacuum State 相关结构图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值