pub struct PostgresStateBackend { /* private fields */ }Expand description
PostgreSQL state backend
This backend uses PostgreSQL for persistent, distributed state management. It provides ACID guarantees, connection pooling, automatic migrations, and advanced features like JSONB metadata and checkpointing.
Implementations§
Source§impl PostgresStateBackend
impl PostgresStateBackend
Sourcepub async fn new(config: PostgresConfig) -> StateResult<Self>
pub async fn new(config: PostgresConfig) -> StateResult<Self>
Create a new PostgreSQL state backend
This initializes the connection pool and runs automatic schema migrations.
§Arguments
config- PostgreSQL configuration
§Returns
Ok(PostgresStateBackend)- Successfully initialized backendErr(StateError)- Failed to initialize
§Example
let config = PostgresConfig::new("postgresql://localhost/test");
let backend = PostgresStateBackend::new(config).await?;Sourcepub async fn put_with_metadata(
&self,
key: &[u8],
value: &[u8],
metadata: Value,
) -> StateResult<()>
pub async fn put_with_metadata( &self, key: &[u8], value: &[u8], metadata: Value, ) -> StateResult<()>
Sourcepub async fn put_with_ttl(
&self,
key: &[u8],
value: &[u8],
ttl: Duration,
) -> StateResult<()>
pub async fn put_with_ttl( &self, key: &[u8], value: &[u8], ttl: Duration, ) -> StateResult<()>
Put a value with TTL
§Arguments
key- The key to storevalue- The value to storettl- Time to live duration
Sourcepub async fn cleanup_expired(&self) -> StateResult<usize>
pub async fn cleanup_expired(&self) -> StateResult<usize>
Clean up expired entries
This removes all entries where expires_at is in the past.
§Returns
Number of entries deleted
Sourcepub async fn create_checkpoint(&self) -> StateResult<Uuid>
pub async fn create_checkpoint(&self) -> StateResult<Uuid>
Create a checkpoint of the current state
This creates a point-in-time snapshot of all state entries.
§Returns
Checkpoint UUID
Sourcepub async fn restore_checkpoint(&self, checkpoint_id: Uuid) -> StateResult<()>
pub async fn restore_checkpoint(&self, checkpoint_id: Uuid) -> StateResult<()>
Sourcepub async fn list_checkpoints(&self) -> StateResult<Vec<CheckpointInfo>>
pub async fn list_checkpoints(&self) -> StateResult<Vec<CheckpointInfo>>
List all checkpoints
Sourcepub async fn stats(&self) -> PostgresStats
pub async fn stats(&self) -> PostgresStats
Get backend statistics
Sourcepub async fn vacuum(&self) -> StateResult<()>
pub async fn vacuum(&self) -> StateResult<()>
Execute VACUUM on the state table
This reclaims storage and updates statistics for better query performance.
Sourcepub async fn table_stats(&self) -> StateResult<(i64, i64)>
pub async fn table_stats(&self) -> StateResult<(i64, i64)>
Get table size statistics
Sourcepub async fn batch_put(&self, entries: &[(&[u8], &[u8])]) -> StateResult<()>
pub async fn batch_put(&self, entries: &[(&[u8], &[u8])]) -> StateResult<()>
Batch put operation
Efficiently insert multiple key-value pairs in a single transaction.
Sourcepub async fn health_check(&self) -> StateResult<bool>
pub async fn health_check(&self) -> StateResult<bool>
Health check - verify database connectivity
Trait Implementations§
Source§impl StateBackend for PostgresStateBackend
impl StateBackend for PostgresStateBackend
Source§fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = StateResult<Option<Vec<u8>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = StateResult<Option<Vec<u8>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn put<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 [u8],
value: &'life2 [u8],
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn put<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 [u8],
value: &'life2 [u8],
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Source§fn delete<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn delete<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn list_keys<'life0, 'life1, 'async_trait>(
&'life0 self,
prefix: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = StateResult<Vec<Vec<u8>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn list_keys<'life0, 'life1, 'async_trait>(
&'life0 self,
prefix: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = StateResult<Vec<Vec<u8>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn clear<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn clear<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = StateResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Auto Trait Implementations§
impl Freeze for PostgresStateBackend
impl !RefUnwindSafe for PostgresStateBackend
impl Send for PostgresStateBackend
impl Sync for PostgresStateBackend
impl Unpin for PostgresStateBackend
impl !UnwindSafe for PostgresStateBackend
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
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.