SPI_EXEC..._EXTENDED(3)

πŸ’– SPI_EXECUTE_PLAN_EXTENDED πŸ’–

okay bestie, gather round, because we're about to talk about SPI_execute_plan_extended from the PostgreSQL 18.6 docs, and honestly? it's giving ✨main character energy✨ for anyone writing C extensions. πŸ¦‹


✨ NAME ✨

SPI_execute_plan_extended β€” execute a statement prepared by SPI_prepare πŸ’…


✨ SYNOPSIS ✨

int SPI_execute_plan_extended(SPIPlanPtr plan,
                              const SPIExecuteOptions * options)

✨ DESCRIPTION ✨

so SPI_execute_plan_extended runs a statement that was prepared by SPI_prepare or one of its siblings. 🌸

It's basically SPI_execute_plan's cooler twin β€” same energy, but the info about the parameter values you're passing to the query gets presented differently, and you also get extra execution-controlling options to play with. πŸŽ€

Your query parameter values are represented by a ParamListInfo struct, which is lowkey so convenient when you've already got values chillin' in that exact format. And dynamic parameter sets? Also a thing! Via hook functions specified in ParamListInfo. πŸͺ„

AND, the best part, ngl: instead of always accumulating the result tuples into a SPI_tuptable structure, you can hand tuples off to a caller-supplied DestReceiver object as the executor generates them. This is iconic for queries that might generate a LOT of tuples, because the data gets processed on-the-fly instead of being accumulated in memory. πŸ§ πŸ’–


✨ ARGUMENTS ✨

  • SPIPlanPtr plan β€” the prepared statement (returned by SPI_prepare) 🫢
  • const SPIExecuteOptions * options β€” struct containing optional arguments
⚠️ okay important one bestie: callers should always zero out the entire options struct, then fill whichever fields they want to set. This ensures forward compatibility of your code, since any fields added to the struct in future will be defined to behave backwards-compatibly if they are zero. no cap. πŸ’€

The currently available options fields are:

  • ParamListInfo params β€” data structure containing query parameter types and values; NULL if none
  • bool read_only β€” true for read-only execution
  • bool allow_nonatomic β€” true allows non-atomic execution of CALL and DO statements (but this field is ignored unless the SPI_OPT_NONATOMIC flag was passed to SPI_connect_ext) 🚨
  • bool must_return_tuples β€” if true, raise error if the query is not of a kind that returns tuples (this does not forbid the case where it happens to return zero tuples)
  • uint64 tcount β€” maximum number of rows to return, or 0 for no limit
  • DestReceiver * dest β€” DestReceiver object that will receive any tuples emitted by the query; if NULL, result tuples are accumulated into a SPI_tuptable structure, as in SPI_execute_plan
  • ResourceOwner owner β€” The resource owner that will hold a reference count on the plan while it is executed. If NULL, CurrentResourceOwner is used. Ignored for non-saved plans, as SPI does not acquire reference counts on those.

✨ RETURN VALUE ✨

The return value is the same as for SPI_execute_plan. simple as that. 😌

When options->dest is NULL, SPI_processed and SPI_tuptable are set as in SPI_execute_plan. 🌷

When options->dest is not NULL, SPI_processed is set to zero and SPI_tuptable is set to NULL. So if a tuple count is required, the caller's DestReceiver object must calculate it. πŸ‘‘ (not me making you do your own math tho 😭)


and that's the whole page, bestie!! πŸ’œ this was PostgreSQL 18.6 documentation, dated 2026, and you absolutely ate that read. go forth and prepare those statements. slay. πŸ’…βœ¨πŸŽ€