Expand description
tokio-postgres extension to support forward fetching cursors.
§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(())
}§Queries
query_cursor will create
a new instance of CursorStream
which will execute the following query to declare a cursor:
DECLARE <cursor> NO SCROLL CURSOR FOR <query>CursorStream implements
Stream trait
and will execute the following query to fetch rows in batches:
FETCH FORWARD <batch_size> FROM <cursor>Cursor won’t be closed automatically when the stream is exhausted,
subsequent polls of CursorStream will return None.
Cursor can be closed manually by using close.
It doesn’t leak outside of transaction and will be closed automatically when transaction ends.
§Vulnerabilities
It’s up to the user to ensure that the query passed to
query_cursor is safe
from SQL injection vulnerabilities.
Structs§
- Cursor
Stream - A stream that fetches rows from a PostgreSQL cursor in batches.
Traits§
- Transaction
Ext - Extension trait for
Transactionto add cursor support.