SPI_SAVEPLAN(3)

πŸ“¦ version 18.6, in opensuse-leap 16.0

πŸ’Ύ SPI_SAVEPLAN(3) πŸ’Ύ

okay bestie, welcome to the PostgreSQL 18.6 Documentation page for SPI_saveplan β€” the little function that makes your prepared statement immortal (well, immortal-ish, we'll get to that) 🫢


✨ NAME ✨

SPI_saveplan β€” save a prepared statement πŸ’…


🌸 SYNOPSIS 🌸

SPIPlanPtr SPI_saveplan(SPIPlanPtr plan)

like that's it. one argument. she's lowkey elegant πŸ’œ


πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea β˜• SPI_saveplan copies a statement that you passed in (and that was prepared by SPI_prepare) into memory that will NOT be freed by SPI_finish, and also NOT by the transaction manager. then it hands you back a pointer to that copied statement ✨

why do you care?? because this gives you the ability to reuse prepared statements in the subsequent invocations of your C function, all within the current session. main character energy fr fr πŸ‘‘


πŸŽ€ ARGUMENTS πŸŽ€

  • SPIPlanPtr plan β€” the prepared statement that's getting saved πŸ₯Ί she's going into the vault

πŸ’– RETURN VALUE πŸ’–

a pointer to the copied statement 🫢 or NULL if it didn't work out 😭

and if there's an error, SPI_result gets set like this:

  • SPI_ERROR_ARGUMENT β€” if plan is NULL or invalid πŸ™ˆ
  • SPI_ERROR_UNCONNECTED β€” if you called this from an unconnected C function πŸ’€

⭐ NOTES ⭐

the statement you originally passed in does not get freed, so you might wanna run SPI_freeplan on it to avoid leaking memory until SPI_finish πŸ‘€ like, please don't leak, bestie.

also, real talk: in most cases, SPI_keepplan is preferred over this function, since it accomplishes largely the same result without needing to physically copy the prepared statement's data structures. so like... SPI_keepplan is the it-girl here, no cap πŸ’…

not me telling you the function you're reading about is usually not the one you want 😭 but the docs said it, not me!

that's the whole page, you ate that πŸ“ go forth and prepare those statements responsibly, queen πŸ‘‘

with love from the PostgreSQL 18.6 docs, dated 2026, page SPI_SAVEPLAN(3) β€” mwah πŸ’‹

πŸ“¦ where this page comes from