pub struct PooledConnection { /* private fields */ }Expand description
A connection checked out from the pool.
When dropped, the connection is automatically returned to the pool
(unless it has been marked as broken). The after_release callback
runs before the connection re-enters the idle queue.
Implementations§
Source§impl PooledConnection
impl PooledConnection
Sourcepub fn mark_broken(&mut self)
pub fn mark_broken(&mut self)
Mark this connection as broken. It will be discarded on drop instead of being returned to the pool.
Methods from Deref<Target = Connection>§
Sourcepub fn cancel_token(&self) -> CancelToken
pub fn cancel_token(&self) -> CancelToken
Get a cancel token for this connection.
The token can be cloned and sent to another task to cancel a
running query. See CancelToken for details.
Sourcepub fn config(&self) -> &Config
pub fn config(&self) -> &Config
Returns true if the connection is using TLS.
Returns the configuration used for this connection.
Sourcepub fn connected_host(&self) -> &str
pub fn connected_host(&self) -> &str
Returns the host this connection is connected to.
Sourcepub fn connected_port(&self) -> u16
pub fn connected_port(&self) -> u16
Returns the port this connection is connected to.
pub fn is_tls(&self) -> bool
Sourcepub fn process_id(&self) -> i32
pub fn process_id(&self) -> i32
The server process ID for this connection.
Sourcepub fn query_timeout(&self) -> Option<Duration>
pub fn query_timeout(&self) -> Option<Duration>
Returns the configured query timeout, if any.
Sourcepub fn is_broken(&self) -> bool
pub fn is_broken(&self) -> bool
Returns true if the connection has been marked broken by a timeout.
A broken connection should be discarded — the server state is indeterminate after a cancelled query.
Sourcepub fn transaction_status(&self) -> TransactionStatus
pub fn transaction_status(&self) -> TransactionStatus
Current transaction status.
Sourcepub async fn copy_in(&mut self, sql: &str) -> Result<CopyIn<'_>>
pub async fn copy_in(&mut self, sql: &str) -> Result<CopyIn<'_>>
Start a COPY IN operation for bulk data loading.
Sourcepub async fn copy_out(&mut self, sql: &str) -> Result<CopyOut<'_>>
pub async fn copy_out(&mut self, sql: &str) -> Result<CopyOut<'_>>
Start a COPY OUT operation for bulk data export.
Sourcepub async fn listen(&mut self, channel: &str) -> Result<()>
pub async fn listen(&mut self, channel: &str) -> Result<()>
Subscribe to LISTEN/NOTIFY on a channel.
Sourcepub async fn unlisten_all(&mut self) -> Result<()>
pub async fn unlisten_all(&mut self) -> Result<()>
Unsubscribe from all channels.
Sourcepub async fn notify(&mut self, channel: &str, payload: &str) -> Result<()>
pub async fn notify(&mut self, channel: &str, payload: &str) -> Result<()>
Send a notification on a channel.
Sourcepub async fn wait_for_notification(&mut self) -> Result<Notification>
pub async fn wait_for_notification(&mut self) -> Result<Notification>
Wait for the next LISTEN/NOTIFY notification.
Blocks until a notification arrives on any subscribed channel.
Sourcepub fn pipeline(&self) -> PipelineBatch
pub fn pipeline(&self) -> PipelineBatch
Create a pipeline batch for executing multiple queries in a single round-trip.
Use execute_pipeline() to send the batch.
Sourcepub async fn execute_pipeline(
&mut self,
batch: PipelineBatch,
) -> Result<Vec<QueryResult>>
pub async fn execute_pipeline( &mut self, batch: PipelineBatch, ) -> Result<Vec<QueryResult>>
Execute a pipeline batch, returning results for each query.
Sourcepub async fn bind_portal(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Portal>
pub async fn bind_portal( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Portal>
Create a server-side portal (cursor) for incremental row fetching.
Portals only work inside transactions. The portal borrows the connection state, so no concurrent queries while a portal is open.
§Example
conn.begin().await?;
let portal = conn.bind_portal("SELECT * FROM big_table", &[]).await?;
let batch = conn.query_portal(&portal, 100).await?;
conn.close_portal(portal).await?;
conn.commit().await?;Sourcepub async fn query_portal(
&mut self,
portal: &Portal,
max_rows: i32,
) -> Result<Vec<Row>>
pub async fn query_portal( &mut self, portal: &Portal, max_rows: i32, ) -> Result<Vec<Row>>
Fetch up to max_rows rows from a portal.
Returns an empty Vec when the cursor is exhausted.
Use max_rows = 0 to fetch all remaining rows.
Sourcepub async fn close_portal(&mut self, portal: Portal) -> Result<()>
pub async fn close_portal(&mut self, portal: Portal) -> Result<()>
Close a portal on the server, freeing resources.
Sourcepub async fn prepare(&mut self, sql: &str) -> Result<Statement>
pub async fn prepare(&mut self, sql: &str) -> Result<Statement>
Prepare a statement on the server using extended query protocol.
Returns a Statement with parameter types and column descriptions.
Sourcepub fn register_statement(&mut self, name: &str, statement: Statement)
pub fn register_statement(&mut self, name: &str, statement: Statement)
Register a prepared statement in the Tier 1 cache.
Sourcepub fn cache_metrics(&self) -> &CacheMetrics
pub fn cache_metrics(&self) -> &CacheMetrics
Get statement cache metrics.
Sourcepub async fn query(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Vec<Row>>
pub async fn query( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Vec<Row>>
Execute a query that returns rows.
Parameters are encoded in binary format.
let rows = conn.query("SELECT * FROM users WHERE id = $1", &[&42i32]).await?;Sourcepub async fn query_one(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Row>
pub async fn query_one( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Row>
Execute a query that returns a single row.
Returns an error if no rows are returned.
Sourcepub async fn query_opt(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>>
pub async fn query_opt( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<Option<Row>>
Execute a query that returns an optional single row.
Sourcepub async fn execute(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<u64>
pub async fn execute( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<u64>
Execute a non-SELECT query (INSERT, UPDATE, DELETE, etc.).
Returns the number of rows affected.
Sourcepub async fn query_with_timeout(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
timeout: Duration,
) -> Result<Vec<Row>>
pub async fn query_with_timeout( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], timeout: Duration, ) -> Result<Vec<Row>>
Execute a query with a timeout.
If the query does not complete within timeout, a cancel request
is sent to the server and the connection is marked as broken.
Sourcepub async fn execute_with_timeout(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
timeout: Duration,
) -> Result<u64>
pub async fn execute_with_timeout( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], timeout: Duration, ) -> Result<u64>
Execute a non-SELECT query with a timeout.
If the query does not complete within timeout, a cancel request
is sent to the server and the connection is marked as broken.
Sourcepub async fn simple_query(
&mut self,
sql: &str,
) -> Result<Vec<SimpleQueryMessage>>
pub async fn simple_query( &mut self, sql: &str, ) -> Result<Vec<SimpleQueryMessage>>
Execute a simple query (no parameters, text protocol).
Returns row data (in text format) and command completions. Useful for DDL statements, multi-statement queries, and queries where you don’t need binary-decoded typed values.
use sentinel_driver::SimpleQueryMessage;
let messages = conn.simple_query("SELECT 1 AS n; SELECT 'hello' AS greeting").await?;
for msg in &messages {
match msg {
SimpleQueryMessage::Row(row) => {
println!("value: {:?}", row.get(0));
}
SimpleQueryMessage::CommandComplete(n) => {
println!("rows: {n}");
}
}
}Sourcepub async fn query_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Vec<Row>>
pub async fn query_typed( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Vec<Row>>
Execute a query with inline parameter types, skipping the prepare step.
Instead of a separate Prepare round-trip, the parameter types are
specified directly in the Parse message. This saves one round-trip
compared to query() at the cost of requiring the
caller to specify types explicitly.
use sentinel_driver::Oid;
let rows = conn.query_typed(
"SELECT $1::int4 + $2::int4 AS sum",
&[(&1i32, Oid::INT4), (&2i32, Oid::INT4)],
).await?;Sourcepub async fn query_typed_one(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Row>
pub async fn query_typed_one( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Row>
Execute a typed query that returns a single row.
Sourcepub async fn query_typed_opt(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Option<Row>>
pub async fn query_typed_opt( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<Option<Row>>
Execute a typed query that returns an optional single row.
Sourcepub async fn execute_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<u64>
pub async fn execute_typed( &mut self, sql: &str, params: &[(&(dyn ToSql + Sync), Oid)], ) -> Result<u64>
Execute a typed non-SELECT query, returning rows affected.
Sourcepub async fn query_stream(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<RowStream<'_>>
pub async fn query_stream( &mut self, sql: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<RowStream<'_>>
Execute a streaming query that returns rows one at a time.
Unlike query() which materializes all rows
in memory, this returns a RowStream that yields rows lazily.
Ideal for large result sets.
The stream holds an exclusive borrow of the connection — no other queries can run until the stream is dropped or fully consumed.
let mut stream = conn.query_stream("SELECT * FROM users", &[]).await?;
while let Some(row) = stream.next().await? {
let name: String = row.get(0);
}Sourcepub async fn begin_with(&mut self, config: TransactionConfig) -> Result<()>
pub async fn begin_with(&mut self, config: TransactionConfig) -> Result<()>
Begin a transaction with custom settings.
Sourcepub async fn rollback_to(&mut self, name: &str) -> Result<()>
pub async fn rollback_to(&mut self, name: &str) -> Result<()>
Rollback to a savepoint.