pub struct TransactionManager { /* private fields */ }Expand description
Manages all transactions in the database
Implementations§
Source§impl TransactionManager
impl TransactionManager
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new transaction manager
For new databases, use this constructor. For restoring from a persisted
database, use TransactionManager::with_start_txn_id() instead.
Sourcepub fn with_start_txn_id(start_txn_id: u64) -> Self
pub fn with_start_txn_id(start_txn_id: u64) -> Self
Create a new transaction manager with a specific starting transaction ID.
Used when restoring from a persisted database to maintain MVCC visibility.
The start_txn_id should be the value persisted from the previous session,
ensuring transaction IDs remain monotonically increasing across restarts.
For new databases, use TransactionManager::new() instead.
Sourcepub fn next_txn_id(&self) -> u64
pub fn next_txn_id(&self) -> u64
Get the current next transaction ID (for persistence)
This value should be persisted to ensure transaction IDs remain monotonically increasing across database restarts.
Sourcepub fn begin(&self, mode: TransactionMode) -> Transaction<'_>
pub fn begin(&self, mode: TransactionMode) -> Transaction<'_>
Begin a new transaction with default configuration
Sourcepub fn begin_with_config(
&self,
mode: TransactionMode,
config: TransactionConfig,
) -> Transaction<'_>
pub fn begin_with_config( &self, mode: TransactionMode, config: TransactionConfig, ) -> Transaction<'_>
Begin a new transaction with specific configuration
Sourcepub fn begin_read_only(&self) -> Transaction<'_>
pub fn begin_read_only(&self) -> Transaction<'_>
Begin a read-only transaction
Sourcepub fn begin_read_write(&self) -> Transaction<'_>
pub fn begin_read_write(&self) -> Transaction<'_>
Begin a read-write transaction
Sourcepub fn commit_txn(&self, txn_id: TransactionId) -> Result<()>
pub fn commit_txn(&self, txn_id: TransactionId) -> Result<()>
Commit a transaction (public API without WAL)
Sourcepub fn abort_txn(&self, txn_id: TransactionId) -> Result<()>
pub fn abort_txn(&self, txn_id: TransactionId) -> Result<()>
Abort a transaction (public API without WAL)
Sourcepub fn add_snapshot_ancestor(
&self,
txn_id: TransactionId,
ancestor: TransactionId,
)
pub fn add_snapshot_ancestor( &self, txn_id: TransactionId, ancestor: TransactionId, )
Record a write operation for conflict detection (first-committer-wins).
Called by the DML executor for each row modified by INSERT, UPDATE, or DELETE. The (table_name, pk_bytes) pair is added to the transaction’s write set, which is checked against committed write sets at commit time. Add an ancestor to a transaction’s snapshot (for savepoint support).
When a savepoint creates a sub-transaction, the sub-transaction needs to recognize the parent transaction as an ancestor so that write-write conflict detection doesn’t flag writes from the same logical transaction.
Sourcepub fn record_write(
&self,
txn_id: TransactionId,
table: String,
pk_bytes: Vec<u8>,
) -> Result<()>
pub fn record_write( &self, txn_id: TransactionId, table: String, pk_bytes: Vec<u8>, ) -> Result<()>
Record a write and check for conflicts (first-committer-wins).
Returns Err(WriteConflict) if another transaction has already written
to the same (table, pk) — either committed after our snapshot or still active.
Sourcepub fn acquire_write_lock(&self) -> impl Drop + '_
pub fn acquire_write_lock(&self) -> impl Drop + '_
Acquire write lock for exclusive writing
Sourcepub fn oldest_active_txn(&self) -> Option<TransactionId>
pub fn oldest_active_txn(&self) -> Option<TransactionId>
Get the oldest active transaction ID
Sourcepub fn version_store(&self) -> &RwLock<VersionStore>
pub fn version_store(&self) -> &RwLock<VersionStore>
Get the version store
Sourcepub fn active_count(&self) -> usize
pub fn active_count(&self) -> usize
Get number of active transactions
Sourcepub fn has_aborted_transactions(&self) -> bool
pub fn has_aborted_transactions(&self) -> bool
Quick check whether any aborted transactions exist.
When false, all is_aborted calls are guaranteed to return false.
Sourcepub fn is_aborted(&self, txn_id: TransactionId) -> bool
pub fn is_aborted(&self, txn_id: TransactionId) -> bool
Check if a transaction was aborted
Sourcepub fn is_row_visible(
&self,
snapshot: &Snapshot,
created_by: TransactionId,
deleted_by: Option<TransactionId>,
) -> bool
pub fn is_row_visible( &self, snapshot: &Snapshot, created_by: TransactionId, deleted_by: Option<TransactionId>, ) -> bool
Check if a row is visible to a snapshot, accounting for aborted transactions.
A row is visible if:
- The creating transaction was NOT aborted
- The snapshot can see the creating transaction (per standard MVCC rules)
- Either the row is not deleted, the deleting transaction was aborted, or the snapshot cannot see the deletion
Sourcepub fn aborted_count(&self) -> usize
pub fn aborted_count(&self) -> usize
Get the number of aborted transactions being tracked
Sourcepub fn cleanup_aborted_txns(&self, older_than: TransactionId)
pub fn cleanup_aborted_txns(&self, older_than: TransactionId)
Clean up old aborted transaction IDs that are no longer needed.
Aborted transaction IDs can be removed once all their written data has been garbage collected. This is typically safe to do when the aborted transaction ID is older than any active transaction’s snapshot, since all such data would have been cleaned up by GC.
Sourcepub fn register_dirty_page(&self, txn_id: TransactionId, page_id: PageId)
pub fn register_dirty_page(&self, txn_id: TransactionId, page_id: PageId)
Register a page as dirtied by a transaction
This is called when a transaction modifies a page. The page ID is tracked so that on commit, only this transaction’s pages are flushed, and on abort, they can be discarded.
Sourcepub fn get_dirty_pages(&self, txn_id: TransactionId) -> HashSet<PageId>
pub fn get_dirty_pages(&self, txn_id: TransactionId) -> HashSet<PageId>
Get all pages dirtied by a transaction
Sourcepub fn clear_dirty_pages(&self, txn_id: TransactionId)
pub fn clear_dirty_pages(&self, txn_id: TransactionId)
Clear dirty page tracking for a transaction (called on commit/abort)
Sourcepub fn dirty_page_count(&self, txn_id: TransactionId) -> usize
pub fn dirty_page_count(&self, txn_id: TransactionId) -> usize
Get the number of dirty pages for a transaction
Sourcepub fn run_gc(&self) -> GcStats
pub fn run_gc(&self) -> GcStats
Run garbage collection on old versions.
Removes old versions that no active transaction can see. Returns statistics about what was collected.
Sourcepub fn version_count(&self) -> usize
pub fn version_count(&self) -> usize
Get the number of old versions in the version store
Sourcepub fn long_running_transactions(
&self,
threshold: Duration,
) -> Vec<TransactionInfo>
pub fn long_running_transactions( &self, threshold: Duration, ) -> Vec<TransactionInfo>
Get information about long-running transactions
Returns a list of transactions that have been running longer than the specified threshold, sorted by age (oldest first).
Sourcepub fn gc_status(&self) -> GcStatus
pub fn gc_status(&self) -> GcStatus
Get current GC status
Returns information about whether garbage collection is blocked by active transactions.
Sourcepub fn metrics(&self) -> TransactionMetricsSnapshot
pub fn metrics(&self) -> TransactionMetricsSnapshot
Get transaction metrics snapshot
Returns a snapshot of transaction metrics including active count, commits, rollbacks, and average transaction duration.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for TransactionManager
impl !RefUnwindSafe for TransactionManager
impl Send for TransactionManager
impl Sync for TransactionManager
impl Unpin for TransactionManager
impl UnsafeUnpin for TransactionManager
impl UnwindSafe for TransactionManager
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> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.