Struct diesel_async::AsyncPgConnection
source · pub struct AsyncPgConnection { /* private fields */ }
Expand description
A connection to a PostgreSQL database.
Connection URLs should be in the form
postgres://[user[:password]@]host/database_name
Checkout the documentation of the tokio_postgres crate for details about the format
§Pipelining
This connection supports pipelined requests. Pipelining can improve performance in use cases in which multiple, independent queries need to be executed. In a traditional workflow, each query is sent to the server after the previous query completes. In contrast, pipelining allows the client to send all of the queries to the server up front, minimizing time spent by one side waiting for the other to finish sending data:
Sequential Pipelined
| Client | Server | | Client | Server |
|----------------|-----------------| |----------------|-----------------|
| send query 1 | | | send query 1 | |
| | process query 1 | | send query 2 | process query 1 |
| receive rows 1 | | | send query 3 | process query 2 |
| send query 2 | | | receive rows 1 | process query 3 |
| | process query 2 | | receive rows 2 | |
| receive rows 2 | | | receive rows 3 | |
| send query 3 | |
| | process query 3 |
| receive rows 3 | |
In both cases, the PostgreSQL server is executing the queries sequentially - pipelining just allows both sides of the connection to work concurrently when possible.
Pipelining happens automatically when futures are polled concurrently (for example, by using the futures join
combinator):
use diesel_async::RunQueryDsl;
let q1 = diesel::select(1_i32.into_sql::<Integer>());
let q2 = diesel::select(2_i32.into_sql::<Integer>());
// construct multiple futures for different queries
let f1 = q1.get_result::<i32>(conn);
let f2 = q2.get_result::<i32>(conn);
// wait on both results
let res = futures_util::try_join!(f1, f2)?;
assert_eq!(res.0, 1);
assert_eq!(res.1, 2);
§TLS
Connections created by AsyncPgConnection::establish
do not support TLS.
TLS support for tokio_postgres connections is implemented by external crates, e.g. tokio_postgres_rustls.
AsyncPgConnection::try_from_client_and_connection
can be used to construct a connection from an existing
tokio_postgres::Connection
with TLS enabled.
Implementations§
source§impl AsyncPgConnection
impl AsyncPgConnection
sourcepub fn build_transaction(&mut self) -> TransactionBuilder<'_, Self>
pub fn build_transaction(&mut self) -> TransactionBuilder<'_, Self>
Build a transaction, specifying additional details such as isolation level
See TransactionBuilder
for more examples.
conn.build_transaction()
.read_only()
.serializable()
.deferrable()
.run(|conn| async move { Ok(()) }.scope_boxed())
.await
sourcepub async fn try_from(conn: Client) -> ConnectionResult<Self>
pub async fn try_from(conn: Client) -> ConnectionResult<Self>
Construct a new AsyncPgConnection
instance from an existing tokio_postgres::Client
sourcepub async fn try_from_client_and_connection<S>(
client: Client,
conn: Connection<Socket, S>,
) -> ConnectionResult<Self>
pub async fn try_from_client_and_connection<S>( client: Client, conn: Connection<Socket, S>, ) -> ConnectionResult<Self>
Constructs a new AsyncPgConnection
from an existing tokio_postgres::Client
and
tokio_postgres::Connection
sourcepub fn cancel_token(&self) -> CancelToken
pub fn cancel_token(&self) -> CancelToken
Constructs a cancellation token that can later be used to request cancellation of a query running on the connection associated with this client.
Trait Implementations§
source§impl AsyncConnection for AsyncPgConnection
impl AsyncConnection for AsyncPgConnection
source§type LoadFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<<AsyncPgConnection as AsyncConnection>::Stream<'conn, 'query>, Error>> + Send + 'query>>
type LoadFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<<AsyncPgConnection as AsyncConnection>::Stream<'conn, 'query>, Error>> + Send + 'query>>
AsyncConnection::load
source§type ExecuteFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<usize, Error>> + Send + 'query>>
type ExecuteFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<usize, Error>> + Send + 'query>>
AsyncConnection::execute
source§type Stream<'conn, 'query> = Pin<Box<dyn Stream<Item = Result<PgRow, Error>> + Send>>
type Stream<'conn, 'query> = Pin<Box<dyn Stream<Item = Result<PgRow, Error>> + Send>>
AsyncConnection::load
source§type Row<'conn, 'query> = PgRow
type Row<'conn, 'query> = PgRow
AsyncConnection::load
source§fn establish<'life0, 'async_trait>(
database_url: &'life0 str,
) -> Pin<Box<dyn Future<Output = ConnectionResult<Self>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn establish<'life0, 'async_trait>(
database_url: &'life0 str,
) -> Pin<Box<dyn Future<Output = ConnectionResult<Self>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
source§fn set_instrumentation(&mut self, instrumentation: impl Instrumentation)
fn set_instrumentation(&mut self, instrumentation: impl Instrumentation)
Instrumentation
implementation for this connectionsource§fn transaction<'a, 'life0, 'async_trait, R, E, F>(
&'life0 mut self,
callback: F,
) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'async_trait>>
fn transaction<'a, 'life0, 'async_trait, R, E, F>( &'life0 mut self, callback: F, ) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'async_trait>>
source§fn begin_test_transaction<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn begin_test_transaction<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
source§impl Drop for AsyncPgConnection
impl Drop for AsyncPgConnection
source§impl SimpleAsyncConnection for AsyncPgConnection
impl SimpleAsyncConnection for AsyncPgConnection
source§fn batch_execute<'life0, 'life1, 'async_trait>(
&'life0 mut self,
query: &'life1 str,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn batch_execute<'life0, 'life1, 'async_trait>(
&'life0 mut self,
query: &'life1 str,
) -> Pin<Box<dyn Future<Output = QueryResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
source§impl<'b, Changes, Output, Tab, V> UpdateAndFetchResults<Changes, Output> for AsyncPgConnectionwhere
Output: Send,
Changes: Copy + AsChangeset<Target = Tab> + Send + Identifiable<Table = Tab>,
Tab: Table + FindDsl<Changes::Id> + 'b,
Find<Tab, Changes::Id>: IntoUpdateTarget<Table = Tab, WhereClause = V>,
UpdateStatement<Tab, V, Changes::Changeset>: AsQuery,
Update<Changes, Changes>: LoadQuery<'b, Self, Output>,
V: Send + 'b,
Changes::Changeset: Send + 'b,
Tab::FromClause: Send,
impl<'b, Changes, Output, Tab, V> UpdateAndFetchResults<Changes, Output> for AsyncPgConnectionwhere
Output: Send,
Changes: Copy + AsChangeset<Target = Tab> + Send + Identifiable<Table = Tab>,
Tab: Table + FindDsl<Changes::Id> + 'b,
Find<Tab, Changes::Id>: IntoUpdateTarget<Table = Tab, WhereClause = V>,
UpdateStatement<Tab, V, Changes::Changeset>: AsQuery,
Update<Changes, Changes>: LoadQuery<'b, Self, Output>,
V: Send + 'b,
Changes::Changeset: Send + 'b,
Tab::FromClause: Send,
source§fn update_and_fetch<'life0, 'async_trait>(
&'life0 mut self,
changeset: Changes,
) -> Pin<Box<dyn Future<Output = QueryResult<Output>> + Send + 'async_trait>>where
Changes: 'async_trait,
Changes::Changeset: 'async_trait,
Self: 'async_trait,
'life0: 'async_trait,
fn update_and_fetch<'life0, 'async_trait>(
&'life0 mut self,
changeset: Changes,
) -> Pin<Box<dyn Future<Output = QueryResult<Output>> + Send + 'async_trait>>where
Changes: 'async_trait,
Changes::Changeset: 'async_trait,
Self: 'async_trait,
'life0: 'async_trait,
Auto Trait Implementations§
impl Freeze for AsyncPgConnection
impl !RefUnwindSafe for AsyncPgConnection
impl Send for AsyncPgConnection
impl Sync for AsyncPgConnection
impl Unpin for AsyncPgConnection
impl !UnwindSafe for AsyncPgConnection
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoSql for T
impl<T> IntoSql for T
source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self
to an expression for Diesel’s query builder. Read moresource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self
to an expression for Diesel’s query builder. Read moresource§impl<T, Conn> RunQueryDsl<Conn> for T
impl<T, Conn> RunQueryDsl<Conn> for T
source§fn execute<'conn, 'query>(
self,
conn: &'conn mut Conn,
) -> Conn::ExecuteFuture<'conn, 'query>
fn execute<'conn, 'query>( self, conn: &'conn mut Conn, ) -> Conn::ExecuteFuture<'conn, 'query>
source§fn load<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> LoadFuture<'conn, 'query, Self, Conn, U>
fn load<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
source§fn load_stream<'conn, 'query, U>(
self,
conn: &'conn mut Conn,
) -> Self::LoadFuture<'conn>where
Conn: AsyncConnection,
U: 'conn,
Self: LoadQuery<'query, Conn, U> + 'query,
fn load_stream<'conn, 'query, U>(
self,
conn: &'conn mut Conn,
) -> Self::LoadFuture<'conn>where
Conn: AsyncConnection,
U: 'conn,
Self: LoadQuery<'query, Conn, U> + 'query,
source§fn get_result<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> GetResult<'conn, 'query, Self, Conn, U>
fn get_result<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> GetResult<'conn, 'query, Self, Conn, U>
source§fn get_results<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> LoadFuture<'conn, 'query, Self, Conn, U>
fn get_results<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
Vec
with the affected rows. Read more