SPI_SAVEPLAN(3)
πΎ 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β ifplanisNULLor 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
- 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