Skip to main content

PostgresBackend

Struct PostgresBackend 

Source
pub struct PostgresBackend {
    pool: Pool,
    table_name: String,
}
Expand description

PostgreSQL-based persistence backend.

Fields§

§pool: Pool

Connection pool.

§table_name: String

Table name for memories.

Implementations§

Source§

impl PostgresBackend

Source

const DEFAULT_POOL_MAX_SIZE: usize = 20

Default maximum connections in pool.

Source

pub fn new(connection_url: &str, table_name: impl Into<String>) -> Result<Self>

Creates a new PostgreSQL backend.

§Errors

Returns an error if the connection pool fails to initialize.

Source

pub fn with_pool_size( connection_url: &str, table_name: impl Into<String>, pool_max_size: Option<usize>, ) -> Result<Self>

Creates a new PostgreSQL backend with configurable pool size.

§Arguments
  • connection_url - PostgreSQL connection URL
  • table_name - Name of the table for storing memories
  • pool_max_size - Maximum connections in pool (defaults to 20)
§Errors

Returns an error if the connection pool fails to initialize after retries.

§Connection Retry (CHAOS-HIGH-003)

Connection establishment uses exponential backoff with jitter for transient failures. This handles scenarios where the database is starting up or temporarily unavailable.

Source

fn parse_connection_url(url: &str) -> Result<Config>

Parses the connection URL into a tokio-postgres config.

Source

fn host_to_string(h: &Host) -> String

Extracts host string from tokio-postgres Host.

Source

fn build_pool_config(config: &Config, pool_max_size: Option<usize>) -> Config

Builds a deadpool config from tokio-postgres config.

§Pool Configuration (HIGH-010, DB-M2)

Configures connection pool with safety limits:

  • Configurable max connections (defaults to 20, prevents pool exhaustion)
  • 5 second acquire timeout (prevents hanging on pool exhaustion)

Pool size can be configured via StorageBackendConfig.pool_max_size.

Source

pub fn with_defaults() -> Result<Self>

Creates a backend with default settings.

§Errors

Returns an error if the connection fails.

Source

fn block_on<F, T>(&self, f: F) -> Result<T>
where F: Future<Output = Result<T>>,

Runs a blocking operation on the async pool.

Source

fn run_migrations(&self) -> Result<()>

Runs migrations.

Source

async fn store_async(&self, memory: &Memory) -> Result<()>

Async implementation of store operation.

Source

async fn get_async(&self, id: &MemoryId) -> Result<Option<Memory>>

Async implementation of get operation.

Source

fn row_to_memory(row: &Row) -> Memory

Converts a database row to a Memory.

Source

async fn delete_async(&self, id: &MemoryId) -> Result<bool>

Async implementation of delete operation.

Source

async fn list_ids_async(&self) -> Result<Vec<MemoryId>>

Async implementation of list_ids operation.

Source

async fn get_batch_async(&self, ids: &[MemoryId]) -> Result<Vec<Memory>>

Async implementation of get_batch operation using single IN query.

Avoids N+1 queries by fetching all IDs in a single round-trip.

Trait Implementations§

Source§

impl PersistenceBackend for PostgresBackend

Source§

fn get_batch(&self, ids: &[MemoryId]) -> Result<Vec<Memory>>

Optimized batch retrieval using a single IN query (HIGH-PERF-002).

Source§

fn store(&self, memory: &Memory) -> Result<()>

Stores a memory. Read more
Source§

fn get(&self, id: &MemoryId) -> Result<Option<Memory>>

Retrieves a memory by ID. Read more
Source§

fn delete(&self, id: &MemoryId) -> Result<bool>

Deletes a memory by ID. Read more
Source§

fn list_ids(&self) -> Result<Vec<MemoryId>>

Lists all memory IDs. Read more
Source§

fn exists(&self, id: &MemoryId) -> Result<bool>

Checks if a memory exists. Read more
Source§

fn count(&self) -> Result<usize>

Returns the total count of memories. 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

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
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: 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, 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more