//! Store-owned handles on the control-plane database: the pool ([`Db`]), an //! open transaction ([`Tx`]), and the [`Executor`] a store verb runs on, which //! is either of them. //! //! A crate above the storage layer holds a [`Db`], opens a [`Tx`] when it //! needs several statements to commit together, and passes either to store //! verbs. It never names the driver: none of these types exposes it, so a //! service or a coordinator that calls store verbs needs no `sqlx` //! dependency. //! //! A store verb takes `impl Executor<'_>` and hands the driver's executor to //! its statement through [`Executor::into_sqlx`]: //! //! ```no_run //! use pplx_space_postgres_pool::{Db, DbError, Executor}; //! //! // In a store, the one place `sqlx` is named. //! async fn ready_node_count(executor: impl Executor<'_>) -> Result { //! let count = sqlx::query_scalar::<_, i64>( //! "SELECT COUNT(*)::BIGINT FROM nodes WHERE state = 'ready'", //! ) //! .fetch_one(executor.into_sqlx()) //! .await?; //! Ok(count) //! } //! //! // In a service: the same verb on the pool or in a transaction. //! async fn caller(db: &Db) -> Result<(), DbError> { //! ready_node_count(db).await?; //! let mut tx = db.begin().await?; //! ready_node_count(&mut tx).await?; //! ready_node_count(&mut tx).await?; //! tx.commit().await //! } //! ``` //! //! # Configuring a transaction //! //! [`Db::begin_snapshot`] opens a read-only `REPEATABLE READ` transaction: //! every statement in it reads one snapshot, and any write to a //! non-temporary table fails. //! //! [`Tx`] sets timeouts for one transaction: //! //! - [`Tx::set_local_lock_timeout`], [`Tx::set_local_statement_timeout`] and //! [`Tx::set_local_idle_in_transaction_timeout`] set `lock_timeout`, //! `statement_timeout` and `idle_in_transaction_session_timeout`. //! - [`Tx::lower_local_lock_timeout`] bounds the lock waits that follow by at //! most a duration without raising a shorter bound already in force, and //! [`Tx::restore_local_lock_timeout`] puts back the value it replaced, so a //! tight bound can cover a few statements rather than the whole //! transaction. //! //! These timeouts are set on the open transaction and last until it commits //! or rolls back, as `SET LOCAL` does; the connection then goes back to the //! pool with its session values. On a savepoint ([`Tx::savepoint`]), a //! timeout is undone if that savepoint, or one enclosing it, rolls back, and //! otherwise lasts until the top-level transaction ends. A pool-wide //! `statement_timeout` is [`DbOptions::statement_timeout`]. //! //! The setters and the lowering take a [`Duration`] and send it in whole //! milliseconds, rounded down. Debug builds panic on a duration under 1 ms, //! and release builds send it as `1ms`, since `0ms` would mean no bound. A //! duration must be at most 2147483647 ms (about 24.8 days); Postgres can //! refuse a longer one, which aborts the transaction. //! //! A caller still holding the driver's transaction reaches the same methods //! through [`TxSettings`]. //! //! ```no_run //! use std::time::Duration; //! //! use pplx_space_postgres_pool::{Db, DbError}; //! //! async fn caller(db: &Db) -> Result<(), DbError> { //! let mut tx = db.begin().await?; //! tx.set_local_statement_timeout(Duration::from_secs(5)).await?; //! let saved = tx.lower_local_lock_timeout(Duration::from_millis(250)).await?; //! // ... statements whose lock waits must stay short ... //! tx.restore_local_lock_timeout(saved).await?; //! tx.commit().await //! } //! ``` //! //! # Savepoints //! //! [`Tx::savepoint`] opens a savepoint in a transaction as a nested [`Tx`], //! so a step that may fail can be undone without losing the transaction: //! after a failed statement in it, roll it back and the outer transaction //! carries on as it was before the savepoint. It issues the statements the //! driver's nested transactions do: `SAVEPOINT _sqlx_savepoint_N` to open, //! `RELEASE SAVEPOINT` on [`Tx::commit`], and `ROLLBACK TO SAVEPOINT` on //! [`Tx::rollback`] or drop. //! //! ```no_run //! use pplx_space_postgres_pool::{Db, DbError, DbErrorKind, Executor}; //! //! // A store verb whose insert can conflict. //! async fn insert_widget(executor: impl Executor<'_>, id: i64) -> Result<(), DbError> { //! sqlx::query("INSERT INTO widgets (id) VALUES ($1)") //! .bind(id) //! .execute(executor.into_sqlx()) //! .await?; //! Ok(()) //! } //! //! async fn caller(db: &Db) -> Result<(), DbError> { //! let mut tx = db.begin().await?; //! let mut savepoint = tx.savepoint().await?; //! match insert_widget(&mut savepoint, 1).await { //! Ok(()) => savepoint.commit().await?, //! Err(error) if error.kind() == DbErrorKind::UniqueViolation => { //! savepoint.rollback().await?; //! } //! Err(error) => return Err(error), //! } //! // ... statements that run whether or not the insert conflicted ... //! tx.commit().await //! } //! ``` use std::future::Future; use std::num::NonZeroU32; use std::pin::Pin; use std::time::Duration; use futures_core::Stream; use pplx_space_sql_locator::sql; use sqlx::postgres::{PgQueryResult, PgRow, PgStatement, PgTypeInfo}; use sqlx::{Describe, Either, Execute, PgConnection, PgPool, Postgres, SqlStr}; use crate::DbError; /// The control-plane database pool. /// /// Cheap to clone: clones share one pool. `From` wraps the caller's /// pool; a pool from this crate's connect functions keeps their pool /// configuration, per-connection setup, metrics and database identity. #[derive(Clone, Debug)] pub struct Db { pool: PgPool, } /// For the transition while services still hold the pool the connect /// functions return: wrap it, sharing the pool rather than opening another. impl From for Db { fn from(pool: PgPool) -> Self { Self { pool } } } impl Db { /// Begin a transaction on a connection from the pool. pub async fn begin(&self) -> Result, DbError> { Ok(Tx { inner: self.pool.begin().await?, }) } /// Begin a transaction that reads one snapshot and writes nothing: /// `BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY`. Every statement in /// it sees the database as of its first statement, and a write to a /// non-temporary table fails with `25006` (`read_only_sql_transaction`). pub async fn begin_snapshot(&self) -> Result, DbError> { Ok(Tx { inner: self .pool .begin_with(sql!("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY")) .await?, }) } } /// What applies to every connection in a pool; what applies to one /// transaction belongs on [`Tx`]. The default is the shared pool /// configuration [`crate::connect_postgres_lazy_as`] documents. /// /// Both settings exist for *sideline* pools, which take them as /// [`crate::SidelineOptions`]; see it for why a sideline needs each. /// /// These settings do not decide the pool metrics reporter: a general pool's /// constructor starts it ([`crate::connect_space_user_pool_lazy`]), and a /// sideline's does not ([`crate::connect_space_user_sideline_pool_lazy`]). #[derive(Clone, Debug, Default)] pub struct DbOptions { /// Connection ceiling. `None`: the `POSTGRES_POOL_MAX_CONNECTIONS` default /// (`POSTGRES_CREDS_POOL_MAX_CONNECTIONS` for `space_creds`). When set, /// `min_connections` is clamped to it so a large /// `POSTGRES_POOL_MIN_CONNECTIONS` cannot exceed it. `NonZeroU32` for the /// reason [`crate::SidelineOptions::max_connections`] gives. pub max_connections: Option, /// Session `statement_timeout` for every connection, set once at connect. /// `None`: the role's. Sent as the [`Tx`] setters send theirs: in whole /// milliseconds, rounded down. Debug builds panic on a duration under /// 1 ms, and release builds send it as `1ms`, since `0ms` would mean no /// `statement_timeout` at all rather than the role's. /// /// That is the piece a connection cap cannot provide: the cap bounds how /// much of the database's capacity a blocked sideline can hold, but each /// blocked statement still runs to the *role's* `statement_timeout`, and /// its caller waits that long. A pool-scoped setting cancels server-side — /// no abandoned statement, no dropped future, and no effect on any other /// pool using the same role. pub statement_timeout: Option, } /// An open transaction from [`Db::begin`] or [`Db::begin_snapshot`], or a /// savepoint in one from [`Tx::savepoint`]. /// /// Pass `&mut tx` to store verbs to run them inside it; a function holding /// `tx: &mut Tx` passes `&mut *tx`, so each verb borrows it afresh. Dropping /// it without [`Tx::commit`] rolls it back. #[derive(Debug)] pub struct Tx<'c> { inner: sqlx::Transaction<'c, Postgres>, } impl Tx<'_> { /// Commit the transaction, or release the savepoint, keeping its writes /// in the transaction around it. pub async fn commit(self) -> Result<(), DbError> { Ok(self.inner.commit().await?) } /// Roll the transaction back, or roll back to the savepoint. pub async fn rollback(self) -> Result<(), DbError> { Ok(self.inner.rollback().await?) } /// Open a savepoint in this transaction, returned as a nested `Tx` that /// borrows this one until it ends: `SAVEPOINT _sqlx_savepoint_N`, the /// driver's nested transaction, `N` counting the transactions open on /// the connection. /// /// [`Tx::commit`] on it releases the savepoint (`RELEASE SAVEPOINT`), /// keeping its writes until this transaction commits or rolls back. /// [`Tx::rollback`] on it issues `ROLLBACK TO SAVEPOINT`, undoing its /// writes; dropping it queues that statement, which the driver sends on /// the connection's next use. After a statement in it fails without /// closing the connection, roll it back or drop it: this transaction is /// then usable again. /// /// A timeout set on it is undone if it, or a savepoint enclosing it, /// rolls back; otherwise it lasts until the top-level transaction ends, /// so releasing the savepoint keeps it. /// /// If this returns an error, or its future is dropped before it /// completes, the driver has also queued a rollback of this transaction /// (of this savepoint, if it is one), and this `Tx` and any `Tx` /// enclosing it no longer match the connection's state: drop them all /// without committing. pub async fn savepoint(&mut self) -> Result, DbError> { Ok(Tx { inner: sqlx::Acquire::begin(&mut self.inner).await?, }) } /// Bound each later lock wait in this transaction by `d`, until it ends: /// `lock_timeout`, set as `SET LOCAL` does. A wait past it fails with /// `55P03` (`lock_not_available`). /// On a savepoint, it lasts as [`Tx::savepoint`] describes. /// /// `d` is sent in whole milliseconds, rounded down. Debug builds panic on /// a duration under 1 ms, and release builds send it as `1ms`, since /// `0ms` would mean no bound. `d` must be at most 2147483647 ms (about /// 24.8 days); Postgres can refuse a longer one, which aborts the /// transaction. pub async fn set_local_lock_timeout(&mut self, d: Duration) -> Result<(), DbError> { set_local_lock_timeout(&mut self.inner, d).await } /// Bound each later statement in this transaction by `d`, until it ends: /// `statement_timeout`, set as `SET LOCAL` does. A statement past it is /// cancelled with `57014` (`query_canceled`). /// On a savepoint, it lasts as [`Tx::savepoint`] describes. /// /// `d` is sent in whole milliseconds, rounded down. Debug builds panic on /// a duration under 1 ms, and release builds send it as `1ms`, since /// `0ms` would mean no bound. `d` must be at most 2147483647 ms (about /// 24.8 days); Postgres can refuse a longer one, which aborts the /// transaction. pub async fn set_local_statement_timeout(&mut self, d: Duration) -> Result<(), DbError> { set_local_statement_timeout(&mut self.inner, d).await } /// Bound how long this transaction may sit idle between statements by /// `d`, until it ends: `idle_in_transaction_session_timeout`, set as /// `SET LOCAL` does. Past it the server ends the session. /// On a savepoint, it lasts as [`Tx::savepoint`] describes. /// /// `d` is sent in whole milliseconds, rounded down. Debug builds panic on /// a duration under 1 ms, and release builds send it as `1ms`, since /// `0ms` would mean no bound. `d` must be at most 2147483647 ms (about /// 24.8 days); Postgres can refuse a longer one, which aborts the /// transaction. pub async fn set_local_idle_in_transaction_timeout( &mut self, d: Duration, ) -> Result<(), DbError> { set_local_idle_in_transaction_timeout(&mut self.inner, d).await } /// Bound each later lock wait in this transaction by at most `d`, never /// raising a shorter bound already in force for the transaction or the /// session. Returns the `lock_timeout` it found, for /// [`Tx::restore_local_lock_timeout`]; without that, the bound lasts /// until the transaction ends. /// On a savepoint, it lasts as [`Tx::savepoint`] describes. /// /// `d` is sent in whole milliseconds, rounded down. Debug builds panic on /// a duration under 1 ms, and release builds send it as `1ms`, since /// `0ms` would mean no bound. `d` must be at most 2147483647 ms (about /// 24.8 days); Postgres can refuse a longer one, which aborts the /// transaction. pub async fn lower_local_lock_timeout( &mut self, d: Duration, ) -> Result { lower_local_lock_timeout(&mut self.inner, d).await } /// Put back, for the rest of this transaction, the `lock_timeout` that /// [`Tx::lower_local_lock_timeout`] replaced. Like any statement, it fails /// with `25P02` once a failed statement has aborted the transaction. pub async fn restore_local_lock_timeout( &mut self, saved: SavedLockTimeout, ) -> Result<(), DbError> { restore_local_lock_timeout(&mut self.inner, saved).await } } /// The `lock_timeout` [`Tx::lower_local_lock_timeout`] replaced, to hand to /// [`Tx::restore_local_lock_timeout`]. #[derive(Debug)] #[must_use = "pass it to `restore_local_lock_timeout`, or the lowered bound lasts until the transaction ends"] pub struct SavedLockTimeout(String); /// [`Tx`]'s settings methods, for the transition: a caller still holding an /// open `sqlx` transaction calls them on it, as `tx.set_local_lock_timeout(d)` /// on a `sqlx::Transaction` or on the `&mut PgConnection` it derefs to. /// /// Sealed: only this crate implements it, for [`Tx`] and the driver's /// connection, so a helper during the transition can take either. The /// connection implementation, and with it this trait, go when the last such /// caller converts to [`Tx`], whose own methods stay. /// /// On a connection with no transaction open, each method returns an error /// and sets nothing. On a nested `sqlx` transaction (a savepoint), a value /// set inside it is undone if that savepoint, or one enclosing it, rolls /// back, and otherwise lasts until the top-level transaction ends. pub trait TxSettings: sealed::Sealed { /// [`Tx::set_local_lock_timeout`]. fn set_local_lock_timeout( &mut self, d: Duration, ) -> impl Future> + Send; /// [`Tx::set_local_statement_timeout`]. fn set_local_statement_timeout( &mut self, d: Duration, ) -> impl Future> + Send; /// [`Tx::set_local_idle_in_transaction_timeout`]. fn set_local_idle_in_transaction_timeout( &mut self, d: Duration, ) -> impl Future> + Send; /// [`Tx::lower_local_lock_timeout`]. fn lower_local_lock_timeout( &mut self, d: Duration, ) -> impl Future> + Send; /// [`Tx::restore_local_lock_timeout`]. fn restore_local_lock_timeout( &mut self, saved: SavedLockTimeout, ) -> impl Future> + Send; } impl TxSettings for Tx<'_> { fn set_local_lock_timeout( &mut self, d: Duration, ) -> impl Future> + Send { set_local_lock_timeout(&mut self.inner, d) } fn set_local_statement_timeout( &mut self, d: Duration, ) -> impl Future> + Send { set_local_statement_timeout(&mut self.inner, d) } fn set_local_idle_in_transaction_timeout( &mut self, d: Duration, ) -> impl Future> + Send { set_local_idle_in_transaction_timeout(&mut self.inner, d) } fn lower_local_lock_timeout( &mut self, d: Duration, ) -> impl Future> + Send { lower_local_lock_timeout(&mut self.inner, d) } fn restore_local_lock_timeout( &mut self, saved: SavedLockTimeout, ) -> impl Future> + Send { restore_local_lock_timeout(&mut self.inner, saved) } } impl TxSettings for PgConnection { fn set_local_lock_timeout( &mut self, d: Duration, ) -> impl Future> + Send { set_local_lock_timeout(self, d) } fn set_local_statement_timeout( &mut self, d: Duration, ) -> impl Future> + Send { set_local_statement_timeout(self, d) } fn set_local_idle_in_transaction_timeout( &mut self, d: Duration, ) -> impl Future> + Send { set_local_idle_in_transaction_timeout(self, d) } fn lower_local_lock_timeout( &mut self, d: Duration, ) -> impl Future> + Send { lower_local_lock_timeout(self, d) } fn restore_local_lock_timeout( &mut self, saved: SavedLockTimeout, ) -> impl Future> + Send { restore_local_lock_timeout(self, saved) } } /// `d` as a setting value, in whole milliseconds and at least `1ms`: `0ms` /// would mean no bound. pub(crate) fn setting_millis(d: Duration) -> String { debug_assert!( d.as_millis() != 0, "a timeout under 1 ms would render as `0ms`, which Postgres reads as no bound: {d:?}" ); let millis = d.as_millis().max(1); format!("{millis}ms") } /// Outside a transaction, a transaction-local setting would last for its /// own statement only. fn require_transaction(connection: &PgConnection) -> Result<(), DbError> { if sqlx::Connection::is_in_transaction(connection) { Ok(()) } else { Err(DbError::from(sqlx::Error::InvalidArgument( "a transaction-local setting needs an open transaction".to_owned(), ))) } } async fn set_local_lock_timeout(connection: &mut PgConnection, d: Duration) -> Result<(), DbError> { require_transaction(connection)?; sqlx::query(sql!("SELECT set_config('lock_timeout', $1, TRUE)")) .bind(setting_millis(d)) .execute(connection) .await?; Ok(()) } async fn set_local_statement_timeout( connection: &mut PgConnection, d: Duration, ) -> Result<(), DbError> { require_transaction(connection)?; sqlx::query(sql!("SELECT set_config('statement_timeout', $1, TRUE)")) .bind(setting_millis(d)) .execute(connection) .await?; Ok(()) } async fn set_local_idle_in_transaction_timeout( connection: &mut PgConnection, d: Duration, ) -> Result<(), DbError> { require_transaction(connection)?; sqlx::query(sql!( "SELECT set_config('idle_in_transaction_session_timeout', $1, TRUE)" )) .bind(setting_millis(d)) .execute(connection) .await?; Ok(()) } async fn lower_local_lock_timeout( connection: &mut PgConnection, d: Duration, ) -> Result { require_transaction(connection)?; let millis = setting_millis(d); let saved: String = sqlx::query_scalar(sql!("SELECT current_setting('lock_timeout')")) .fetch_one(&mut *connection) .await?; // Sets nothing when a bound at least as short is in force; `0` is none. sqlx::query(sql!( "SELECT set_config('lock_timeout', $1::text, TRUE) WHERE current_setting('lock_timeout')::interval = interval '0' OR current_setting('lock_timeout')::interval > $1::text::interval" )) .bind(millis) .execute(connection) .await?; Ok(SavedLockTimeout(saved)) } async fn restore_local_lock_timeout( connection: &mut PgConnection, saved: SavedLockTimeout, ) -> Result<(), DbError> { require_transaction(connection)?; sqlx::query(sql!("SELECT set_config('lock_timeout', $1, TRUE)")) .bind(saved.0) .execute(connection) .await?; Ok(()) } mod sealed { pub trait Sealed {} impl Sealed for &super::Db {} impl Sealed for &mut super::Tx<'_> {} impl Sealed for &sqlx::PgPool {} impl Sealed for &mut sqlx::PgConnection {} impl Sealed for super::Tx<'_> {} impl Sealed for sqlx::PgConnection {} } /// Where a store verb runs its statements: the pool (`&Db`) or an open /// transaction (`&mut Tx`). /// /// Sealed: only this crate implements it. Besides [`Db`] and [`Tx`], it is /// implemented for the driver's pool and connection for the transition, so a /// store verb can switch to `impl Executor<'_>` while some of its callers /// still hold a `sqlx` pool or pass an open `sqlx` transaction as /// `&mut *tx`. Those two implementations go once no caller needs them. pub trait Executor<'c>: sealed::Sealed + Send { /// The driver's executor, for a store verb's statement. Only stores /// call it: [`SqlxExecutor`] is for passing to `sqlx`. fn into_sqlx(self) -> SqlxExecutor<'c>; } impl<'c> Executor<'c> for &'c Db { fn into_sqlx(self) -> SqlxExecutor<'c> { SqlxExecutor(SqlxExecutorInner::Pool(&self.pool)) } } impl<'c> Executor<'c> for &'c mut Tx<'_> { fn into_sqlx(self) -> SqlxExecutor<'c> { SqlxExecutor(SqlxExecutorInner::Connection(&mut self.inner)) } } impl<'c> Executor<'c> for &'c PgPool { fn into_sqlx(self) -> SqlxExecutor<'c> { SqlxExecutor(SqlxExecutorInner::Pool(self)) } } impl<'c> Executor<'c> for &'c mut PgConnection { fn into_sqlx(self) -> SqlxExecutor<'c> { SqlxExecutor(SqlxExecutorInner::Connection(self)) } } /// The driver's executor behind an [`Executor`]: the pool or one connection. /// /// One concrete type rather than a type per [`Executor`] implementation (an /// associated type). A store verb's future holds it across an await, and the /// compiler proves such a future `Send`, as `tokio::spawn` requires, for every /// lifetime its borrows could have. It can do that for a concrete type, as for /// `&mut PgConnection` itself, but not for an associated type of an /// implementation that holds only for related lifetimes, as the one for /// `&'c mut Tx` must: a verb run on a borrowed `Tx` inside a spawned task /// would then not compile. #[derive(Debug)] pub struct SqlxExecutor<'c>(SqlxExecutorInner<'c>); #[derive(Debug)] enum SqlxExecutorInner<'c> { Pool(&'c PgPool), Connection(&'c mut PgConnection), } type PgRowStream<'e, T> = Pin> + Send + 'e>>; type PgFuture<'e, T> = Pin> + Send + 'e>>; // Each method is one the pool and the connection implement themselves; the // rest are the trait's defaults built on these, as they are for both. impl<'c> sqlx::Executor<'c> for SqlxExecutor<'c> { type Database = Postgres; fn fetch_many<'e, 'q: 'e, E>(self, query: E) -> PgRowStream<'e, Either> where 'c: 'e, E: 'q + Execute<'q, Postgres>, { match self.0 { SqlxExecutorInner::Pool(pool) => pool.fetch_many(query), SqlxExecutorInner::Connection(connection) => connection.fetch_many(query), } } fn fetch_optional<'e, 'q: 'e, E>(self, query: E) -> PgFuture<'e, Option> where 'c: 'e, E: 'q + Execute<'q, Postgres>, { match self.0 { SqlxExecutorInner::Pool(pool) => pool.fetch_optional(query), SqlxExecutorInner::Connection(connection) => connection.fetch_optional(query), } } fn prepare_with<'e>( self, sql: SqlStr, parameters: &'e [PgTypeInfo], ) -> PgFuture<'e, PgStatement> where 'c: 'e, { match self.0 { SqlxExecutorInner::Pool(pool) => pool.prepare_with(sql, parameters), SqlxExecutorInner::Connection(connection) => connection.prepare_with(sql, parameters), } } fn describe<'e>(self, sql: SqlStr) -> PgFuture<'e, Describe> where 'c: 'e, { match self.0 { SqlxExecutorInner::Pool(pool) => pool.describe(sql), SqlxExecutorInner::Connection(connection) => connection.describe(sql), } } } #[cfg(test)] mod tests;