Struct diesel_async::AsyncPgConnection
source · pub struct AsyncPgConnection { /* private fields */ }
postgres
only.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
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);
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 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
§type LoadFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<<AsyncPgConnection as AsyncConnection>::Stream<'conn, 'query>, Error>> + Send + 'query, Global>>
type LoadFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<<AsyncPgConnection as AsyncConnection>::Stream<'conn, 'query>, Error>> + Send + 'query, Global>>
AsyncConnection::load
§type ExecuteFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<usize, Error>> + Send + 'query, Global>>
type ExecuteFuture<'conn, 'query> = Pin<Box<dyn Future<Output = Result<usize, Error>> + Send + 'query, Global>>
AsyncConnection::execute
§type Stream<'conn, 'query> = Pin<Box<dyn Stream<Item = Result<PgRow, Error>> + Send, Global>>
type Stream<'conn, 'query> = Pin<Box<dyn Stream<Item = Result<PgRow, Error>> + Send, Global>>
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 transaction<'a, 'life0, 'async_trait, R, E, F>(
&'life0 mut self,
callback: F
) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'async_trait>>where
F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a + 'async_trait,
E: From<Error> + Send + 'a + 'async_trait,
R: Send + 'a + 'async_trait,
Self: 'async_trait,
'a: 'async_trait,
'life0: '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>>where F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a + 'async_trait, E: From<Error> + Send + 'a + 'async_trait, R: Send + 'a + 'async_trait, Self: 'async_trait, 'a: 'async_trait, 'life0: '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§fn test_transaction<'a, 'async_trait, R, E, F>(
&'a mut self,
f: F
) -> Pin<Box<dyn Future<Output = R> + Send + 'async_trait>>where
F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a + 'async_trait,
E: Debug + Send + 'a + 'async_trait,
R: Send + 'a + 'async_trait,
Self: 'a + 'async_trait,
'a: 'async_trait,
fn test_transaction<'a, 'async_trait, R, E, F>( &'a mut self, f: F ) -> Pin<Box<dyn Future<Output = R> + Send + 'async_trait>>where F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a + 'async_trait, E: Debug + Send + 'a + 'async_trait, R: Send + 'a + 'async_trait, Self: 'a + 'async_trait, 'a: '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 !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
§impl<T> Conv for T
impl<T> Conv for T
§impl<Choices> CoproductSubsetter<CNil, HNil> for Choices
impl<Choices> CoproductSubsetter<CNil, HNil> for Choices
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoSql for T
impl<T> IntoSql for T
source§fn into_sql<T>(self) -> Self::Expressionwhere
Self: AsExpression<T> + Sized,
T: SqlType + TypedExpressionType,
fn into_sql<T>(self) -> Self::Expressionwhere Self: AsExpression<T> + Sized, T: SqlType + TypedExpressionType,
self
to an expression for Diesel’s query builder. Read moresource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
T: SqlType + TypedExpressionType,
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere &'a Self: AsExpression<T>, T: SqlType + TypedExpressionType,
&self
to an expression for Diesel’s query builder. Read more§impl<T, U, I> LiftInto<U, I> for Twhere
U: LiftFrom<T, I>,
impl<T, U, I> LiftInto<U, I> for Twhere U: LiftFrom<T, I>,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_mut()
into the pipe
function.§impl<T> Pointable for T
impl<T> Pointable for T
source§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>where
Conn: AsyncConnection + Send,
Self: ExecuteDsl<Conn> + 'query,
fn execute<'conn, 'query>( self, conn: &'conn mut Conn ) -> Conn::ExecuteFuture<'conn, 'query>where Conn: AsyncConnection + Send, Self: ExecuteDsl<Conn> + 'query,
source§fn load<'query, 'conn, U>(
self,
conn: &'conn mut Conn
) -> LoadFuture<'conn, 'query, Self, Conn, U>where
U: Send,
Conn: AsyncConnection,
Self: LoadQuery<'query, Conn, U> + 'query,
fn load<'query, 'conn, U>( self, conn: &'conn mut Conn ) -> LoadFuture<'conn, 'query, Self, Conn, U>where U: Send, Conn: AsyncConnection, Self: LoadQuery<'query, Conn, U> + 'query,
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,
Stream
] with the returned rows. Read moresource§fn get_result<'query, 'conn, U>(
self,
conn: &'conn mut Conn
) -> GetResult<'conn, 'query, Self, Conn, U>where
U: Send + 'conn,
Conn: AsyncConnection,
Self: LoadQuery<'query, Conn, U> + 'query,
fn get_result<'query, 'conn, U>( self, conn: &'conn mut Conn ) -> GetResult<'conn, 'query, Self, Conn, U>where U: Send + 'conn, Conn: AsyncConnection, Self: LoadQuery<'query, Conn, U> + 'query,
source§fn get_results<'query, 'conn, U>(
self,
conn: &'conn mut Conn
) -> LoadFuture<'conn, 'query, Self, Conn, U>where
U: Send,
Conn: AsyncConnection,
Self: LoadQuery<'query, Conn, U> + 'query,
fn get_results<'query, 'conn, U>( self, conn: &'conn mut Conn ) -> LoadFuture<'conn, 'query, Self, Conn, U>where U: Send, Conn: AsyncConnection, Self: LoadQuery<'query, Conn, U> + 'query,
Vec
with the affected rows. Read more§impl<Source> Sculptor<HNil, HNil> for Source
impl<Source> Sculptor<HNil, HNil> for Source
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.