SPI_CURSOR_OPEN(3)
β¨ SPI_CURSOR_OPEN β¨
okay so bestie, this is a PostgreSQL 14.24 page from the SPI_CURSOR_OPEN(3) manual β and it's literally the glow-up you need when you want to run a prepared statement but likeβ¦ slowly, a few rows at a time π
π NAME π
SPI_cursor_open β set up a cursor using a statement created with SPI_prepare. it's giving "prep now, browse later" energy, no cap π¦
πΈ 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 (internally, a portal β that's the glow-up name for it) that will execute a statement prepared by SPI_prepare. the parameters have the same meanings as the corresponding parameters to SPI_execute_plan. iykyk π«Ά
using a cursor instead of executing the statement directly has two benefits, and honestly? both of them ate:
- the result rows can be retrieved a few at a time, avoiding memory overrun for queries that return many rows. main character energy for memory safety fr fr π₯
- a portal can outlive the current C function (it can, in fact, live to the end of the current transaction). returning the portal name to the C function's caller provides a way of returning a row set as result. iconic behaviour tbh π
the passed-in parameter data will be copied into the cursor's portal, so it can be freed while the cursor still exists. lowkey so convenient, you literally don't have to babysit it π§
β ARGUMENTS β
const char * namename for portal, orNULLto let the system select a name. let the system pick, it's got impeccable taste πSPIPlanPtr planprepared statement (returned bySPI_prepare). this is the thing you already prepped, bestie πDatum * valuesAn array of actual parameter values. Must have same length as the statement's number of arguments. no short arrays, no extra vibes β match the count exactly πΈconst char * nullsAn array describing which parameters are null. Must have same length as the statement's number of arguments. (yes, same rule again π«Ά)
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 β it's literally just background noise π) Note that nulls is not a text string, just an array: it does not need a '\0' terminator. please don't add one, it's not that kind of party π
bool read_onlytruefor read-only execution. setfalseonly if you're feeling brave π¦
π RETURN VALUE π
Pointer to portal containing the cursor. π
β οΈ heads up bestie: there is no error return convention; any error will be reported viaelog. so likeβ¦ don't sit there waiting for aNULLto come save you, okay? π
π SEE YOU LATER π
this was SPI_CURSOR_OPEN(3) from the PostgreSQL 14.24 documentation, dated 2026. love that for us. slay, and go open some cursors responsibly πβ¨π
π¦ 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