SPI_CURSOR_OPEN(3)

πŸ“¦ version 14.24, in opensuse-leap 16.0

✨ 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:

  1. 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 πŸ”₯
  2. 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 * name name for portal, or NULL to let the system select a name. let the system pick, it's got impeccable taste πŸ’…
  • SPIPlanPtr plan prepared statement (returned by SPI_prepare). this is the thing you already prepped, bestie πŸŽ€
  • Datum * values An 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 * nulls An 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_only true for read-only execution. set false only 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 via elog. so like… don't sit there waiting for a NULL to 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