SPI_SAVEPLAN(3)
πΎ SPI_SAVEPLAN(3) β like, the save button for prepared statements β¨
okay bestie, so this is SPI_SAVEPLAN(3) from the PostgreSQL 14.24 docs, and honestly? it's a little memory-management baddie and i'm obsessed π
β¨ SYNOPSIS β¨
SPIPlanPtr SPI_saveplan(SPIPlanPtr plan)
π DESCRIPTION π
so here's the tea β β SPI_saveplan copies a statement that got passed in (one that was prepared by SPI_prepare, iykyk) into memory that is not gonna get freed by SPI_finish, and not by the transaction manager either. then it hands you back a pointer to the copied statement. π«Ά
the whole point? it's giving reuse. it gives you the ability to reuse prepared statements in the subsequent invocations of your C function in the current session. main character energy for your prepared statements, fr fr π
πΈ ARGUMENTS πΈ
SPIPlanPtr planβ the prepared statement to be saved. that's it, that's the whole argument, she's lowkey simple π
β RETURN VALUE β
you get a pointer to the copied statement π« β or NULL if it didn't work out π. on error, SPI_result is set thus:
SPI_ERROR_ARGUMENTβ ifplanisNULLor invalid πSPI_ERROR_UNCONNECTEDβ if called from an unconnected C function π
π¦ NOTES π¦
heads up: the originally passed-in statement is not freed, so you might wish to do SPI_freeplan on it to avoid leaking memory until SPI_finish. not me letting memory leak... don't do that bestie π«Ά
also, 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... same vibe, less work? iconic, honestly π
fr fr: if you don't need the physical copy, SPI_keepplan is the move π
π and that's the page!! brought to you by PostgreSQL 14.24, dated 2026 β slay, stay hydrated, and don't leak your plans πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14-docs 14.24-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms