Skip to content

Commit

Permalink
Use SPITupleTable->numvals when available (#1037)
Browse files Browse the repository at this point in the history
Starting from PostgreSQL, SPI tuple table contains number of rows
returned in `numvals`, with `SPI_processed` still available for
"historical reasons":

"The fields tupdesc, vals, and numvals can be used by SPI callers; the
remaining fields are internal. vals is an array of pointers to rows. The
number of rows is given by numvals (for somewhat historical reasons,
this count is also returned in SPI_processed)."

(https://www.postgresql.org/docs/current/spi-spi-execute.html)

Also:

"Some utility commands (COPY, CREATE TABLE AS) don't return a row set,
so SPI_tuptable is NULL, but they still return the number of rows
processed in SPI_processed."

So we're following this logic.

This is to ensure we're not relying on "historical" API going forward.
  • Loading branch information
yrashk authored Feb 16, 2023
1 parent 31567c1 commit 05d8b35
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pgx/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,17 @@ impl<'a> SpiClient<'a> {
Some(pg_sys::SPI_tuptable)
}
},
#[cfg(any(feature = "pg11", feature = "pg12"))]
size: unsafe { pg_sys::SPI_processed as usize },
#[cfg(not(any(feature = "pg11", feature = "pg12")))]
// SAFETY: no concurrent access
size: unsafe {
if pg_sys::SPI_tuptable.is_null() {
pg_sys::SPI_processed as usize
} else {
(*pg_sys::SPI_tuptable).numvals as usize
}
},
current: -1,
})
}
Expand Down

0 comments on commit 05d8b35

Please sign in to comment.