1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>47.3. Memory Management</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="spi-spi-result-code-string.html" title="SPI_result_code_string" /><link rel="next" href="spi-spi-palloc.html" title="SPI_palloc" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">47.3. Memory Management</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="spi-spi-result-code-string.html" title="SPI_result_code_string">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="spi.html" title="Chapter 47. Server Programming Interface">Up</a></td><th width="60%" align="center">Chapter 47. Server Programming Interface</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 11beta1 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="spi-spi-palloc.html" title="SPI_palloc">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="SPI-MEMORY"><div class="titlepage"><div><div><h2 class="title" style="clear: both">47.3. Memory Management</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="refentrytitle"><a href="spi-spi-palloc.html">SPI_palloc</a></span><span class="refpurpose"> — allocate memory in the upper executor context</span></dt><dt><span class="refentrytitle"><a href="spi-realloc.html">SPI_repalloc</a></span><span class="refpurpose"> — reallocate memory in the upper executor context</span></dt><dt><span class="refentrytitle"><a href="spi-spi-pfree.html">SPI_pfree</a></span><span class="refpurpose"> — free memory in the upper executor context</span></dt><dt><span class="refentrytitle"><a href="spi-spi-copytuple.html">SPI_copytuple</a></span><span class="refpurpose"> — make a copy of a row in the upper executor context</span></dt><dt><span class="refentrytitle"><a href="spi-spi-returntuple.html">SPI_returntuple</a></span><span class="refpurpose"> — prepare to return a tuple as a Datum</span></dt><dt><span class="refentrytitle"><a href="spi-spi-modifytuple.html">SPI_modifytuple</a></span><span class="refpurpose"> — create a row by replacing selected fields of a given row</span></dt><dt><span class="refentrytitle"><a href="spi-spi-freetuple.html">SPI_freetuple</a></span><span class="refpurpose"> — free a row allocated in the upper executor context</span></dt><dt><span class="refentrytitle"><a href="spi-spi-freetupletable.html">SPI_freetuptable</a></span><span class="refpurpose"> — free a row set created by <code class="function">SPI_execute</code> or a similar
3
function</span></dt><dt><span class="refentrytitle"><a href="spi-spi-freeplan.html">SPI_freeplan</a></span><span class="refpurpose"> — free a previously saved prepared statement</span></dt></dl></div><p>
4
<a id="id-1.8.12.11.2.1" class="indexterm"></a>
5
<span class="productname">PostgreSQL</span> allocates memory within
6
<em class="firstterm">memory contexts</em>, which provide a convenient method of
7
managing allocations made in many different places that need to
8
live for differing amounts of time. Destroying a context releases
9
all the memory that was allocated in it. Thus, it is not necessary
10
to keep track of individual objects to avoid memory leaks; instead
11
only a relatively small number of contexts have to be managed.
12
<code class="function">palloc</code> and related functions allocate memory
13
from the <span class="quote">“<span class="quote">current</span>”</span> context.
15
<code class="function">SPI_connect</code> creates a new memory context and
16
makes it current. <code class="function">SPI_finish</code> restores the
17
previous current memory context and destroys the context created by
18
<code class="function">SPI_connect</code>. These actions ensure that
19
transient memory allocations made inside your procedure are
20
reclaimed at procedure exit, avoiding memory leakage.
22
However, if your procedure needs to return an object in allocated
23
memory (such as a value of a pass-by-reference data type), you
24
cannot allocate that memory using <code class="function">palloc</code>, at
25
least not while you are connected to SPI. If you try, the object
26
will be deallocated by <code class="function">SPI_finish</code>, and your
27
procedure will not work reliably. To solve this problem, use
28
<code class="function">SPI_palloc</code> to allocate memory for your return
29
object. <code class="function">SPI_palloc</code> allocates memory in the
30
<span class="quote">“<span class="quote">upper executor context</span>”</span>, that is, the memory context
31
that was current when <code class="function">SPI_connect</code> was called,
32
which is precisely the right context for a value returned from your
33
procedure. Several of the other utility procedures described in
34
this section also return objects created in the upper executor context.
36
When <code class="function">SPI_connect</code> is called, the private
37
context of the procedure, which is created by
38
<code class="function">SPI_connect</code>, is made the current context. All
39
allocations made by <code class="function">palloc</code>,
40
<code class="function">repalloc</code>, or SPI utility functions (except as
41
described in this section) are made in this context. When a
42
procedure disconnects from the SPI manager (via
43
<code class="function">SPI_finish</code>) the current context is restored to
44
the upper executor context, and all allocations made in the
45
procedure memory context are freed and cannot be used any more.
46
</p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="spi-spi-result-code-string.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="spi.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="spi-spi-palloc.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">SPI_result_code_string </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> SPI_palloc</td></tr></table></div></body></html>
b'\\ No newline at end of file'