SQL SQL In Depth
0%
Guided SQL & Engine Coding Lab

Multi-Version Tuple Store with Snapshot Isolation & Vacuum GC

A PostgreSQL-style Multi-Version Concurrency Control (MVCC) storage engine implementing (xmin, xmax) tuple headers, Snapshot Isolation (xmin, xmax, xip_list) visibility rules, First-Committer-Wins write conflict detection, and VACUUM dead-tuple garbage collection.

Guided Algorithm Coding Lab 0/4 Steps Completed

Code & Verify SQL & Storage Engine Invariants in the Browser Workbench

Complete 4 progressive algorithmic steps with guided hints in C++20 (WebAssembly), TypeScript, or JavaScript against automated unit test assertions — zero local setup required.

Architectural Overview

A PostgreSQL-style Multi-Version Concurrency Control (MVCC) storage engine implementing (xmin, xmax) tuple headers, Snapshot Isolation (xmin, xmax, xip_list) visibility rules, First-Committer-Wins write conflict detection, and VACUUM dead-tuple garbage collection.

In production relational and analytical database systems—spanning PostgreSQL, SQLite, MySQL/InnoDB, and DuckDB—understanding how query clauses, schema constraints, index structures, and storage engines execute under the hood separates surface-level query authors from staff-level database engineers. This capstone build lab guides you through implementing Multi-Version Tuple Store with Snapshot Isolation & Vacuum GC from first principles with deterministic self-test invariants.

Guided Implementation Walkthrough (4 Progressive Steps)

Step 1: Transaction Allocation & Snapshot Capture (xmin, xmax, xip)

  • Core Database Concept: Recording Active Concurrent Transaction IDs at Snapshot Creation Time
  • Target Module: index.js (MvccStorageEngine.beginTransaction)
  • Engineering Specification: Implement beginTransaction() to allocate a monotonically increasing txid and capture a snapshot { selfTx: txid, xmin, xmax: this.nextTxId, xip: new Set(activeOtherTxs) }.
  • Implementation Hint: Allocate txid = this.nextTxId++, capture active xip set of other running transactions, and return snapshot.
  • Verification Invariant: deterministic assertion verified in test.js step 1

Step 2: Tuple Visibility Function (xmin / xmax / Commit State)

  • Core Database Concept: Readers Never Block Writers: Evaluating Whether a Tuple Version Is Visible to a Snapshot
  • Target Module: index.js (MvccStorageEngine.isTupleVisible)
  • Engineering Specification: Implement isTupleVisible(tuple, snapshot) so a tuple version is visible iff its creating transaction xmin is visible to snapshot and its deleting transaction xmax is either null or not visible to snapshot.
  • Implementation Hint: Check creator xmin is either selfTx or (committed && xmin < snap.xmax && !snap.xip.has(xmin)), and deleter xmax is either null, aborted, or not visible to snap.
  • Verification Invariant: deterministic assertion verified in test.js step 2

Step 3: Non-Overwriting UPDATE with First-Committer-Wins Conflict Detection

  • Core Database Concept: Expiring the Current Version (xmax = txid) and Appending a New Version (xmin = txid)
  • Target Module: index.js (MvccStorageEngine.update)
  • Engineering Specification: Implement update(txid, key, newValue) to detect concurrent write conflicts (tuple.xmax !== null) and append a new tuple version.
  • Implementation Hint: Find current visible version for key; if tuple.xmax !== null, throw serialization error (First-Committer-Wins); else set tuple.xmax = txid and push new version.
  • Verification Invariant: deterministic assertion verified in test.js step 3

Step 4: VACUUM Dead Tuple Garbage Collector

  • Core Database Concept: Reclaiming Superseded Tuple Versions Invisible to All Active Snapshots
  • Target Module: index.js (MvccStorageEngine.vacuum)
  • Engineering Specification: Implement vacuum() to compute globalOldestXmin across all active transactions and purge dead tuple versions that no active or future transaction can ever see.
  • Implementation Hint: Remove tuples whose xmax is committed and strictly less than globalOldestXmin (or whose xmin was aborted).
  • Verification Invariant: deterministic assertion verified in test.js step 4

Relational & Storage Engine Guarantees

  • Curriculum Domain: MVCC, Locking & Deadlocks
  • Difficulty & Time Budget: Advanced (~45 minutes)
  • Key Architectural Highlights:
    • PostgreSQL tuple header versioning (xmin, xmax) where UPDATE expires the old version and appends a new version
    • Snapshot Isolation visibility evaluator over (xmin, xmax, xip_list) active transaction sets
    • First-Committer-Wins write-write conflict detection and VACUUM dead-tuple reclamation below globalOldestXmin
  • Core Primitives & Tags: MVCC, Tuple Visibility, Snapshot Isolation, First-Committer-Wins, VACUUM GC

Related Curriculum Units

Production Database Context & Verification

This from-scratch relational engine component is engineered with zero external dependencies and deterministic verification harnesses. Every internal database invariant—from three-valued logic (TRUE / FALSE / UNKNOWN), referential integrity, and fan-out-free join pre-aggregation to B+Tree page splits, MVCC snapshot visibility (xmin / xmax), slotted-page compaction, and ARIES write-ahead log crash recovery—is asserted directly in test.js. Use the interactive browser workbench above to complete the 4 guided steps in Guided Lab mode or inspect and benchmark the complete reference architecture in Full Solution mode.