SPI_PREPARE(3)
π SPI_PREPARE(3) β like, the PostgreSQL 18.6 era π
okay bestie, sit down, hydrate, because we're talking about SPI_prepare β it's a thing that prepares a statement without actually executing it yet. yes really. it's giving "getting ready but not going anywhere" energy and honestly? relatable.
β¨ SYNOPSIS β¨
SPIPlanPtr SPI_prepare(const char * command, int nargs, Oid * argtypes)
πΈ DESCRIPTION πΈ
So SPI_prepare creates and returns a prepared statement for the command you hand it, but β and this is the whole point β it does not execute the command. nope. it just... makes it. the prepared statement can later be executed repeatedly using SPI_execute_plan. it's a vibe-setter, not a performer. π
Now, when the same or a similar command is gonna be executed over and over, it's generally advantageous to do the 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. and the prepared statement also provides a place for caching an execution plan β that's useful if it turns out that generating a custom plan for each execution is not helpful. so it's like a little plan-cubby. a plan locker. a plan wardrobe if you will. β¨
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. so the prepared command can be used over a wider range of situations than would be possible without parameters. glow up achieved. π¦
β οΈ important!! The statement returned bySPI_preparecan be used only in the current invocation of the C function, sinceSPI_finishfrees the memory allocated for such a statement. but bestie, you can save it for longer using the functionsSPI_keepplanorSPI_saveplan. keep your faves, just do it on purpose. π
π ARGUMENTS π
const char * commandβ the command string. that's the whole thing. it's the string. it's the command. iconic.int nargsβ number of input parameters ($1,$2, etc.). count 'em.Oid * argtypesβ pointer to an array containing the OIDs of the data types of the parameters. yes, all of them, in an array, pointed at. she's organized like that. π
π₯ RETURN VALUE π₯
SPI_prepare returns a non-null pointer to an SPIPlan, which is an opaque struct representing a prepared statement. mystery box but make it legit. π
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 like, three very specific ways to annoy it. noted. π
π NOTES π
okay this part is the lore. buckle up. π§
- 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. - 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. - 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. you have options. you have flags. slay. π©
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 keeps receipts. π
π«Ά This function should only be called from a connected C function. like, connected. do not try this while disconnected, it's not gonna go how you want.
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. hands off the opaque. π
also, the name SPIPlanPtr is somewhat historical, since the data structure no longer necessarily contains an execution plan. it's a legacy name, she kept the nickname. π
π and that's the page π
written against PostgreSQL 18.6, dated 2026, filed under SPI_PREPARE(3) β go forth and prepare responsibly, bestie. periodt. π
β¨ππ¦πΈ
π¦ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 Β· PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 Β· PostgreSQL
- arch rolling: postgresql 18.6-4 Β· PostgreSQL
- fedora 43: postgresql-server-devel 18.6-1.fc43 Β· PostgreSQL
- fedora 43: postgresql-server-devel 18.6-1.fc43 Β· PostgreSQL
- fedora 44: postgresql-server-devel 18.6-1.fc44 Β· PostgreSQL
- fedora 44: postgresql-server-devel 18.6-1.fc44 Β· PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-doc-18 18.6-0ubuntu0.26.04.1 Β· PostgreSQL
- ubuntu stonking: postgresql-doc-18 18.6-3build2 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms