1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 >Memory Management</TITLE
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
15 TITLE="Server Programming Interface"
18 TITLE="SPI_getrelname"
19 HREF="spi-spi-getrelname.html"><LINK
22 HREF="spi-spi-palloc.html"><LINK
25 HREF="stylesheet.css"><META
27 CONTENT="2003-12-22T03:48:47"></HEAD
33 SUMMARY="Header navigation table"
43 >PostgreSQL 7.4.1 Documentation</TH
51 HREF="spi-spi-getrelname.html"
67 >Chapter 41. Server Programming Interface</TD
81 HREF="spi-spi-palloc.html"
96 >41.3. Memory Management</A
103 >Table of Contents</B
107 HREF="spi-spi-palloc.html"
109 > -- allocate memory in the upper executor context</DT
112 HREF="spi-realloc.html"
114 > -- reallocate memory in the upper executor context</DT
117 HREF="spi-spi-pfree.html"
119 > -- free memory in the upper executor context</DT
122 HREF="spi-spi-copytuple.html"
124 > -- make a copy of a row in the upper executor context</DT
127 HREF="spi-spi-copytupledesc.html"
128 >SPI_copytupledesc</A
129 > -- make a copy of a row descriptor in the upper executor context</DT
132 HREF="spi-spi-copytupleintoslot.html"
133 >SPI_copytupleintoslot</A
134 > -- make a copy of a row and descriptor in the upper executor context</DT
137 HREF="spi-spi-modifytuple.html"
139 > -- create a row by replacing selected fields of a given row</DT
142 HREF="spi-spi-freetuple.html"
144 > -- frees a row allocated in the upper executor context</DT
147 HREF="spi-spi-freetupletable.html"
149 > -- free a row set created by <CODE
152 > or a similar function</DT
155 HREF="spi-spi-freeplan.html"
157 > -- free a previously saved plan</DT
164 > allocates memory within
171 >, which provide a convenient method of
172 managing allocations made in many different places that need to
173 live for differing amounts of time. Destroying a context releases
174 all the memory that was allocated in it. Thus, it is not necessary
175 to keep track of individual objects to avoid memory leaks; instead
176 only a relatively small number of contexts have to be managed.
180 > and related functions allocate memory
190 > creates a new memory context and
191 makes it current. <CODE
195 previous current memory context and destroys the context created by
199 >. These actions ensure that
200 transient memory allocations made inside your procedure are
201 reclaimed at procedure exit, avoiding memory leakage.
204 > However, if your procedure needs to return an object in allocated
205 memory (such as a value of a pass-by-reference data type), you
206 cannot allocate that memory using <CODE
210 least not while you are connected to SPI. If you try, the object
211 will be deallocated by <CODE
215 procedure will not work reliably. To solve this problem, use
219 > to allocate memory for your return
223 > allocates memory in the
226 >"upper executor context"</SPAN
227 >, that is, the memory context
228 that was current when <CODE
232 which is precisely the right context for return a value from your
239 > is called while the procedure is
240 not connected to SPI, then it acts the same as a normal
244 >. Before a procedure connects to the
245 SPI manager, the current memory context is the upper executor
246 context, so all allocations made by the procedure via
250 > or by SPI utility functions are made in
257 > is called, the private
258 context of the procedure, which is created by
262 >, is made the current context. All
263 allocations made by <CODE
270 >, or SPI utility functions (except for
277 >SPI_copytupledesc</CODE
281 >SPI_copytupleintoslot</CODE
285 >SPI_modifytuple</CODE
290 >) are made in this context. When a
291 procedure disconnects from the SPI manager (via
295 >) the current context is restored to
296 the upper executor context, and all allocations made in the
297 procedure memory context are freed and cannot be used any more.
300 > All functions described in this section may be used by both
301 connected and unconnected procedures. In an unconnected procedure,
302 they act the same as the underlying ordinary server functions
314 SUMMARY="Footer navigation table"
325 HREF="spi-spi-getrelname.html"
343 HREF="spi-spi-palloc.html"