SPI_PREPARE(3)
SPI_PREPARE(3) π
okay so this is the PostgreSQL 13.23 Documentation page for SPI_PREPARE(3), and it's lowkey iconic: it's all about preparing a statement without executing it yet. bestie, we are not running the command here β we're just getting it ready β¨
π NAME π
SPI_prepare β prepare a statement, without executing it yet. it's giving "prep now, execute later" main character energy π
β¨ SYNOPSIS β¨
SPIPlanPtr SPI_prepare(const char * command, int nargs, Oid * argtypes)
π¦ DESCRIPTION π¦
SPI_prepare creates and returns a prepared statement for the specified command, but it does not execute the command. The prepared statement can later be executed repeatedly using SPI_execute_plan. periodt.
When the same or a similar command is to be executed repeatedly, it is 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 is found that generating a custom plan for each execution is not helpful. highkey that's the whole point, and I'm obsessed π
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. iconic behavior, honestly π
The statement returned by SPI_prepare can be used only in the current invocation of the C function, since SPI_finish frees memory allocated for such a statement. But the statement can be saved for longer using the functions SPI_keepplan or SPI_saveplan. important: that "only in the current invocation" part is not a suggestion, bestie π
π ARGUMENTS π
const char * commandβ command string. literally the command string, no cap.int nargsβ number of input parameters ($1,$2, etc.). count those parameters, bestie.Oid * argtypesβ pointer to an array containing the OIDs of the data types of the parameters. yes, OIDs of the data types β not a vibe check π«Ά
π₯ RETURN VALUE π₯
SPI_prepare hands you back a non-null pointer to an SPIPlan, which is an opaque struct representing a prepared statement. 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 is set to SPI_ERROR_ARGUMENT if command is NULL, or if nargs is less than 0, or if nargs is greater than 0 and argtypes is NULL. so if you see NULL, that SPI_ERROR_ARGUMENT case is your argument vibe check failing π
πΈ 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. no parameters = generic plan from the first use, and it keeps using it β¨ - If there are parameters, the first few uses of
SPI_execute_planwill generate custom plans that are specific to the supplied parameter values. After enough uses of the same prepared statement,SPI_execute_planwill build a generic plan, and if that is not too much more expensive than the custom plans, it will start using the generic plan instead of re-planning each time. that's the plan-switching glow-up, iykyk π₯ - If this default behavior is unsuitable, you can alter it by passing the
CURSOR_OPT_GENERIC_PLANorCURSOR_OPT_CUSTOM_PLANflag toSPI_prepare_cursor, to force use of generic or custom plans respectively. flag girl summer π - 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_pathchanges from one use to the next, the statement will be re-parsed using the newsearch_path. (This latter behavior is new as of PostgreSQL 9.3.) SeePREPARE(7)for more information about the behavior of prepared statements. so yes, it can still re-parse and re-plan β the cache is not immune to DDL orsearch_pathchanges, fr fr π«Ά SPI_prepareshould only be called from a connected C function. like, connected C function only, okay? that's the vibe check πSPIPlanPtris declared as a pointer to an opaque struct type inspi.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. > bestie, do not poke around inside the opaque struct. it's opaque for a reason, and future PostgreSQL revisions will humble your code π- The name
SPIPlanPtris somewhat historical, since the data structure no longer necessarily contains an execution plan. historical name, current lore πΈ
π that's the whole page, bestie β source/version: PostgreSQL 13.23, date: 2025, footer page: SPI_PREPARE(3). mwah! π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13-docs 13.23-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