Keyboard shortcuts

Press or to navigate between chapters

Press ? to show this help

Press Esc to hide this help

Background Services

src/background/ hosts the asynchronous workers that keep a database healthy: WAL writers, checkpoints, buffer flushers, and MVCC vacuum. A central registry makes it easy to start/stop workers together—ideal for teaching how background maintenance supports foreground queries.


Responsibilities

  • Start workers according to configuration (WalOptions, MvccVacuumConfig, etc.).
  • Define lightweight traits (CheckpointWal, BufferMaintenance, TxnSnapshotOps) so workers can run without pulling in an async runtime.
  • Provide BackgroundWorkers, a registry that tracks WorkerHandles and shuts them down when Database drops.

Built-in Workers

WorkerTriggerBehavior
WAL writerwal_writer_interval_msCalls WalManager::background_flush to durably write log buffers.
Checkpointcheckpoint_interval_msCaptures dirty page / active txn tables and emits Checkpoint records to bound recovery.
Buffer writerbg_writer_intervalFlushes dirty frames to reduce checkpoint pressure.
MVCC vacuumMvccVacuumConfigRemoves obsolete tuple versions once safe_xmin advances.

Every worker registers itself with BackgroundWorkers; shutdown_all() ensures threads exit cleanly during tests or process teardown.


Interactions

  • WalManager – WAL writer and checkpoint workers operate on Arc<dyn CheckpointWal>.
  • BufferManager – background flushers inspect dirty frames and help checkpoints capture consistent snapshots.
  • TransactionManager – MVCC vacuum queries TxnSnapshotOps for safe_xmin.

Teaching Ideas

  • Tune MvccVacuumConfig::batch_limit and chart how quickly old tuple versions disappear.
  • Disable a worker in tests to show why unflushed WAL or missing checkpoints lengthen recovery.
  • Enable RUST_LOG=background=info to trace how these tasks complement foreground load.