Skip to main content

PostgresOptions

Struct PostgresOptions 

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

Where the store keeps its tables and how it talks to the database.

The defaults are what PostgresStore::connect uses: tables found through search_path, migration history in _sqlx_migrations, prepared statements cached per connection, and 10 connections.

Implementations§

Source§

impl PostgresOptions

Source

pub fn new() -> Self

The defaults.

Source

pub fn schema(self, schema: impl Into<String>) -> Self

Keeps the store’s tables in schema and names it in every query, so search_path does not matter. The migration history goes in schema._sqlx_migrations, apart from any other sqlx migrations in the database.

schema is a name, used exactly as written (case included), not SQL. migrate creates it if it does not exist.

Source

pub fn transaction_pooling(self, enabled: bool) -> Self

For a pool that reaches Postgres through PgBouncer, or another pooler, in transaction mode.

Such a pooler may hand each transaction, and each statement outside one, to a different server connection. So no statement is prepared for reuse, and every store operation runs inside a transaction, so that a statement’s parse, bind and execution all reach the same server. Reads that would otherwise run alone cost a BEGIN and a COMMIT more.

The pool is the application’s, so its connect options are too. Two of sqlx’s matter behind a pooler: turn off the extra_float_digits startup parameter (PgConnectOptions::extra_float_digits(None)) unless the pooler ignores it, and do not set search_path through PgConnectOptions::options, which a pooler does not pass on; use schema instead. The store has no float columns, so it does not depend on extra_float_digits.

Source

pub fn max_connections(self, max: u32) -> Self

The size of the pool connect and connect_with open; 10 by default. attach uses the pool it is given as it is.

Source

pub async fn connect(&self, url: &str) -> Result<PostgresStore, StoreError>

Connects to url, applies migrations, and attaches.

Migrating needs a direct connection (see migrate), so url must not go through a pooler in transaction mode; for that, migrate directly and attach to the pooled pool.

§Errors

StoreError::Backend if connecting or migrating fails.

Source

pub async fn connect_with( &self, options: PgConnectOptions, ) -> Result<PostgresStore, StoreError>

Connects with options, applies migrations, and attaches. Like connect, it needs a direct connection.

§Errors

StoreError::Backend if connecting or migrating fails.

Source

pub async fn migrate(&self, pool: &PgPool) -> Result<(), StoreError>

Applies the store’s migrations through pool.

pool must reach Postgres directly, not through a pooler in transaction mode: migrations hold a session-level advisory lock, so concurrent callers are safe. Through such a pooler the lock can be left held on a server connection, blocking every later migration in the database, the application’s included. With a schema, the connection used is closed afterwards instead of going back to the pool, so pool keeps its own search_path.

§Errors

StoreError::Backend if migrating fails, or if the history table holds a different migration under one of the store’s versions (as when it is shared with an application’s migrations; give the store a schema of its own).

Source

pub async fn attach(&self, pool: PgPool) -> Result<PostgresStore, StoreError>

Uses pool without migrating; pool may go through a pooler (see transaction_pooling for its connect options).

Checks that every migration this version needs has been applied, so a missing migrate fails here rather than on the first effect.

§Errors

StoreError::Backend if the database is unreachable or a migration is missing or not the store’s own.

Trait Implementations§

Source§

impl Clone for PostgresOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PostgresOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PostgresOptions

Source§

fn default() -> Self

Returns the “default value” for a 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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