[][src]Crate deadpool_postgres

Deadpool simple async pool for PostgreSQL connections.

This crate implements a deadpool manager for tokio-postgres and also provides a statement cache by wrapping tokio_postgres::Client and tokio_postgres::Transaction.

You should not need to use deadpool directly. Use the Pool type provided by this crate instead.

Example

use std::env;

use deadpool_postgres::{Manager, Pool};
use tokio_postgres::{Config, NoTls};

#[tokio::main]
async fn main() {
    let mut cfg = Config::new();
    cfg.host("/var/run/postgresql");
    cfg.user(env::var("USER").unwrap().as_str());
    cfg.dbname("deadpool");
    let mgr = Manager::new(cfg, tokio_postgres::NoTls);
    let pool = Pool::new(mgr, 16);
    for i in 1..10 {
        let mut client = pool.get().await.unwrap();
        let stmt = client.prepare("SELECT 1 + $1").await.unwrap();
        let rows = client.query(&stmt, &[&i]).await.unwrap();
        let value: i32 = rows[0].get(0);
        assert_eq!(value, i + 1);
    }
}

Structs

Client

A wrapper for tokio_postgres::Client which includes a statement cache.

Manager

The manager for creating and recyling postgresql connections

StatementCache

This structure holds the cached statements and provides access to functions for retrieving the current size and clearing the cache.

Transaction

A wrapper for tokio_postgres::Transaction which uses the statement cache from the client object it was created by.

Type Definitions

Pool

A type alias for using deadpool::Pool with tokio_postgres