SPI_PREPARE(3)
π SPI_PREPARE(3) β the PostgreSQL 14.24 glow-up
okay so bestie, welcome to the SPI_prepare page β¨ this is the PostgreSQL 14.24 documentation, and today we're learning how to prep a statement without, like, actually running it yet. iconic behavior honestly π
πΈ NAME πΈ
SPI_prepare β prepare a statement, without executing it yet π«Ά
β¨ SYNOPSIS β¨
SPIPlanPtr SPI_prepare(const char * command, int nargs, Oid * argtypes)
that's the whole vibe. one function, three args, slay π
π¦ DESCRIPTION π¦
SPI_prepare creates and returns a prepared statement for the specified command β but it does not execute the command. nope, not even a little. the prepared statement can later be executed repeatedly using SPI_execute_plan. it's giving "meal prep" energy fr fr π
When the same or a similar command is gonna be executed repeatedly, it's generally advantageous to perform parse analysis only once, and it might furthermore be advantageous to re-use an execution plan for the command. SPI_prepare converts a command string into a prepared statement that encapsulates the results of parse analysis. The prepared statement also provides a place for caching an execution plan if it's found that generating a custom plan for each execution is not helpful. lowkey efficient, highkey smart π
A prepared command can be generalized by writing parameters ($1, $2, etc.) in place of what would be constants in a normal command. The actual values of the parameters are then specified when SPI_execute_plan is called. This allows the prepared command to be used over a wider range of situations than would be possible without parameters. main character flexibility π
β οΈ The statement returned bySPI_preparecan be used only in the current invocation of the C function, sinceSPI_finishfrees memory allocated for such a statement. But the statement can be saved for longer using the functionsSPI_keepplanorSPI_saveplan. so like... don't get attached unless you save it, bestie π
π ARGUMENTS π
const char * commandβ command string πint nargsβ number of input parameters ($1,$2, etc.) π’Oid * argtypesβ pointer to an array containing the OIDs of the data types of the parameters π·οΈ
π RETURN VALUE π
SPI_prepare returns a non-null pointer to an SPIPlan, which is an opaque struct representing a prepared statement. obsessed with an opaque struct ngl π«Ά
On error, NULL will be returned, and SPI_result will be set to one of the same error codes used by SPI_execute, except that it's set to SPI_ERROR_ARGUMENT if:
commandisNULL, ornargsis less than 0, ornargsis greater than 0 andargtypesisNULL
so watch those three, k? π
π₯ NOTES π₯
If no parameters are defined, a generic plan will be created at the first use of SPI_execute_plan, and used for all subsequent executions as well. set it and forget it π
If there are parameters, the first few uses of SPI_execute_plan will generate custom plans that are specific to the supplied parameter values. After enough uses of the same prepared statement, SPI_execute_plan will build a generic plan, and if that's not too much more expensive than the custom plans, it will start using the generic plan instead of re-planning each time. it literally grows up and settles down πβ¨
If this default behavior is unsuitable, you can alter it by passing the CURSOR_OPT_GENERIC_PLAN or CURSOR_OPT_CUSTOM_PLAN flag to SPI_prepare_cursor, to force use of generic or custom plans respectively. you have options, bestie π
Although the main point of a prepared statement is to avoid repeated parse analysis and planning of the statement, PostgreSQL will force re-analysis and re-planning of the statement before using it whenever database objects used in the statement have undergone definitional (DDL) changes since the previous use of the prepared statement. Also, if the value of search_path changes from one use to the next, the statement will be re-parsed using the new search_path. (This latter behavior is new as of PostgreSQL 9.3.) See PREPARE(7) for more information about the behavior of prepared statements. it's called self-care, sweaty π
π¨ This function should only be called from a connected C function. no exceptions, periodt.
SPIPlanPtr is declared as a pointer to an opaque struct type in spi.h. It is unwise to try to access its contents directly, as that makes your code much more likely to break in future revisions of PostgreSQL. don't peek behind the curtain, it's giving chaos π
The name SPIPlanPtr is somewhat historical, since the data structure no longer necessarily contains an execution plan. a little name from the past living its best life π¦
π see also: PREPARE(7) β go read it, iykyk
and that's the whole page, bestie!! πΈ this was PostgreSQL 14.24 docs, dated 2026 β mwah, take care of your prepared statements and they'll take care of you ππ«Άπ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14-docs 14.24-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms