tokio-postgres-cursor

Crates.iotokio-postgres-cursor
lib.rstokio-postgres-cursor
version0.1.0
created_at2025-11-05 21:33:39.81859+00
updated_at2025-11-05 21:33:39.81859+00
descriptionPostgreSQL forward fetch cursor support for `tokio-postgres`
homepage
repositoryhttps://github.com/mee7ya/tokio-postgres-cursor
max_upload_size
id1918669
size36,131
Timur (mee7ya)

documentation

README

tokio-postgres-cursor

PostgreSQL forward fetch cursor support for tokio-postgres.

Example

use futures::StreamExt;

use tokio_postgres::{Error, NoTls};
use tokio_postgres_cursor::TransactionExt;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let (mut client, connection) =
        tokio_postgres::connect("host=localhost user=postgres", NoTls).await?;
    tokio::spawn(async move {
        if let Err(e) = connection.await {
            eprintln!("connection error: {}", e);
        }
    });

    // Cursors require to be declared inside a transaction
    let tx = client.transaction().await?;

    // Following line will declare cursor inside transaction and return CursorStream
    let mut cursor_stream = tx.query_cursor("SELECT * FROM my_table", 10).await?;
    
    // Fetch rows in batches of 10
    while let Some(result) = cursor_stream.next().await {
        match result {
            Ok(rows) => {
                for row in rows {
                    println!("{row:?}");
                }
            }
            Err(e) => eprintln!("{e}"),
        }
    }
    
    // Explicitly closing cursor
    cursor_stream.close().await?;
    tx.commit().await?;
    Ok(())
}
Commit count: 0

cargo fmt