Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
44.3. Memory Management
Prev UpChapter 44. Server Programming InterfaceHome Next

44.3. Memory Management

SPI_palloc — allocate memory in the upper executor context
SPI_repalloc — reallocate memory in the upper executor context
SPI_pfree — free memory in the upper executor context
SPI_copytuple — make a copy of a row in the upper executor context
SPI_returntuple — prepare to return a tuple as a Datum
SPI_modifytuple — create a row by replacing selected fields of a given row
SPI_freetuple — free a row allocated in the upper executor context
SPI_freetuptable — free a row set created bySPI_execute or a similar function
SPI_freeplan — free a previously saved prepared statement

Postgres Pro allocates memory withinmemory 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 thecurrent context.

SPI_connect creates a new memory context and makes it current.SPI_finish restores the previous current memory context and destroys the context created bySPI_connect. These actions ensure that transient memory allocations made inside your procedure are reclaimed at procedure exit, avoiding memory leakage.

However, if your procedure needs to return an object in allocated memory (such as a value of a pass-by-reference data type), you cannot allocate that memory usingpalloc, at least not while you are connected to SPI. If you try, the object will be deallocated bySPI_finish, and your procedure will not work reliably. To solve this problem, useSPI_palloc to allocate memory for your return object.SPI_palloc allocates memory in theupper executor context, that is, the memory context that was current whenSPI_connect was called, which is precisely the right context for a value returned from your procedure. Several of the other utility procedures described in this section also return objects created in the upper executor context.

WhenSPI_connect is called, the private context of the procedure, which is created bySPI_connect, is made the current context. All allocations made bypalloc,repalloc, or SPI utility functions (except as described in this section) are made in this context. When a procedure disconnects from the SPI manager (viaSPI_finish) the current context is restored to the upper executor context, and all allocations made in the procedure memory context are freed and cannot be used any more.


Prev Up Next
SPI_getnspname Home SPI_palloc
epubpdf
Go to Postgres Pro Standard 10
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp