SPI_CURS...WITH_ARGS(3)

๐Ÿ“ฆ version 13.23, in opensuse-leap 16.0

๐ŸŽ€ 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, or NULL to 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 length nargs, containing the OIDs of the data types of the parameters. Itโ€™s giving โ€œtype info, but make it arrayโ€ ๐Ÿงš
  • Datum * values โ€” an array of length nargs, containing the actual parameter values. The real ones. The payload ๐Ÿ’–
  • const char * nulls โ€” an array of length nargs, describing which parameters are null. If nulls is NULL then SPI_cursor_open_with_args 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.) Note that nulls is not a text string, just an array: it does not need a '\0' terminator. Tiny but important detail, no cap ๐Ÿ’€
  • bool read_only โ€” true for read-only execution. Set it to true when 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