Postgresql 中文操作指南
47.3. Memory Management #
PostgreSQL 在 memory contexts 中分配内存,它提供了一种方便的方法来管理在不同位置进行的分配,而这些分配需要存在于不同的时间量中。销毁上下文会释放分配在其中的所有内存。因此,不必跟踪各个对象以避免内存泄漏;相反,只需要管理相对较少数量的上下文。 palloc 和相关函数从“当前”上下文中分配内存。
PostgreSQL allocates memory within memory contexts, which provide a convenient method of managing allocations made in many different places that need to live for differing amounts of time. Destroying a context releases all the memory that was allocated in it. Thus, it is not necessary to keep track of individual objects to avoid memory leaks; instead only a relatively small number of contexts have to be managed. palloc and related functions allocate memory from the “current” context.
SPI_connect 创建一个新的内存上下文并使其成为当前上下文。SPI_finish 恢复以前的当前内存上下文并销毁由 SPI_connect 创建的上下文。这些动作确保在 C 函数退出时回收在 C 函数内进行的瞬态内存分配,避免内存泄漏。
SPI_connect creates a new memory context and makes it current. SPI_finish restores the previous current memory context and destroys the context created by SPI_connect. These actions ensure that transient memory allocations made inside your C function are reclaimed at C function exit, avoiding memory leakage.
但是,如果你的 C 函数需要在分配的内存中返回一个对象(例如传递引用数据类型的值),你不能使用 palloc 分配该内存,至少在连接到 SPI 时不行。如果你尝试这样做,该对象将由 SPI_finish 销毁,并且 C 函数将无法可靠地工作。为了解决此问题,请使用 SPI_palloc 为你的返回对象分配内存。SPI_palloc 在“上层执行器上下文”中分配内存,即 SPI_connect 调用时的当前内存上下文,这正是 C 函数返回的值的正确上下文。本部分中描述的其他几个实用函数也会在上层执行器上下文中返回创建的对象。
However, if your C function needs to return an object in allocated memory (such as a value of a pass-by-reference data type), you cannot allocate that memory using palloc, at least not while you are connected to SPI. If you try, the object will be deallocated by SPI_finish, and your C function will not work reliably. To solve this problem, use SPI_palloc to allocate memory for your return object. SPI_palloc allocates memory in the “upper executor context”, that is, the memory context that was current when SPI_connect was called, which is precisely the right context for a value returned from your C function. Several of the other utility functions described in this section also return objects created in the upper executor context.
调用 SPI_connect 时,由 SPI_connect 创建的 C 函数的私有上下文会变为当前上下文。由 palloc、repalloc 或 SPI 实用函数(本节中所述的情况除外)进行的所有分配都是在此上下文中进行的。当一个 C 函数断开与 SPI 管理器的连接时(通过 SPI_finish),就会将当前上下文还原为上层执行器上下文,函数内存上下文中进行的所有分配都会被释放并不能再被使用了。
When SPI_connect is called, the private context of the C function, which is created by SPI_connect, is made the current context. All allocations made by palloc, repalloc, or SPI utility functions (except as described in this section) are made in this context. When a C function disconnects from the SPI manager (via SPI_finish) the current context is restored to the upper executor context, and all allocations made in the C function memory context are freed and cannot be used any more.