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 by SPI_prepare can be used only in the current invocation of the C function, since SPI_finish frees the memory allocated for such a statement. but bestie, you can save it for longer using the functions SPI_keepplan or SPI_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_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 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_PLAN or CURSOR_OPT_CUSTOM_PLAN flag to SPI_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. πŸ’…βœ¨πŸŽ€πŸ¦‹πŸŒΈ