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 =

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

被折叠的 条评论
为什么被折叠?



