pub struct PgPool { /* private fields */ }Expand description
Implementations§
Source§impl PgPool
impl PgPool
Sourcepub async fn from_config() -> PgResult<Self>
pub async fn from_config() -> PgResult<Self>
Sourcepub async fn connect(config: PoolConfig) -> PgResult<Self>
pub async fn connect(config: PoolConfig) -> PgResult<Self>
Create a new connection pool.
Sourcepub async fn acquire_raw(&self) -> PgResult<PooledConnection>
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.
Sourcepub async fn acquire_with_rls(
&self,
ctx: RlsContext,
) -> PgResult<PooledConnection>
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::operator("550e8400-e29b-41d4-a716-446655440000")
).await?;
// All queries through `conn` are now scoped to this operatorSourcepub async fn acquire_with_rls_timeout(
&self,
ctx: RlsContext,
timeout_ms: u32,
) -> PgResult<PooledConnection>
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.
Sourcepub async fn acquire_with_rls_timeouts(
&self,
ctx: RlsContext,
statement_timeout_ms: u32,
lock_timeout_ms: u32,
) -> PgResult<PooledConnection>
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.
Sourcepub async fn acquire_system(&self) -> PgResult<PooledConnection>
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_operator_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.
Sourcepub async fn acquire_global(&self) -> PgResult<PooledConnection>
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.
Sourcepub async fn acquire_for_tenant(
&self,
tenant_id: &str,
) -> PgResult<PooledConnection>
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 tenantSourcepub async fn acquire_with_branch(
&self,
ctx: &BranchContext,
) -> PgResult<PooledConnection>
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-awareSourcepub async fn idle_count(&self) -> usize
pub async fn idle_count(&self) -> usize
Get the current number of idle connections.
Sourcepub fn active_count(&self) -> usize
pub fn active_count(&self) -> usize
Get the number of connections currently in use.
Sourcepub fn max_connections(&self) -> usize
pub fn max_connections(&self) -> usize
Get the maximum number of connections.
Sourcepub async fn close(&self)
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.
Sourcepub async fn close_graceful(&self, drain_timeout: Duration)
pub async fn close_graceful(&self, drain_timeout: Duration)
Close the pool gracefully with an explicit drain timeout.