pg源码阅读四

本文深入探讨了 PostgreSQL 中的内存管理,尤其是内存上下文的概念。内存上下文分为不同类型,如默认、顶层和错误上下文等。MemoryContextSwitchTo() 函数用于在上下文之间切换。在系统初始化时,通过 MemoryContextInit 创建和初始化内存上下文,如 TopMemoryContext 和 ErrorContext。AllocSetContextCreate 和 MemoryContextCreate 负责上下文的创建,维护了一个由 AllocBlock 指针链接的内存块链表,以高效地分配和管理内存。

pg中的内存是内存上下文来管理的,有不同类型的上下文,不同的上下文是通过MemoryContextMethods指针操作的,下面的是顶层的上下文
MemoryContext CurrentMemoryContext = NULL;默认的上下文
MemoryContext TopMemoryContext = NULL;
MemoryContext ErrorContext = NULL;
MemoryContext PostmasterContext = NULL;
MemoryContext CacheMemoryContext = NULL;
MemoryContext MessageContext = NULL;
MemoryContext TopTransactionContext = NULL;
MemoryContext CurTransactionContext = NULL;

使用The MemoryContextSwitchTo()
operation selects a new current context (and returns the previous context,
so that the caller can restore the previous context before exiting).
能切换到新的上下文中并保留之前的上下文
在postmaster初始化的时候调用MemoryContextInit
* In normal multi-backend operation, this is called once during
* postmaster startup, and not at all by individual backend startup

在入口main函数中使用MemoryContextInit();来创建内存上下文,这个方法会初始化TopMemoryContext and ErrorContext

在MemoryContextInit中使用下马的方法分配上下文
TopMemoryContext =

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值