Skip to main content

PgPool

Struct PgPool 

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

§Example

let config = PoolConfig::new("localhost", 5432, "user", "db")
    .password("secret")
    .max_connections(20);
let pool = PgPool::connect(config).await?;
// Get a connection from the pool
let mut conn = pool.acquire_raw().await?;
conn.simple_query("SELECT 1").await?;

Implementations§

Source§

impl PgPool

Source

pub async fn from_config() -> PgResult<Self>

Create a pool from qail.toml (loads and parses automatically).

§Example
let pool = PgPool::from_config().await?;
Source

pub async fn connect(config: PoolConfig) -> PgResult<Self>

Create a new connection pool.

Source

pub async fn acquire_raw(&self) -> PgResult<PooledConnection>

Acquire a raw connection from the pool (crate-internal only).

§Safety (not unsafe in the Rust sense, but security-critical)

This returns a connection with no RLS context. All tenant data queries on this connection will bypass row-level security.

Safe usage: Pair with fetch_all_with_rls() for pipelined RLS+query execution (single roundtrip). Or use acquire_with_rls() / acquire_with_rls_timeout() for the 2-roundtrip path.

Unsafe usage: Running queries directly on a raw connection without RLS context. Every call site MUST include a // SAFETY: comment explaining why raw acquisition is justified.

Source

pub async fn acquire_with_rls( &self, ctx: RlsContext, ) -> PgResult<PooledConnection>

Acquire a connection with RLS context pre-configured.

Sets PostgreSQL session variables for tenant isolation before returning the connection. When the connection is dropped, it automatically clears the RLS context before returning to the pool.

§Example
use qail_core::rls::RlsContext;

let mut conn = pool.acquire_with_rls(
    RlsContext::tenant("550e8400-e29b-41d4-a716-446655440000")
).await?;
// All queries through `conn` are now scoped to this tenant
Source

pub async fn with_rls<T, F>(&self, ctx: RlsContext, f: F) -> PgResult<T>
where F: for<'a> FnOnce(&'a mut PooledConnection) -> ScopedPoolFuture<'a, T>,

Scoped connection helper that guarantees release() after closure execution.

Prefer this over manual acquire_with_rls() in normal request handlers.

Source

pub async fn with_system<T, F>(&self, f: F) -> PgResult<T>
where F: for<'a> FnOnce(&'a mut PooledConnection) -> ScopedPoolFuture<'a, T>,

Scoped helper for system-level operations (RlsContext::empty()).

Source

pub async fn with_global<T, F>(&self, f: F) -> PgResult<T>
where F: for<'a> FnOnce(&'a mut PooledConnection) -> ScopedPoolFuture<'a, T>,

Scoped helper for global/platform row access (tenant_id IS NULL).

Source

pub async fn with_tenant<T, F>(&self, tenant_id: &str, f: F) -> PgResult<T>
where F: for<'a> FnOnce(&'a mut PooledConnection) -> ScopedPoolFuture<'a, T>,

Scoped helper for single-tenant access.

Source

pub async fn acquire_with_rls_timeout( &self, ctx: RlsContext, timeout_ms: u32, ) -> PgResult<PooledConnection>

Acquire a connection with RLS context AND statement timeout.

Like acquire_with_rls(), but also sets statement_timeout to prevent runaway queries from holding pool connections indefinitely.

Source

pub async fn with_rls_timeout<T, F>( &self, ctx: RlsContext, timeout_ms: u32, f: F, ) -> PgResult<T>
where F: for<'a> FnOnce(&'a mut PooledConnection) -> ScopedPoolFuture<'a, T>,

Scoped connection helper that guarantees release() after closure execution.

Source

pub async fn acquire_with_rls_timeouts( &self, ctx: RlsContext, statement_timeout_ms: u32, lock_timeout_ms: u32, ) -> PgResult<PooledConnection>

Acquire a connection with RLS context, statement timeout, AND lock timeout.

Like acquire_with_rls_timeout(), but also sets lock_timeout to prevent queries from blocking indefinitely on row/table locks. When lock_timeout_ms is 0, the lock_timeout clause is omitted.

Source

pub async fn with_rls_timeouts<T, F>( &self, ctx: RlsContext, statement_timeout_ms: u32, lock_timeout_ms: u32, f: F, ) -> PgResult<T>
where F: for<'a> FnOnce(&'a mut PooledConnection) -> ScopedPoolFuture<'a, T>,

Scoped connection helper that guarantees release() after closure execution.

Source

pub async fn acquire_system(&self) -> PgResult<PooledConnection>

Acquire a connection for system-level operations (no tenant context).

Sets RLS session variables to maximally restrictive values:

  • app.current_tenant_id = ''
  • app.current_agent_id = ''
  • app.is_super_admin = false

Use this for startup introspection, migrations, and health checks that must not operate within any tenant scope.

Source

pub async fn acquire_global(&self) -> PgResult<PooledConnection>

Acquire a connection scoped to global/platform rows.

Shorthand for acquire_with_rls(RlsContext::global()). Use this for shared reference data (for example: currencies, ports, vessel types) stored as tenant_id IS NULL.

Source

pub async fn acquire_for_tenant( &self, tenant_id: &str, ) -> PgResult<PooledConnection>

Acquire a connection scoped to a specific tenant.

Shorthand for acquire_with_rls(RlsContext::tenant(tenant_id)). Use this when you already know the tenant UUID and want a tenant-scoped connection in a single call.

§Example
let mut conn = pool.acquire_for_tenant("550e8400-...").await?;
// All queries through `conn` are now scoped to this tenant
Source

pub async fn acquire_with_branch( &self, ctx: &BranchContext, ) -> PgResult<PooledConnection>

Acquire a connection with branch context pre-configured.

Sets PostgreSQL session variable app.branch_id for data virtualization. When the connection is dropped, it automatically clears the branch context.

§Example
use qail_core::branch::BranchContext;

let ctx = BranchContext::branch("feature-auth");
let mut conn = pool.acquire_with_branch(&ctx).await?;
// All queries through `conn` are now branch-aware
Source

pub async fn idle_count(&self) -> usize

Get the current number of idle connections.

Source

pub fn active_count(&self) -> usize

Get the number of connections currently in use.

Source

pub fn max_connections(&self) -> usize

Get the maximum number of connections.

Source

pub async fn stats(&self) -> PoolStats

Get comprehensive pool statistics.

Source

pub fn is_closed(&self) -> bool

Check if the pool is closed.

Source

pub async fn close(&self)

Close the pool gracefully.

Rejects new acquires immediately, then waits up to acquire_timeout for in-flight connections to be released before dropping idle connections. Connections released after closure are destroyed by return_connection and not returned to the idle queue.

Source

pub async fn close_graceful(&self, drain_timeout: Duration)

Close the pool gracefully with an explicit drain timeout.

Source

pub async fn maintain(&self)

Run one maintenance cycle: evict stale idle connections and backfill to min_connections. Called periodically by spawn_pool_maintenance.

Trait Implementations§

Source§

impl Clone for PgPool

Source§

fn clone(&self) -> PgPool

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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
Source§

impl<T> ColumnValue<Value> for T