Skip to main content

Pool

Struct Pool 

Source
pub struct Pool { /* private fields */ }
Expand description

A PostgreSQL connection pool.

Wraps deadpool-postgres with fail-fast acquire semantics, singleflight query coalescing, and optional read/write splitting.

Implementations§

Source§

impl Pool

Source

pub async fn connect(url: &str) -> BsqlResult<Self>

Connect to PostgreSQL using a connection URL.

Format: postgres://user:password@host:port/dbname

Source

pub fn builder() -> PoolBuilder

Create a pool builder for fine-grained configuration.

Source

pub async fn acquire(&self) -> BsqlResult<PoolConnection>

Acquire a connection from the primary pool.

Fail-fast: returns BsqlError::Pool immediately if no connections are available. Does not wait. Does not timeout. See CREDO principle #17.

Source

pub fn is_pgbouncer(&self) -> bool

Whether PgBouncer was detected between the client and PostgreSQL.

Source

pub fn has_replicas(&self) -> bool

Whether read replicas are configured.

Source

pub async fn begin(&self) -> BsqlResult<Transaction>

Begin a new transaction.

Acquires a connection from the primary pool. BEGIN is sent lazily on the first query inside the transaction (see Transaction::ensure_begun). This eliminates one PG round-trip when the transaction is created.

If the transaction is committed or dropped without executing any queries, no BEGIN/COMMIT/ROLLBACK is sent at all and the connection returns to the pool cleanly.

Fail-fast: returns BsqlError::Pool immediately if no connections are available. See CREDO principle #17.

Source

pub async fn query_stream( &self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> BsqlResult<QueryStream>

Execute a query and return a stream of rows.

Acquires a connection from the pool and returns a QueryStream that holds the connection alive until the stream is consumed or dropped. Rows arrive one at a time, avoiding buffering the entire result set in memory.

Fail-fast: returns BsqlError::Pool immediately if no connections are available. See CREDO principle #17.

This method is only available on Pool (not PoolConnection or Transaction) because the stream must own the connection for its entire lifetime.

Source

pub async fn warmup(&self, sqls: &[&str]) -> BsqlResult<()>

Pre-prepare SQL statements on a connection from the pool.

Acquires one connection and calls prepare_cached for each SQL string. This populates the connection’s statement cache so the first real query on that connection pays zero PREPARE overhead.

Call this once after creating the pool with the SQL strings your application uses. The easiest source is your query!() calls:

pool.warmup(&[
    "SELECT id, login FROM users WHERE id = $1",
    "UPDATE tickets SET status = $1 WHERE id = $2",
]).await?;

Best-effort: if the pool is exhausted or a PREPARE fails (e.g. table was dropped), the error is returned but partial warmup of earlier statements still takes effect.

Calling warmup with an empty slice is a no-op.

Source

pub fn status(&self) -> PoolStatus

Current pool status: available and total connections.

Trait Implementations§

Source§

impl Debug for Pool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Executor for Pool

Source§

type Rows = Arc<[Row]>

Row container returned by query_raw / query_raw_readonly. Read more
Source§

async fn query_raw( &self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> BsqlResult<Arc<[Row]>>

Execute a query and return all rows.
Source§

async fn query_raw_readonly( &self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> BsqlResult<Arc<[Row]>>

Execute a read-only query. Routes to replicas when available. Read more
Source§

async fn execute_raw( &self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> BsqlResult<u64>

Execute a query and return the number of affected rows.

Auto Trait Implementations§

§

impl !Freeze for Pool

§

impl !RefUnwindSafe for Pool

§

impl Send for Pool

§

impl Sync for Pool

§

impl Unpin for Pool

§

impl UnsafeUnpin for Pool

§

impl !UnwindSafe for Pool

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more