SPI_CURS...WITH_ARGS(3)
๐ NAME ๐
okay bestie, youโre on the SPI_CURS...WITH_ARGS(3) page from the PostgreSQL 13.23 Documentation, and weโre about to make cursor setup make sense ๐
SPI_cursor_open_with_args โ set up a cursor using a query and parameters โจ
๐ธ SYNOPSIS ๐ธ
Portal SPI_cursor_open_with_args(const char *name,
const char *command,
int nargs, Oid *argtypes,
Datum *values, const char *nulls,
bool read_only, int cursorOptions)
๐ฆ DESCRIPTION ๐ฆ
okay so SPI_cursor_open_with_args sets up a cursor โ internally, a portal โ that will execute the query you pass in. Most of the parameters have the same meanings as the corresponding parameters to SPI_prepare_cursor and SPI_cursor_open, so if you already know those two, youโre lowkey already halfway there ๐ซถ
For one-time query execution, this function should be preferred over SPI_prepare_cursor followed by SPI_cursor_open. Thatโs the vibe: one-shot query, use this. But if the same command is going to be executed with many different parameters, either method might be faster โ it depends on the cost of re-planning versus the benefit of custom plans. So itโs giving โsituationalโ, not โalways pick this oneโ ๐
The passed-in parameter data will be copied into the cursorโs portal, so it can be freed while the cursor still exists. Translation: your parameter data gets its own little portal home, and you donโt have to keep it alive afterward ๐
โจ ARGUMENTS โจ
const char * nameโ name for the portal, orNULLto let the system select a name. Very โyou handle it, bestieโ of you ๐const char * commandโ command string. This is the query itself, the main character of the whole operation ๐ฌint nargsโ number of input parameters ($1,$2, etc.). Count them, donโt guess, fr fr ๐ขOid * argtypesโ an array of lengthnargs, containing the OIDs of the data types of the parameters. Itโs giving โtype info, but make it arrayโ ๐งDatum * valuesโ an array of lengthnargs, containing the actual parameter values. The real ones. The payload ๐const char * nullsโ an array of lengthnargs, describing which parameters are null. IfnullsisNULLthenSPI_cursor_open_with_argsassumes 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 the latter case, the actual value in the correspondingvaluesentry doesnโt matter.) Note thatnullsis not a text string, just an array: it does not need a'\0'terminator. Tiny but important detail, no cap ๐bool read_onlyโtruefor read-only execution. Set it totruewhen youโre just looking, not touching ๐int cursorOptionsโ integer bit mask of cursor options; zero produces default behavior. Zero is the โno notes, default vibesโ choice โญ
๐ RETURN VALUE ๐
Pointer to portal containing the cursor. Note there is no error return convention; any error will be reported via elog. So like, donโt go looking for a special error return value โ itโs not that kind of function ๐ญ
made with ๐ by the PostgreSQL 13.23 docs, 2025 โ SPI_CURS...WITH_ARGS(3) vibes only ๐ซถ
๐ฆ 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