SPI_MODIFYTUPLE(3)

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

πŸ’… SPI_MODIFYTUPLE(3) β€” the PostgreSQL glow-up page ✨

okay bestie, welcome to SPI_MODIFYTUPLE(3) from the PostgreSQL 17.11 docs β€” like, yes it's a C function page, but it's actually iconic once you get it 🫢


🌸 NAME 🌸

SPI_modifytuple β€” makes a whole new row by swapping out the fields you pick from a row you already have πŸ‘‘

✨ SYNOPSIS ✨

HeapTuple SPI_modifytuple(Relation rel, HeapTuple row, int ncols,
                          int * colnum, Datum * values, const char * nulls)

πŸ’– DESCRIPTION πŸ’–

So SPI_modifytuple creates a new row by substituting new values into selected columns, and it copies over the original row's columns everywhere else. The way it just... leaves the input row alone? respectful. untouched. not modified. periodt πŸ’… The new row comes back to you in the upper executor context.

⚠️ Big caveat, no cap: this function can only be used while connected to SPI. If you're not connected, it returns NULL and sets SPI_result to SPI_ERROR_UNCONNECTED. it's giving "you must be this tall to ride" 🎒

πŸŽ€ ARGUMENTS πŸŽ€

  • Relation rel β€” literally only used as the source of the row descriptor for the row. ngl the docs themselves call passing a relation instead of a row descriptor a misfeature πŸ’€ so like. know that going in πŸ™ˆ
  • HeapTuple row β€” the row you're gonna modify ✨
  • int ncols β€” how many columns you're changing πŸ”’
  • int * colnum β€” an array of length ncols holding the numbers of the columns to be changed. heads up: column numbers start at 1 🌟
  • Datum * values β€” an array of length ncols with the new values for those specified columns πŸ’–
  • const char * nulls β€” an array of length ncols that describes which of your new values are null.

If nulls is NULL, then SPI_modifytuple assumes that no new values are null. Otherwise, each entry of the nulls array should be ' ' if the corresponding new value is non-null, or 'n' if the corresponding new value is null. (In that null case, the actual value sitting in the corresponding values entry doesn't matter at all β€” it's giving decorative πŸŽ€) Also important: nulls is not a text string, it's just an array, so it does not need a '\0' terminator 🚫

🌈 RETURN VALUE 🌈

A new row with your modifications, allocated in the upper executor context β€” or NULL on error (peep SPI_result for the error indication πŸ‘€).

On error, SPI_result gets set like this:

  • SPI_ERROR_ARGUMENT β€” if rel is NULL, or if row is NULL, or if ncols is less than or equal to 0, or if colnum is NULL, or if values is NULL. basically if anything's missing, she's not doing it πŸ’€
  • SPI_ERROR_NOATTRIBUTE β€” if colnum contains an invalid column number (less than or equal to 0 or greater than the number of columns in row) 🚩
  • SPI_ERROR_UNCONNECTED β€” if SPI is not active 😭

and that's the whole page bestie, you ate that πŸ“ β€” this was SPI_MODIFYTUPLE(3) from the PostgreSQL 17.11 documentation, dated 2026. go be the main character of your own query planner πŸ‘‘πŸ’œ

πŸ“¦ where this page comes from