Build MVCC visibility.
An UPDATE can create a new row version while an older transaction keeps reading the old value. Implement the predicate that decides which physical version becomes a logical row.
You will: evaluate xmin/xmax against snapshot membership and transaction status, trace an update's version chain, and identify versions vacuum must retain. Prerequisites: transaction descriptors and JavaScript sets/maps. Runtime: a small executable version store; PostgreSQL has additional visibility rules.
Multiversion concurrency control (MVCC) retains multiple physical tuple versions. A snapshot describes which transactions a reader can see; a committed version can still be outside an older snapshot. The diagram introduces the creator marker xmin and superseder marker xmax; the predicate below makes their meanings precise.
One logical row can have two physical versions
Predict
Unlike the first diagram example, let the writer begin before the reader but remain active when the snapshot is captured. It commits afterward. Does an ID below the snapshot boundary make its new tuple visible? Predict the old tuple’s visibility if that writer aborts instead. Test both variations in Change one thing.
A row is a history
This model stores each tuple as {id, value, xmin, xmax}. xmin identifies its creator. A non-null xmax identifies the transaction that superseded it. A separate transaction-status table says whether an ID is active, committed or aborted. These markers describe logical visibility; they do not physically remove a row when a transaction commits.
A snapshot contains the reader's own ID, an upper allocation boundary xmax, and IDs active when the snapshot was captured. The snapshot's xmax and a tuple's xmax are different fields with different meanings.
Define a transaction as visible to a snapshot when it is the reader itself, or when all three are true:
- Its ID is below the snapshot's upper boundary.
- It was absent from the snapshot's active set.
- It committed successfully.
Then a tuple is visible if its creator is visible and its deleter/superseder is absent or not visible. A transaction that was active at snapshot time stays excluded even if it commits later. A transaction that aborted must not become a visible creator. An aborted delete must not erase the old version.
Control the experiment
The page loads Baseline with one version worth 100 and a schedule that starts the reader before the writer. Select Step through the update and commit. In Physical tuple versions, follow Creator xmin and Superseder xmax. Compare the writer’s status with the reader’s pinned snapshot.
An executable tuple-version and snapshot-membership algorithm with bounded transaction IDs. One writer and one reader; no PostgreSQL command IDs, wraparound, subtransactions or physical vacuum implementation.
Edit the experiment
Change the JSON settings and select Step or Run to end. Restore scenario reloads the selected preset.
Select Step to inspect the initial state.
Model checks at this step
Inspect JSON or download run evidence
No execution yet.
The version chain outlines the tuple visible to the selected snapshot and labels every hidden version. In the Read Committed scenario, the drawing explicitly projects a fresh statement’s view from the current state. Follow xmin/xmax and compare the selected tuple with the retained version table.
At the last read, Baseline has removed v0 too early. Run Repair and compare the version table and Observed values. Then run new-snapshot. It uses a fresh statement snapshot, so the reader may see 70 after the writer commits.
Inspect and implement visibility
The same visibility rule supports reads and version retention. Select Test implementation to see which cases the starter misses. In Your JavaScript implementation, use the creator and superseder rules above. Test again, especially the active-at-capture creator, aborted superseder and reader’s own write.
Read the engine code executed by this lab
These functions come directly from the running engine. Shared helpers and every mechanism are in the complete downloadable module.
function visible(version, snapshot, statuses) {
const before = tx => tx===snapshot.self || (tx<snapshot.xmax&&!snapshot.active.includes(tx)&&statuses[tx]==='committed');
if (!before(version.xmin)) return false;
return version.xmax===null || !before(version.xmax);
}
function mvcc(config) {
choose(config,'isolation',['repeatable-read','read-committed']);
choose(config,'retention',['latest-only','snapshot-aware']);
const s={nextXid:1,statuses:{0:'committed'},versions:[{id:'v0',value:100,xmin:0,xmax:null}],reader:null,writer:null,snapshot:null,reads:[],vacuumRemoved:[]};
const snapshot = self => ({self,xmax:s.nextXid,active:Object.keys(s.statuses).map(Number).filter(x=>x!==self&&s.statuses[x]==='active')});
const read = snap => s.versions.find(v=>visible(v,snap,s.statuses))?.value??null;
const t=recorder(s,s=>[check('a pinned repeatable snapshot still has its original visible row',
config.isolation==='repeatable-read'&&s.snapshot?read(s.snapshot)===s.pinnedValue:null)]);
for (const op of program(config)) {
if (op==='begin-reader'||op==='begin-writer') {
const role=op.slice(6); if(s[role]!==null)throw Error(`${role} already began.`);
s[role]=s.nextXid++; s.statuses[s[role]]='active';
if(role==='reader'){s.snapshot=snapshot(s.reader);s.pinnedValue=read(s.snapshot);}
t.emit(`${role} receives transaction ID ${s[role]}`);
} else if(op==='write') {
if(s.writer===null||s.statuses[s.writer]!=='active'||s.versions.some(v=>v.xmin===s.writer))throw Error('Begin one active writer and update only once.');
const old=s.versions.find(v=>visible(v,snapshot(s.writer),s.statuses));
if(!old)throw Error('No visible version to update.');
old.xmax=s.writer;s.versions.push({id:`v${s.writer}`,value:70,xmin:s.writer,xmax:null});
t.emit('UPDATE marks the old tuple xmax and appends a new xmin tuple');
} else if(op==='commit-writer'||op==='abort-writer') {
if(s.writer===null||s.statuses[s.writer]!=='active')throw Error('No active writer.');
s.statuses[s.writer]=op==='commit-writer'?'committed':'aborted'; t.emit(`${op}: change the transaction-status table`);
} else if(op==='read-reader') {
if(s.reader===null)throw Error('Begin the reader first.');
const snap=config.isolation==='repeatable-read'?s.snapshot:snapshot(s.reader);
s.reads.push({snapshot:copy(snap),value:read(snap)});t.emit('Apply the tuple visibility predicate');
} else if(op==='vacuum') {
const current=snapshot(-1),pins=config.isolation==='repeatable-read'&&s.snapshot?[s.snapshot]:[];
const keep=v=>s.statuses[v.xmin]==='active'||(config.retention==='latest-only'?visible(v,current,s.statuses):[current,...pins].some(snap=>visible(v,snap,s.statuses)));
s.vacuumRemoved=s.versions.filter(v=>!keep(v)).map(v=>v.id);s.versions=s.versions.filter(keep);
t.emit('Vacuum removes versions invisible to the snapshots it protects');
} else throw Error(`Unknown MVCC operation ${op}`);
}
return t.finish();
}Implement tuple visibility
solve(version, snapshot, statuses) returns a boolean. version has xmin/xmax; null xmax means no deleter. A transaction is visible if it is self, or committed AND below snapshot.xmax AND absent from snapshot.active. The creator must be visible and the deleter must not be visible.
The trace above uses the published algorithm. Test your own function here against the named cases.
Test the starter to find the cases it misses.
Compare the reference implementation
Attempt the named cases before comparing your solution.
function solve(version, snapshot, statuses) {
const before = tx => tx===snapshot.self || (tx<snapshot.xmax&&!snapshot.active.includes(tx)&&statuses[tx]==='committed');
if (!before(version.xmin)) return false;
return version.xmax===null || !before(version.xmax);
}Download your code
Change one thing
Choose Repair and open Edit the experiment. In schedule, move "begin-writer" to the first position, before "begin-reader"; keep the other operations in order. Select Run to end. Check the active list in Pinned snapshot and the last Observed values. Why can a lower transaction ID still be invisible?
Replace "commit-writer" with "abort-writer" and run again. Compare the writer’s status, Superseder xmax on the old version and the final observed value. Restore the Repair schedule. Move "vacuum" immediately before "commit-writer", run, then select the vacuum step in the trace. Which versions did it retain while the writer was still active?
Hint 1 · Test creation and deletion separately
Apply the same snapshot membership question to xmin and xmax. A visible creator plus an invisible deleter means the old tuple remains visible.
Hint 2 · Commit time is not snapshot membership
An ID below the snapshot boundary may have been active at capture. Its later commit does not move it into that historical snapshot.
Why this result occurs
The pinned reader keeps seeing 100 while a new snapshot sees 70. Removing v0 because a current reader prefers v2 destroys the old snapshot's history. The repair retains versions needed by its explicit pin set and active creators. PostgreSQL uses visibility horizons and more metadata, including command IDs, subtransactions, wraparound handling and tuple-header flags; this small predicate deliberately does not reproduce all of those details.
Transfer the idea
Design a second pinned reader that begins after the writer commits. Draw the minimum tuple set needed by both snapshots. The browser engine has one reader; testing two pins requires extending the downloaded engine. Explain how a long-running transaction can retain dead versions and increase storage even while short transactions commit normally. Then predict whether visibility alone prevents write skew; follow the next isolation investigations to test that prediction.
Success criteria: calculate visibility from creator, superseder, active-at-capture membership and status; explain an aborted writer; retain every version needed by the pinned snapshot; pass the visibility bench and distinguish the two meanings of xmax.
Primary sources: PostgreSQL system columns, MVCC introduction and vacuum.
Your design record
Explain what happened and cite the run or SQL result that supports it.
Write your design notes
Download notes and evidence
Downloads include current editor text. Run evidence stays on this page until you leave.