Skip to main content

SnapshotManager

Struct SnapshotManager 

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

Central allocator and liveness tracker.

Uses an atomic counter for xid allocation and a parking_lot-guarded HashSet for in-progress/aborted bookkeeping. The sets stay small — only unfinished transactions plus a finite rollback history — so a plain HashSet outperforms more complex data structures here.

§Autocommit xid pool

Single-row autocommit writes (MutationEngine::append_one) need an xid that’s “born committed” — they call begin() then commit() back-to-back before the row is even durable. The pre-commit pool (autocommit_pool_*) batches the reservation: one next_xid.fetch_add(BATCH) reserves a contiguous range of xids, each handed out via a single atomic without touching the RwLock<ManagerState>. Pool xids are never inserted into active or aborted so they look like already-committed transactions to every snapshot — identical visibility semantics to the legacy begin()/commit() pair (which also leaves the xid in neither set).

Implementations§

Source§

impl SnapshotManager

Source

pub fn new() -> SnapshotManager

Source

pub fn begin(&self) -> u64

Allocate a new xid and mark it active. Returns the xid for stamping onto UnifiedEntity::xmin/xmax.

Source

pub fn snapshot(&self, xid: u64) -> Snapshot

Capture a point-in-time snapshot. Must be called after begin() when using SnapshotIsolation/Serializable. ReadCommitted refreshes this per statement via the same call.

Source

pub fn commit(&self, xid: u64)

Mark a transaction as committed. Its writes become visible to future snapshots; earlier snapshots keep their own view.

Source

pub fn allocate_committed_xid(&self) -> u64

Allocate an xid that is born committed — for autocommit callers (MutationEngine::append_one) that previously paid two state.write() lock acquisitions per row to insert-then-remove from the active set.

The returned xid is never inserted into active and never into aborted, which matches the steady state of the legacy begin()/commit() pair when called back-to-back: the xid leaves the manager’s tracking sets unobservably. Concurrent readers therefore see it as an already-committed transaction once xmin <= snapshot.xid, which is exactly the semantics the autocommit path needs.

Implementation: a small reservation pool (AUTOCOMMIT_POOL_BATCH xids) is reserved with one fetch_add. Each caller hands itself the next xid via a single atomic. When the pool drains, the next caller serialises briefly through autocommit_pool_refill to bump the window, then falls back into the lock-free hot path.

Durability note: this method does NOT make the row durable — it only allocates the identifier. The caller must complete the usual WAL-append + fsync cycle before acknowledging the write. Pre-allocating the xid is safe because the xid carries no promise that any row exists; it’s just a number for xmin.

Source

pub fn rollback(&self, xid: u64)

Mark a transaction as rolled back. Its writes MUST stay hidden from every future read — is_visible consults the aborted set before honouring a row’s xmin.

Source

pub fn is_aborted(&self, xid: u64) -> bool

Is this xid known to have rolled back? Called by the read path to skip tuples whose creator never committed.

Source

pub fn is_active(&self, xid: u64) -> bool

Is this xid still active?

Source

pub fn oldest_active_xid(&self) -> Option<u64>

Snapshot of every still-active xid (for VACUUM oldest-active-xid calculation — any row with xmax < min(active) is reclaimable).

Source

pub fn oldest_pinned_xid(&self) -> Option<u64>

Oldest externally pinned xid. Pinned snapshots behave like active snapshots for VACUUM: any tuple visible to that xid must survive even when no SQL transaction is currently active.

Source

pub fn peek_next_xid(&self) -> u64

Return the next xid that would be allocated. Useful for diagnostics and for VACUUM to know the upper bound of aborted-xid retention.

Source

pub fn observe_committed_xid(&self, xid: u64)

Advance the allocator so future snapshots consider an xid recovered from storage/WAL to be in the committed past.

Source

pub fn prune_aborted(&self, below: u64)

Prune the aborted-xid set. Safe to call once every aborted xid is below oldest_active, which guarantees no live snapshot depends on the distinction between “aborted” and “never existed”. Pinned xids are always retained so higher-level references (VCS commits, replica snapshots) stay readable.

Source

pub fn pin(&self, xid: u64)

Pin an xid so its row versions stay reclaim-safe across VACUUM. Reference-counted — call unpin once per pin to release.

Source

pub fn unpin(&self, xid: u64)

Decrement an xid’s pin count. At zero it is removed and becomes VACUUM-eligible again. No-op if the xid was never pinned.

Source

pub fn is_pinned(&self, xid: u64) -> bool

Is this xid currently pinned?

Source

pub fn pin_count(&self, xid: u64) -> u32

Current pin count for an xid (0 if not pinned). Diagnostic only.

Trait Implementations§

Source§

impl Default for SnapshotManager

Source§

fn default() -> SnapshotManager

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> 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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

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

impl<L> LayerExt<L> for L

Source§

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.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
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