#async #database #pool #postgres

deadpool-postgres

Dead simple async pool for tokio-postgres

17 releases

0.6.0 Nov 4, 2020
0.5.6 Jul 14, 2020
0.5.5 Feb 28, 2020
0.4.2 Dec 31, 2019
0.2.1 Nov 18, 2019

#35 in Database interfaces

Download history 947/week @ 2020-08-13 976/week @ 2020-08-20 1044/week @ 2020-08-27 1230/week @ 2020-09-03 1548/week @ 2020-09-10 1431/week @ 2020-09-17 1702/week @ 2020-09-24 1446/week @ 2020-10-01 1273/week @ 2020-10-08 1450/week @ 2020-10-15 1510/week @ 2020-10-22 1439/week @ 2020-10-29 1429/week @ 2020-11-05 1514/week @ 2020-11-12 1712/week @ 2020-11-19 1366/week @ 2020-11-26

5,575 downloads per month
Used in 9 crates (5 directly)

MIT/Apache

80KB
1K SLoC

Deadpool for PostgreSQL Latest Version

Deadpool is a dead simple async pool for connections and objects of any type.

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

Features

Feature Description Extra dependencies Default
config Enable support for config crate config, serde/derive yes

Example

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

#[tokio::main]
async fn main() {
    let mut cfg = Config::new();
    cfg.dbname = Some("deadpool".to_string());
    cfg.manager = Some(ManagerConfig { recycling_method: RecyclingMethod::Fast });
    let pool = cfg.create_pool(NoTls).unwrap();
    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);
    }
}

Example with config and dotenv crate

# .env
PG__DBNAME=deadpool
use deadpool_postgres::{Manager, Pool};
use dotenv::dotenv;
use serde::Deserialize;
use tokio_postgres::NoTls;

#[derive(Debug, Deserialize)]
struct Config {
    pg: deadpool_postgres::Config
}

impl Config {
    pub fn from_env() -> Result<Self, ::config_crate::ConfigError> {
        let mut cfg = ::config_crate::Config::new();
        cfg.merge(::config_crate::Environment::new().separator("__"))?;
        cfg.try_into()
    }
}

#[tokio::main]
async fn main() {
    dotenv().ok();
    let mut cfg = Config::from_env().unwrap();
    let pool = cfg.pg.create_pool(NoTls).unwrap();
    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);
    }
}

Note: The code above uses the crate name config_crate because of the config feature and both features and dependencies share the same namespace. In your own code you will probably want to use ::config::ConfigError and ::config::Config instead.

Example using an existing tokio_postgres::Config object

use std::env;
use deadpool_postgres::{Manager, ManagerConfig, Pool, RecyclingMethod};
use tokio_postgres::NoTls;

#[tokio::main]
async fn main() {
    let mut pg_config = tokio_postgres::Config::new();
    pg_config.host_path("/run/postgresql");
    pg_config.host_path("/tmp");
    pg_config.user(env::var("USER").unwrap().as_str());
    pg_config.dbname("deadpool");
    let mgr_config = ManagerConfig {
        recycling_method: RecyclingMethod::Fast
    };
    let mgr = Manager::from_config(pg_config, NoTls, mgr_config);
    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);
    }
}

FAQ

  • The database is unreachable. Why does the pool creation not fail?

    Deadpool has identical startup and runtime behaviour and therefore the pool creation will never fail.

    If you want your application to crash on startup if no database connection can be established just call pool.get().await right after creating the pool.

  • Why are connections retrieved from the pool sometimes unuseable?

    In deadpool-postgres 0.5.5 a new recycling method was implemented which will become the default in 0.6. With that recycling method the manager no longer performs a test query prior returning the connection but relies solely on tokio_postgres::Client::is_closed instead. Under some rare circumstances (e.g. unreliable networks) this can lead to tokio_postgres not noticing a disconnect and reporting the connection as useable.

    The old and slightly slower recycling method can be enabled by setting ManagerConfig::recycling_method to RecyclingMethod::Verified or when using the config crate by setting PG.MANAGER.RECYCLING_METHOD=Verified.

  • How can I enable features of the tokio-postgres crate?

    Make sure that you depend on the same version of tokio-postgres as deadpool-postgres does and enable the needed features in your own Crate.toml file:

    [dependencies]
    deadpool-postgres = { version = "0.6" }
    tokio-postgres = { version = "0.6", features = ["with-uuid-0_8"] }
    

    Important: The version numbers of deadpool-postgres and tokio-postgres do not necessarily match. It is just a coincidence that both crates have the same MAJOR and MINOR version number at the time of this writing.

License

Licensed under either of

at your option.

Dependencies

~8MB
~176K SLoC