SPI_CURSOR_OPEN(3)
β¨ SPI_CURSOR_OPEN β¨
okay bestie, gather round, because we're about to talk about SPI_cursor_open β and no, it's not a PostgreSQL 18.6 Documentation thing you can skip, this is the one that sets up a cursor using a statement created with SPI_prepare. π
πΈ SYNOPSIS πΈ
Portal SPI_cursor_open(const char * name, SPIPlanPtr plan,
Datum * values, const char * nulls,
bool read_only)
π DESCRIPTION π
so SPI_cursor_open sets up a cursor (which internally? a portal, iykyk) that will execute a statement you already prepared with SPI_prepare. and the parameters? they have the same meanings as the corresponding parameters to SPI_execute_plan. like, same energy, same rules, no cap. π«Ά
now why would you use a cursor instead of just executing the statement directly?? two benefits, and both of them are lowkey iconic:
- the result rows can be retrieved a few at a time β which means you avoid memory overrun for queries that return a lot of rows. your RAM stays unbothered. π
- a portal can outlive the current C function β it can, in fact, live all the way to the end of the current transaction. main character longevity fr fr. and returning the portal name to the C function's caller gives you a way of returning a row set as a result. π
also, important: the passed-in parameter data will be copied into the cursor's portal, so you can free it while the cursor still exists. she doesn't need your original array anymore, she has her own. β¨
π¦ ARGUMENTS π¦
const char * nameβ the name for the portal, orNULLto let the system select a name. let the system cook. πSPIPlanPtr planβ the prepared statement (the one returned bySPI_prepare). you know her.Datum * valuesβ an array of actual parameter values. must have the same length as the statement's number of arguments. no short arrays, no long arrays, we match. βconst char * nullsβ an array describing which parameters are null. must have the same length as the statement's number of arguments.
if nulls is NULL then SPI_cursor_open assumes that no parameters are null. otherwise, each entry of the nulls array should be ' ' if the corresponding parameter value is non-null, or 'n' if the corresponding parameter value is null. (in the latter case, the actual value in the corresponding values entry doesn't matter β she's not even looking at it. π) and note: nulls is not a text string, just an array β it does not need a '\0' terminator. this is not a string bestie, do not terminate her. π
bool read_onlyβtruefor read-only execution.
π RETURN VALUE π
a pointer to the portal containing the cursor. π
β οΈ heads up though: there is no error return convention β any error will be reported viaelog. so like, don't go checking for aNULLreturn and thinking you're safe, because that's not the vibe here. errors come throughelog, periodt.
and that's the whole page, bestie!! every option, every argument, every little '\0' caveat β all accounted for. stay slaying those portals π§
π this has been PostgreSQL 18.6 documentation, dated 2026, signing off β mwah π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql18-docs 18.6-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms