SPI_CURSOR_OPEN(3)
SPI_CURSOR_OPEN(3) π
okay so this is the PostgreSQL 13.23 Documentation page for SPI_cursor_open, manual section 3, and it's literally the glow-up for turning a prepared statement into a cursor. no cap. β¨
π NAME π
SPI_cursor_open β set 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 π
SPI_cursor_open sets up a cursor β and internally that cursor is a portal β that will execute a statement you already prepared with SPI_prepare. The parameters do the same jobs as the corresponding parameters to SPI_execute_plan, iykyk.
Using a cursor instead of just executing the statement directly gives you two big benefits, bestie:
- You can grab the result rows a few at a time, so queries that return a ton of rows don't cause a memory overrun. literally saving your RAM's life. π«Ά
- A portal can outlive the current C function β it can actually live until the end of the current transaction. So if you return the portal name to the C function's caller, you've got a way to hand back a row set as the result. main character portal behavior. π
The parameter data you pass in gets copied into the cursor's portal, so you can free it while the cursor still exists. no stress. πΈ
π ARGUMENTS π
const char * nameβ the name for the portal, orNULLto let the system pick a name for you. choose-your-own-adventure but the system can choose too.SPIPlanPtr planβ the prepared statement, returned bySPI_prepare. that's the whole point.Datum * valuesβ an array of actual parameter values. It must have the same length as the statement's number of arguments. match made in heaven, but like, length-wise. πconst char * nullsβ an array describing which parameters are null. It also must have the same length as the statement's number of arguments. IfnullsisNULL, thenSPI_cursor_openassumes that no parameters are null. Otherwise, each entry of thenullsarray should be' 'if the corresponding parameter value is non-null, or'n'if the corresponding parameter value is null. (In that null case, the actual value in the correspondingvaluesentry doesn't matter.) Also note:nullsis not a text string, it's just an array, so it does not need a'\0'terminator. bestie please don't add one. it's giving char array not string. πbool read_onlyβtruefor read-only execution.
π RETURN VALUE π
It returns a pointer to the portal containing the cursor. Heads up: there's no error return convention here β any error will be reported via elog. so like, don't go looking for a fancy error code, mmkay? π
xoxo, this page is from PostgreSQL 13.23, dated 2025 β stay slay and may your portals outlive your functions π β¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13-docs 13.23-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