Server Programming Interface (SPI)

PL/Rust provides support for PostgreSQL's SPI.

Error

Result

Spi

Example usage

The following function uses SPI to create a PostgreSQL Set Returning Function (SRF).

CREATE FUNCTION spi_srf()
    RETURNS SETOF BIGINT
    LANGUAGE plrust
AS
$$
    let query = "SELECT id::BIGINT FROM generate_series(1, 3) id;";

    Spi::connect(|client| {
        let mut results = Vec::new();
        let mut tup_table = client.select(query, None, None)?;

        while let Some(row) = tup_table.next() {
            let id = row["id"].value::<i64>()?;
            results.push(id);
        }
        Ok(Some(SetOfIterator::new(results)))
    })

$$;

Complex return types

PL/Rust currently does not support RETURNS TABLE or complex types with RETURNS SETOF.