pgrx icon indicating copy to clipboard operation
pgrx copied to clipboard

how to read data using SPI ?

Open NLGithubWP opened this issue 1 year ago • 1 comments

hello, everyone, i reaally need some help here I got a table where each column in integer. I want to select some data using the server programming interface provided by pgrx.spi.

This code helps,

let _ = Spi::connect(|client| {
    let query = format!("SELECT * FROM {} {} LIMIT {}", dataset, SQL, batch_size);
    let mut cursor = client.open_cursor(&query, None);
    let table = match cursor.fetch(batch_size as c_long) {
        Ok(table) => table,
        Err(e) => return Err(e.to_string()),
    };
    for row in the table.into_iter() {
        for i in 3..=row.columns() {
            if let Ok(Some(val)) = row.get::<i32>(i) {
                all_rows.push(val);
            }
        }
    }

However, now one problem here: I need to further execute a for loop to iterate all columns and all rows, this is too slow> Is there any method that can skip such for loop? and directly get the result as a Vec?

NLGithubWP avatar Oct 15 '23 03:10 NLGithubWP

let mut all_rows = Vec::new();

NLGithubWP avatar Oct 15 '23 03:10 NLGithubWP