TxTransactions Lab
GitHub
II · Implement isolation

Break a rule with snapshots.

Each doctor sees someone else on call. Both leave. Every row update succeeds, and the combined result breaks the roster.

You will: distinguish a stable snapshot from serializability and identify a cross-row read/write dependency. Prerequisites: snapshots and lost updates. Runtime: a teaching model. Its assumptions appear before the scenario controls.

Serializability means the result matches some one-at-a-time order of the same transactions. Write skew occurs here when two decisions based on earlier reads change different rows and together break a rule.

Concept diagram · before the experiment

Different rows can still share one invariant

Doctors A and B both see A on call and B on call. A turns its own row off because it read B on; B turns its own row off because it read A on. The writes touch different rows, but both off violates the rule. Read/write dependencies form a cycle.
Read the diagram: Each arrow goes from a reader of an old version to the writer that invalidates that observation. Stable snapshots and disjoint writes do not by themselves serialize a cross-row decision. The model rejects a dependency cycle; PostgreSQL SSI uses a different detection algorithm. Open full diagram

Predict before running

Compare the overlapping schedule with A.read → A.write → B.read → B.write. What does B observe in the serial schedule, and must the serializable policy abort B merely because it writes second? Predict the roster and dependency graph for both histories.

Control the experiment

The page loads Baseline with A and B on call and both reads scheduled before either write. Select Step through the two reads and both publications. Compare reads with onCall after each transaction commits.

A two-row snapshot and read/write dependency engine. The serializable policy runs a DFS cycle check before publication; PostgreSQL SSI uses a different dangerous-structure algorithm.

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.

    The dependency drawing distinguishes a committed dependency graph from the rejected candidate graph. A rejected cycle is labelled as rejected; it is not presented as a cycle in the repaired committed history.

    Inspect the evidence

    The initial roster has A and B. Step through both reads before either write. Each decision is locally reasonable. Check the roster after each commit and identify which earlier observation the other transaction invalidated.

    Change one thing

    Choose Repair and select Run to end. Compare onCall, commits and aborts with Baseline. Identify the rejected cycle in the graph.

    Keep Repair selected. Open Edit the experiment and replace schedule with ["A.read", "A.write", "B.read", "B.write"]. Select Run to end. What does B see in reads now? Use the graph and aborts to explain why B can stay on call without the earlier abort.

    Draw the dependency arrows

    Draw an arrow from A’s read of B to B’s change, and another from B’s read of A to A’s change.

    Protect the cross-row rule

    A CHECK on one doctor row cannot generally enforce a rule across the whole roster. Protect the decision set, or serialize through an agreed invariant-owner row.

    Why this result occurs

    The baseline snapshot preserves each local view while both doctors leave. The repaired model rejects the known unsafe dependency. It is deliberately smaller than PostgreSQL SSI, whose abort point can differ. Use independent PostgreSQL sessions in the server extension to reproduce real write skew and the serialization failure.

    Under the hood: derive the conflicting dependencies

    The model now records row versions read by each transaction. When A overwrites a version B read, it adds B → A: B depended on reading the state before A's change. B's proposed overwrite can add A → B. The serializable policy actually runs cycle detection on that proposed graph before publishing the second write. It does not reject the transaction merely because it is “the second commit.”

    A read/write antidependency points from the reader of an older version to the writer that supersedes it. A cycle is incompatible with a serial order for this read/write schedule. General serializability checking must also consider other dependency types and predicate reads; this two-row model is deliberately narrower.

    PostgreSQL SSI is different from this complete-small-graph check. SIREAD predicate-read tracking does not block writers; it helps track read/write conflicts and detect dangerous dependency structures. PostgreSQL can abort conservatively without waiting to materialize a complete cycle. The OCC implementation provides another strategy: validate read versions at commit.

    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 hasCycle(graph) {
      const visiting=new Set(),finished=new Set();
      function visit(node) {
        if(visiting.has(node))return true;
        if(finished.has(node))return false;
        visiting.add(node);
        if((graph[node]||[]).some(visit))return true;
        visiting.delete(node);finished.add(node);return false;
      }
      return Object.keys(graph).some(visit);
    }
    
    function snapshotDependencies(config) {
      choose(config,'isolation',['snapshot','serializable']);
      const s={onCall:{A:true,B:true},rowVersions:{A:0,B:0},reads:{},readVersions:{},dependencies:{},rejectedDependencies:null,commits:[],aborts:[]};
      const t=recorder(s,s=>[check('at least one doctor remains on call',s.onCall.A||s.onCall.B)]);
      for(const op of program(config)) {
        const [who,action]=op.split('.');
        if(!['A','B'].includes(who))throw Error('Use A.read, B.read, A.write or B.write.');
        if(action==='read') {
          if(who in s.reads)throw Error('Each transaction reads once.');
          s.reads[who]=Number(s.onCall.A)+Number(s.onCall.B);s.readVersions[who]=copy(s.rowVersions);
          t.emit(`${who} reads a snapshot and records both row versions`);
        }else if(action==='write') {
          if(!(who in s.reads)||s.commits.includes(who)||s.aborts.includes(who))throw Error('Read once before writing once.');
          if(s.reads[who]<=1){s.commits.push(who);t.emit(`${who} stays: already the last doctor`);continue;}
          const graph=copy(s.dependencies);
          for(const [reader,versions] of Object.entries(s.readVersions))if(reader!==who&&versions[who]===s.rowVersions[who])graph[reader]=[...new Set([...(graph[reader]||[]),who])];
          if(config.isolation==='serializable'&&hasCycle(graph)){
            s.rejectedDependencies=graph;s.aborts.push(who);t.emit(`${who} aborts before publication: proposed read/write graph has a cycle`);
          }else{
            s.dependencies=graph;s.onCall[who]=false;s.rowVersions[who]++;s.commits.push(who);t.emit(`${who} publishes its row and read/write dependencies`);
          }
        }else throw Error('Use a read or write operation.');
      }
      return t.finish();
    }

    Transfer the idea

    Design a rule requiring two staff with different certifications. Compare serializable transactions with an explicitly locked roster-owner row. State which writers must follow the protocol and what happens when new staff rows are inserted.

    Success criteria: derive both read/write arrows from actual old versions; distinguish a rejected candidate cycle from committed history; compare overlapping and serial schedules and state why PostgreSQL SSI can abort at a different point.

    Primary reference: PostgreSQL Serializable isolation.

    Take this investigation to independent PostgreSQL sessions →

    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.