SPI_SAVEPLAN(3)

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

πŸ’… SPI_SAVEPLAN πŸ’…

okay bestie, so this is a page from the PostgreSQL 17.11 Documentation set, and it's literally all about SPI_saveplan β€” a function that saves a prepared statement so it doesn't ghost you when the transaction ends. no cap, it's lowkey iconic for anyone writing C functions that need to reuse a plan across calls. 🫢


✨ NAME ✨

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


πŸŽ€ SYNOPSIS πŸŽ€

SPIPlanPtr SPI_saveplan(SPIPlanPtr plan)

🌸 DESCRIPTION 🌸

so SPI_saveplan copies a statement you pass in (one that was prepared by SPI_prepare, mmkay) into memory that will not be freed by SPI_finish nor by the transaction manager β€” and then it hands you back a pointer to that copied statement. the way this works is it literally gives you the ability to reuse prepared statements in the subsequent invocations of your C function in the current session. main character energy fr fr. ✨


πŸ¦‹ ARGUMENTS πŸ¦‹

  • SPIPlanPtr plan β€” the prepared statement that's getting saved, bestie. that's the one. πŸŽ€

πŸ’œ RETURN VALUE πŸ’œ

you get back a pointer to the copied statement πŸ’– … or NULL if it didn't work out 😭. on error, SPI_result gets set like this:

  • SPI_ERROR_ARGUMENT β€” happens if plan is NULL or invalid. yeah. not great. πŸ’€
  • SPI_ERROR_UNCONNECTED β€” happens if it's called from an unconnected C function. πŸ™ˆ

⭐ NOTES ⭐

the originally passed-in statement is not freed, so you might want to do SPI_freeplan on it to avoid leaking memory until SPI_finish. don't forget, okay? your memory will thank you. 🧚

in most cases, SPI_keepplan is preferred to this function, since it accomplishes largely the same result without needing to physically copy the prepared statement's data structures. so like… consider it? πŸ‘€

and that's the whole vibe check, bestie πŸ’… this page came to you courtesy of PostgreSQL 17.11, dated 2026 β€” go forth and save those plans responsibly πŸ“πŸ‘‘πŸŒˆ

πŸ“¦ where this page comes from