Struct PostgresProjectionStore
#[non_exhaustive]pub struct PostgresProjectionStore { /* private fields */ }projection-store only.Expand description
A Postgres-backed projection store.
Implementations§
§impl PostgresProjectionStore
impl PostgresProjectionStore
pub fn new(pool: PgPool) -> Self
pub fn new(pool: PgPool) -> Self
Wraps an existing pool.
Sharing one pool with
PostgresEventStore is the
expected deployment, which is why neither type builds its own.
pub fn pool(&self) -> &PgPool
pub fn pool(&self) -> &PgPool
The pool this store writes through.
Trait Implementations§
§impl Clone for PostgresProjectionStore
impl Clone for PostgresProjectionStore
§impl Debug for PostgresProjectionStore
impl Debug for PostgresProjectionStore
§impl ProjectionProbe for PostgresProjectionStore
The conformance suite’s write and read seam.
impl ProjectionProbe for PostgresProjectionStore
The conformance suite’s write and read seam.
It lives here, in src/, and not in tests/projection.rs: that is a
different crate, where neither ProjectionProbe nor
PostgresProjectionStore is local and the orphan rule answers
error[E0117].
§const READS_THROUGH_BATCH: bool = false
const READS_THROUGH_BATCH: bool = false
false, and it is the honest answer rather than a gap.
A PostgresProjectionBatch is a list of statements that has been sent
to Postgres exactly never, so there is no open transaction to read
through — and answering from committed state is what PS-12 forbids by
name. The two rules that would have used it are emitted as reported skips
rather than omitted.
§async fn probe_write(
&self,
batch: &mut Self::Batch,
key: &str,
value: u64,
) -> Result<(), Self::Error>
async fn probe_write( &self, batch: &mut Self::Batch, key: &str, value: u64, ) -> Result<(), Self::Error>
Queues one probe row. Never awaits and never fails, because queueing a statement into a buffer the caller owns cannot fail.
§async fn probe_delete_all(
&self,
batch: &mut Self::Batch,
) -> Result<(), Self::Error>
async fn probe_delete_all( &self, batch: &mut Self::Batch, ) -> Result<(), Self::Error>
Queues removal of every probe row, so that
reset can be checked
without the suite knowing what a read model is.
§async fn probe_read(&self, key: &str) -> Result<Option<u64>, Self::Error>
async fn probe_read(&self, key: &str) -> Result<Option<u64>, Self::Error>
Reads one probe row from committed state.
§Errors
PostgresProjectionStoreError::Driver if the pool or the server
rejected the read.
§async fn probe_read_through(
&self,
_batch: &mut Self::Batch,
_key: &str,
) -> Result<Option<u64>, Self::Error>
async fn probe_read_through( &self, _batch: &mut Self::Batch, _key: &str, ) -> Result<Option<u64>, Self::Error>
Never called, because
READS_THROUGH_BATCH
is false.
§Panics
Always. The specification permits exactly this for an adapter whose batch has no read path, and the panic is the honest answer: any value returned here would be a claim about pending writes Postgres has never been told about.
§impl SendProjectionStore for PostgresProjectionStore
impl SendProjectionStore for PostgresProjectionStore
§type Batch = PostgresProjectionBatch
type Batch = PostgresProjectionBatch
An owned buffered write set. See the module documentation for why
this is not sqlx::Transaction<'static, Postgres>, which is what this
line said while every body below was todo!().
§async fn begin(&self) -> Result<Self::Batch, Self::Error>
async fn begin(&self) -> Result<Self::Batch, Self::Error>
Never awaits and never fails: minting an empty Vec and copying a u64
touches no connection. The signature is async and fallible for the
live store’s sake, and this body is what PS-6’s discipline looks like on
a buffer.
§async fn checkpoint(&self, id: &ProjectionId) -> Result<Checkpoint, Self::Error>
async fn checkpoint(&self, id: &ProjectionId) -> Result<Checkpoint, Self::Error>
Reads id’s checkpoint, or Checkpoint::NeverRun when it has no row.
The absence of a row is the NeverRun state, which is why a
successful reset deletes rather than writing a sentinel:
a sentinel position would make a reset indistinguishable from a commit at
the first position.
§Errors
PostgresProjectionStoreError::Driverif the pool or the server rejected the read;CheckpointOutOfRangeorInvalidAuthorityif the row is one this adapter could not have written.
§async fn commit(
&self,
batch: Self::Batch,
id: &ProjectionId,
position: SequencePosition,
authority: Authority,
) -> Result<(), CommitError<Self::Error>>
async fn commit( &self, batch: Self::Batch, id: &ProjectionId, position: SequencePosition, authority: Authority, ) -> Result<(), CommitError<Self::Error>>
Applies batch and advances id’s checkpoint to position, as one unit.
§How the regression guard is built, and why not the obvious way
The upsert carries its own guard — ON CONFLICT … DO UPDATE … WHERE projection_checkpoint.position <= excluded.position — so two runners
racing on one ProjectionId are serialised by the row lock ON CONFLICT DO UPDATE takes, without this adapter holding a lock of its own.
happenstance-sqlite reads the recorded position first and relies on a
global write lock; Postgres has no such lock, which is the point of this
crate.
A guarded upsert reports a regression as zero rows and cannot say what
the recorded position was: RETURNING on a DO UPDATE yields the new
row, and Postgres exposes no OLD there. So the recorded value is re-read
inside the same transaction before the error is built —
CommitError::CheckpointRegression carries both values so a caller can
log the gap rather than spend a second round trip on it.
The transaction is rolled back on that path, and that is not bookkeeping: the caller’s statements are already in it, and a refused commit that left the read model written would be a partial application with no checkpoint to record it.
§Errors
CommitError::ForeignBatchifbatchwas begun on a different store instance, decided before any statement is prepared;CommitError::CheckpointRegressionifpositionis below the one recorded — equal is accepted, because a batch that wrote nothing new still records that its events were considered;CommitError::StorecarryingParameterCountif a statement’s values do not match the highest$nits text uses, decided before the transaction opens;CommitError::Storeif the driver or the server failed.
§async fn reset(
&self,
batch: Self::Batch,
id: &ProjectionId,
) -> Result<(), ResetError<Self::Error>>
async fn reset( &self, batch: Self::Batch, id: &ProjectionId, ) -> Result<(), ResetError<Self::Error>>
Applies batch and returns id to Checkpoint::NeverRun, as one unit.
commit’s dual. It deletes nothing this adapter chose: the read model is
the caller’s, and an adapter that truncated a table of its own naming
would be scoped to something the port deliberately never told it.
§Errors
ResetError::ForeignBatchifbatchwas begun on a different store instance;ResetError::StorecarryingParameterCountif a statement’s values do not match the highest$nits text uses, decided before the transaction opens;ResetError::Storeif the driver or the server failed.
ResetError::Refused is never returned: this store holds no protection
policy, which is PS-18’s mechanism left unexercised by a store with
nothing to protect.
§async fn rollback(&self, batch: Self::Batch) -> Result<(), Self::Error>
async fn rollback(&self, batch: Self::Batch) -> Result<(), Self::Error>
Discards the batch without committing.
Nothing was ever sent to Postgres, so this is a drop — which is also the evidence PS-7 asks for: for a buffering adapter, “dropping a batch must roll back” is free, and the store is usable afterwards because no connection was ever checked out to return.
§Errors
PostgresProjectionStoreError::ForeignBatch if batch was begun on a
different store instance. The batch is consumed either way; the refusal is
how a caller learns it was holding the wrong one, on the call that was
meant to be the cleanup.
§type Error = PostgresProjectionStoreError
type Error = PostgresProjectionStoreError
Auto Trait Implementations§
impl !RefUnwindSafe for PostgresProjectionStore
impl !UnwindSafe for PostgresProjectionStore
impl Freeze for PostgresProjectionStore
impl Send for PostgresProjectionStore
impl Sync for PostgresProjectionStore
impl Unpin for PostgresProjectionStore
impl UnsafeUnpin for PostgresProjectionStore
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> 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 more