Skip to main content

PostgresStoreBackend

Struct PostgresStoreBackend 

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

A Postgres-backed axonstore. Holds one lazy, bounded PgPool. Cheap to Clone (the pool is internally reference-counted).

Implementations§

Source§

impl PostgresStoreBackend

Source

pub fn connect(connection: &str) -> Result<Self, StoreError>

Resolve connection and build a lazy, bounded connection pool.

Equivalent to connect_named with no store name — the connection’s application_name is the bare axon-store. Prefer connect_named so each session is attributable to its declaring axonstore.

Source

pub fn connect_named( connection: &str, store_name: &str, ) -> Result<Self, StoreError>

Resolve connection and build a lazy, bounded connection pool, stamping each connection’s application_name with store_name.

Synchronous and cheap: the DSN is parsed into a PgConnectOptions (a malformed DSN is a typed StoreError::PoolInit) but connect_lazy_with opens no connection — the first real connection is made on the first operation (D7 — lazy).

Two production-grade properties are set on every connection:

  • statement_cache_capacity(0) (Gap 3) — disables sqlx’s named server-side prepared-statement cache so the backend is safe behind a transaction-mode pooler (PgBouncer pool_mode=transaction, Supabase Supavisor :6543, Neon, RDS Proxy), where a cached name minted on one physical session collides on the next (prepared statement "sqlx_s_1" already exists). Applied unconditionally — harmless on a direct connection, and there is no knob to misconfigure.
  • application_nameaxon-store/<store_name> (bare axon-store when store_name is empty), capped at the Postgres 63-byte NAMEDATALEN-1 limit on a char boundary, so every axon-owned session is identifiable in pg_stat_activity, pooler logs and DBA dashboards.

Must be called within a Tokio runtime context: a well-formed DSN registers a background connection reaper. In production this is always satisfied — the registry (35.d) is built while the axum server’s runtime is live.

Source

pub fn connect_named_with_namespace( connection: &str, store_name: &str, namespace: Option<&str>, ) -> Result<Self, StoreError>

§Fase 38.f (D3) — same as Self::connect_named but stamps an OPTIONAL per-tenant schema namespace into application_name.

connect_named_with_namespace("env:DB", "claims", Some("tenant_42")) produces a pool whose every session’s application_name reads axon-store/claims/tenant_42 — so a DBA reading pg_stat_activity, pooler logs, or RDS Performance Insights sees both the axonstore declaration AND the resolved tenant.

None for namespace is the pre-38 shape (axon-store/<store>, byte-identical to connect_named).

Source

pub fn masked_dsn(&self) -> String

The resolved DSN with its password masked — safe to log.

Source

pub fn pool(&self) -> &PgPool

The underlying pool — 35.i’s Stream<Row> borrows it.

Source

pub async fn acquire_pin(&self) -> Result<PoolConnection<Postgres>, StoreError>

§Fase 37.x.j (D1) — Acquire ONE physical Postgres connection from the pool to be held for the duration of a flow execution ([crate::runner::ExecContext] for the sync path, crate::flow_dispatcher::DispatchCtx for the async streaming path).

The returned sqlx::pool::PoolConnection is wrapped by the caller in crate::store::store_conn::StoreConn::Pinned and passed to every operation (query / insert / mutate / purge / ping) against this axonstore for the flow lifetime. Because every op runs against the same physical Postgres backend connection, a transaction-mode pooler (Supabase Supavisor, PgBouncer, Neon, RDS Proxy) cannot swap the backend between queries — the D3 “unnamed prepared statement does not exist” race that Fase 37.x.j closes.

The connection is released back to the pool on Drop of the returned PoolConnection. The existing after_release(DEALLOCATE ALL) hook (Fase 38.x.a D2) wipes any prepared statements before the conn is reused — composing cleanly with the per-flow pinning of 37.x.j.

Failure modes:

  • StoreError::Connect if the pool’s acquire_timeout elapses (no conn becomes available — pool exhausted or Postgres unreachable).
  • StoreError::Connect if the pool is in a permanently-bad state (TLS handshake failure, DNS resolution failure, etc.).
Source

pub async fn query( &self, conn: &mut StoreConn<'_>, table: &str, where_expr: &str, bindings: &HashMap<String, String>, ) -> Result<Vec<StoreRow>, StoreError>

retrieve — run SELECT * FROM "schema"."table" WHERE … and map every row to a JSON-safe StoreRow.

§Fase 37.x.d (D3) — on a cache MISS the schema introspection and the SELECT execute inside ONE transaction, so a transaction-mode pooler pins one physical backend for both — they cannot split across sessions. A cache HIT needs no transaction: the cached resolution is already correct and the SELECT is schema-qualified, so it resolves on any session.

v1.30.0 materializes the full result (fetch_all); 35.i adds the backpressured Stream<Row> variant (Pillar III).

Source

pub async fn insert( &self, conn: &mut StoreConn<'_>, table: &str, data: &[(String, SqlValue)], ) -> Result<u64, StoreError>

persist — run INSERT INTO "schema"."table" (…) VALUES (…). Returns the number of rows inserted. §Fase 37.x.d (D3) — on a cache MISS the resolution + the INSERT execute in ONE transaction; a cache HIT needs no transaction.

Source

pub async fn mutate( &self, conn: &mut StoreConn<'_>, table: &str, where_expr: &str, data: &[(String, SqlValue)], bindings: &HashMap<String, String>, ) -> Result<u64, StoreError>

mutate — run UPDATE "schema"."table" SET … WHERE …. Returns the number of rows affected. §Fase 37.x.d (D3) — on a cache MISS the resolution + the UPDATE execute in ONE transaction; a cache HIT needs no transaction.

Source

pub async fn purge( &self, conn: &mut StoreConn<'_>, table: &str, where_expr: &str, bindings: &HashMap<String, String>, ) -> Result<u64, StoreError>

purge — run DELETE FROM "schema"."table" WHERE …. Returns the number of rows deleted. §Fase 37.x.d (D3) — on a cache MISS the resolution + the DELETE execute in ONE transaction; a cache HIT needs no transaction.

Source

pub async fn ping(&self) -> Result<(), StoreError>

Verify database reachability with SELECT 1.

Trait Implementations§

Source§

impl Clone for PostgresStoreBackend

Source§

fn clone(&self) -> PostgresStoreBackend

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 PostgresStoreBackend

Source§

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

Formats the value using the given formatter. 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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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