Skip to main content

PooledConnection

Struct PooledConnection 

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

A pooled connection handle.

Use PooledConnection::release for deterministic reset+return behavior. If dropped without release(), the pool performs best-effort bounded async cleanup; on any uncertainty it destroys the connection (fail-closed).

Implementations§

Source§

impl PooledConnection

Source

pub fn get(&self) -> PgResult<&PgConnection>

Get a shared reference to the underlying connection.

Returns an error if the connection has already been released.

Source

pub fn get_mut(&mut self) -> PgResult<&mut PgConnection>

Get a mutable reference to the underlying connection.

Returns an error if the connection has already been released.

Source

pub fn cancel_token(&self) -> PgResult<CancelToken>

Get a token to cancel the currently running query.

Source

pub async fn release(self)

Deterministic connection cleanup and pool return.

This is the correct way to return a connection to the pool. COMMITs the transaction (which auto-resets transaction-local RLS session variables) and returns the connection to the pool with prepared statement caches intact.

If cleanup fails, the connection is destroyed (not returned to pool).

§Usage
let mut conn = pool.acquire_with_rls(ctx).await?;
let result = conn.fetch_all_cached(&cmd).await;
conn.release().await; // COMMIT + return to pool
result
Source

pub async fn begin(&mut self) -> PgResult<()>

Begin an explicit transaction on this pooled connection.

Use this when you need multi-statement atomicity beyond the implicit transaction created by acquire_with_rls().

§Example
let mut conn = pool.acquire_with_rls(ctx).await?;
conn.begin().await?;
conn.execute(&insert1).await?;
conn.execute(&insert2).await?;
conn.commit().await?;
conn.release().await;
Source

pub async fn commit(&mut self) -> PgResult<()>

Commit the current transaction. Makes all changes since begin() permanent.

Source

pub async fn rollback(&mut self) -> PgResult<()>

Rollback the current transaction. Discards all changes since begin().

Source

pub async fn savepoint(&mut self, name: &str) -> PgResult<()>

Create a named savepoint within the current transaction. Use rollback_to() to return to this savepoint.

Source

pub async fn rollback_to(&mut self, name: &str) -> PgResult<()>

Rollback to a previously created savepoint. Discards changes since the savepoint, but keeps the transaction open.

Source

pub async fn release_savepoint(&mut self, name: &str) -> PgResult<()>

Release a savepoint (free resources). After release, the savepoint cannot be rolled back to.

Source

pub async fn pipeline_ast( &mut self, cmds: &[Qail], ) -> PgResult<Vec<Vec<Vec<Option<Vec<u8>>>>>>

Execute multiple QAIL commands in a single PG pipeline round-trip.

Sends all queries as Parse+Bind+Execute in one write, receives all responses in one read. Returns raw column data per query per row.

This is the fastest path for batch operations — amortizes TCP overhead across N queries into a single syscall pair.

Source

pub async fn explain_estimate( &mut self, cmd: &Qail, ) -> PgResult<Option<ExplainEstimate>>

Run EXPLAIN (FORMAT JSON) on a Qail command and return cost estimates.

Uses simple_query under the hood — no additional round-trips beyond the single EXPLAIN statement. Returns None if parsing fails or the EXPLAIN output is unexpected.

Source

pub async fn listen(&mut self, channel: &str) -> PgResult<()>

Subscribe to a PostgreSQL notification channel.

Delegates to PgConnection::listen.

Source

pub async fn unlisten(&mut self, channel: &str) -> PgResult<()>

Unsubscribe from a PostgreSQL notification channel.

Delegates to PgConnection::unlisten.

Source

pub async fn unlisten_all(&mut self) -> PgResult<()>

Unsubscribe from all notification channels.

Delegates to PgConnection::unlisten_all.

Source

pub async fn recv_notification(&mut self) -> PgResult<Notification>

Wait for the next notification, blocking until one arrives.

Delegates to PgConnection::recv_notification. Useful for dedicated LISTEN connections in background tasks.

Source§

impl PooledConnection

Source

pub async fn fetch_all_uncached(&mut self, cmd: &Qail) -> PgResult<Vec<PgRow>>

Execute a QAIL command and fetch all rows (UNCACHED). Returns rows with column metadata for JSON serialization.

Source

pub async fn query_raw_with_params( &mut self, sql: &str, params: &[Option<Vec<u8>>], ) -> PgResult<Vec<Vec<Option<Vec<u8>>>>>

Execute raw SQL with bind parameters and return raw row data.

Uses the Extended Query Protocol so parameters are never interpolated into the SQL string. Intended for EXPLAIN or other SQL that can’t be represented as a Qail AST but still needs parameterized execution.

Returns raw column bytes; callers must decode as needed.

Source

pub async fn copy_export(&mut self, cmd: &Qail) -> PgResult<Vec<Vec<String>>>

Export data using AST-native COPY TO STDOUT and collect parsed rows.

Source

pub async fn copy_export_stream_raw<F, Fut>( &mut self, cmd: &Qail, on_chunk: F, ) -> PgResult<()>
where F: FnMut(Vec<u8>) -> Fut, Fut: Future<Output = PgResult<()>>,

Stream AST-native COPY TO STDOUT chunks with bounded memory usage.

Source

pub async fn copy_export_stream_rows<F>( &mut self, cmd: &Qail, on_row: F, ) -> PgResult<()>
where F: FnMut(Vec<String>) -> PgResult<()>,

Stream AST-native COPY TO STDOUT rows with bounded memory usage.

Source

pub async fn copy_export_table( &mut self, table: &str, columns: &[String], ) -> PgResult<Vec<u8>>

Export a table using COPY TO STDOUT and collect raw bytes.

Source

pub async fn copy_export_table_stream<F, Fut>( &mut self, table: &str, columns: &[String], on_chunk: F, ) -> PgResult<()>
where F: FnMut(Vec<u8>) -> Fut, Fut: Future<Output = PgResult<()>>,

Stream a table export using COPY TO STDOUT with bounded memory usage.

Source

pub async fn fetch_all_uncached_with_format( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<Vec<PgRow>>

Execute a QAIL command and fetch all rows (UNCACHED) with explicit result format.

Source

pub async fn fetch_all_fast(&mut self, cmd: &Qail) -> PgResult<Vec<PgRow>>

Execute a QAIL command and fetch all rows (FAST VERSION). Uses native AST-to-wire encoding and optimized recv_with_data_fast. Skips column metadata for maximum speed.

Source

pub async fn fetch_all_fast_with_format( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<Vec<PgRow>>

Execute a QAIL command and fetch all rows (FAST VERSION) with explicit result format.

Source

pub async fn fetch_all_cached(&mut self, cmd: &Qail) -> PgResult<Vec<PgRow>>

Execute a QAIL command and fetch all rows (CACHED). Uses prepared statement caching: Parse+Describe on first call, then Bind+Execute only on subsequent calls with the same SQL shape. This matches PostgREST’s behavior for fair benchmarks.

Source

pub async fn fetch_all_cached_with_format( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<Vec<PgRow>>

Execute a QAIL command and fetch all rows (CACHED) with explicit result format.

Source

pub async fn fetch_typed<T: QailRow>(&mut self, cmd: &Qail) -> PgResult<Vec<T>>

Execute a QAIL command and decode rows into typed structs (CACHED, text format).

Source

pub async fn fetch_typed_with_format<T: QailRow>( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<Vec<T>>

Execute a QAIL command and decode rows into typed structs with explicit result format.

Use ResultFormat::Binary for binary wire values; row decoders should use metadata-aware helpers like PgRow::try_get() / try_get_by_name().

Source

pub async fn fetch_one_typed<T: QailRow>( &mut self, cmd: &Qail, ) -> PgResult<Option<T>>

Execute a QAIL command and decode one typed row (CACHED, text format).

Source

pub async fn fetch_one_typed_with_format<T: QailRow>( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<Option<T>>

Execute a QAIL command and decode one typed row with explicit result format.

Source

pub async fn fetch_all_with_rls( &mut self, cmd: &Qail, rls_sql: &str, ) -> PgResult<Vec<PgRow>>

Execute a QAIL command with RLS context in a SINGLE roundtrip.

Pipelines the RLS setup (BEGIN + set_config) and the query (Parse/Bind/Execute/Sync) into one write_all syscall. PG processes messages in order, so the BEGIN + set_config completes before the query executes — security is preserved.

Wire layout:

[SimpleQuery: "BEGIN; SET LOCAL...; SELECT set_config(...)"]
[Parse (if cache miss)]
[Describe (if cache miss)]
[Bind]
[Execute]
[Sync]

Response processing: consume 2× ReadyForQuery (SimpleQuery + Sync).

Source

pub async fn fetch_all_with_rls_with_format( &mut self, cmd: &Qail, rls_sql: &str, result_format: ResultFormat, ) -> PgResult<Vec<PgRow>>

Execute a QAIL command with RLS context in a SINGLE roundtrip with explicit result format.

Trait Implementations§

Source§

impl Drop for PooledConnection

Source§

fn drop(&mut self)

Executes the destructor for this type. 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> 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
Source§

impl<T> ColumnValue<Value> for T