Build Logs/Building this site · Part 6 of 9
·14 min read
Content generated from real runs
Part 6 of the build log for this site. Two kinds of post where no output is typed by hand: a 92-question SQL practice bank whose every answer is run in DuckDB, and a database series whose demos were recorded from PostgreSQL, MySQL, MongoDB, Redis and Memcached on real concurrent connections. How each generator works, how a blocked statement is recorded, and what a re-run proved.
A wrong number in a tutorial is worse than none: a reader trusts it, learns it, and repeats it. Two series on this site avoid typed outputs entirely. The Data Engineering SQL posts are generated from specs whose answers are run in DuckDB, and the Database Engineering posts replay recordings made against real database engines. This part explains both generators and what they guarantee.
#TL;DR
- The SQL posts are compiled, not written.
scripts/sql-bank/build.pycreates a fresh DuckDB database per question, runs the sample setup and the answer, and writes the post and its step-through data from the real results. - Re-runnable answers are run twice. 13 of the 92 questions are marked
rerun, and the build fails if the second run changes anything. ✅ Re-run on 2026-11-13 with DuckDB 2.0.0: all 92 questions in 5 seconds, and git showed no file changed. - The database demos are recordings.
scripts/db-lab/record.mjsstarts five engines in Docker and drives several real connections step by step. 📜 25 multi-session recordings, 100 command runs, 15 measured charts and 22 live boxes, from 2026-11-05. - A blocked statement is recorded as blocked. Each statement races a 450 ms timer; if the timer wins, the run moves on and later records which step released it.
- The prose still quotes numbers by hand, so the rule is: re-run, then re-read the text around every changed number.
#The SQL practice bank
Data
#A question is a spec
Each of the six posts is a Python module in scripts/sql-bank/posts/. A question looks roughly like this (field names from the real dataclass; the content shortened):
@dataclass
class Q:
id: str
title: str
level: str # Easy | Medium | Hard | Expert
pattern: str
prompt: str
setup: str # SQL that creates and fills the sample tables
answer: str # the answer, run for real
explain: str
check: str
hint: str = ""
result: str = "" # query for the expected output when the answer is DML
diff: tuple[str, str] | None = None # (table, key columns) to mark inserted/changed/removed rows
rerun: bool = False # run the answer twice and require the same result
steps: list[Step] | None = None # custom trace; default is input -> result
display: str = "" # engine-specific SQL shown instead of the runnable answer
build.py imports every module, and for each question:
- Opens a fresh in-memory DuckDB database, so no question can see another's tables.
- Runs
setup, then renders the sample tables from the database, not from the spec. - Runs
answer, thencheck, and renders the expected output from the result. - Runs each trace step: a step can run statements and then show tables, and a shown query can return a
_markcolumn (keep,new,drop,upd,hit,bad) that tints rows in the step-through. For a statement that changes data,diff_tablecompares the table before and after and marks inserted, removed and changed rows by key. - For a question marked
rerun=True, runs the answer a second time and fails the build if the result moves. That is how aMERGEor an incremental load proves it is idempotent: running it twice must give the same table.
It then writes content/blog/<slug>.mdx and content/sql-traces/<slug>.json, which the <DataTrace> component reads to show each answer stage by stage.
When the answer needs syntax DuckDB doesn't share with Snowflake, the spec shows the Snowflake form with display= while DuckDB runs its own equivalent, and the explanation cites Snowflake's documentation for the behaviour.
#What a re-run proved
python -m venv .venv && .venv/Scripts/python -m pip install duckdb
.venv/Scripts/python scripts/sql-bank/build.py
git status --short content/blog/de-sql-* content/sql-traces
de-sql-foundations: 15 questions
de-sql-dimensional-warehouse: 22 questions
de-sql-medallion-cdc: 17 questions
de-sql-product-analytics: 18 questions
de-sql-cross-domain: 14 questions
q87 ok
...
q92 ok
de-sql-capstones: 6 questions
git status printed nothing: all six posts and all six trace files came out exactly as committed, in 5 seconds, on a DuckDB version newer than the one they were first built with. That is the property the generator exists for. A change to a spec, or a DuckDB upgrade that changed a result, would show up as a diff to read before committing.
📜 When the bank was first built, running the answers caught several wrong numbers in the hand-written explanations. The explanations are still prose, so after any change to sample data the numbers they quote have to be compared with the regenerated tables by hand.
#The database recordings
Data
The Database Engineering series shows transactions blocking each other, crashes losing writes, query plans changing with data size, and replicas falling behind. Those are things that happen between connections over time, and a typed transcript of them would be guesswork. So each one was recorded.
#Engines and specs
scripts/db-lab/lab.mjs knows five engines, each a Docker image with its own port so it can't collide with anything else on the machine: PostgreSQL 18, MySQL 8.4, MongoDB 8.0, Redis 8.0 and Memcached 1.6. A spec in scripts/db-lab/specs/ describes, per post, what to record:
| Kind | What it is | Shown by |
|---|---|---|
| Theater | Several sessions running statements step by step, with what a third connection saw after each step | <TxnTheater> |
| Run | A command and its real output, labelled with the engine and version | <Ran> |
| Chart | A measurement over a range of sizes, such as query time against rows | <LabChart> |
| Playground | SQL for a live box, checked on PGlite before it is saved | <PgPlayground> (part 7) |
| Data | One-off captures: raw pages, plan matrices, a wire trace | Purpose-built components |
node scripts/db-lab/record.mjs design # record one post's spec
ONLY='^collisions$' node scripts/db-lab/record.mjs design # record one item
record.mjs saves after every item, so a crash or a timeout loses at most one recording. ✅ The recordings in the repository today hold 25 theaters, 100 runs, 15 charts, 22 playgrounds and 5 data captures across 14 files (312 KB). 📜 They were recorded on 2026-11-05. I didn't re-record them for this series: Docker wasn't running on this machine, and I chose not to start it for a re-run that would only reproduce timings.
#Recording a blocked statement
The hard part of recording concurrency is that a blocked statement doesn't return, and a script that waits for it waits forever. The recorder never waits:
const work = sessions[who].run(sql); // a real query on that session's own connection
const running = work.then(
(r) => (box.done = { ok: true, r, ms: Date.now() - began }),
(e) => (box.done = { ok: false, e, ms: Date.now() - began }),
);
await Promise.race([running, pause(opts.wait ?? t.wait ?? 450)]); // returned, or 450 ms passed
if (box.done) Object.assign(step, toResult(box.done));
else {
step.status = "blocked"; // still running: it is waiting on something
waiting.set(i, box);
}
await pause(opts.settle ?? 150);
for (const [k, b] of [...waiting]) {
if (!b.done || k === i) continue;
steps[k].resolvedAt = i; // this step released it
steps[k].resolved = toResult(b.done); // and this is what it finally returned
waiting.delete(k);
}
if (t.observe) step.table = await admin.run(fill(t.observe, vars)); // what a third connection sees now
So a step either returns within 450 ms or is marked blocked. After every later step the recorder lets the engine settle for 150 ms and checks which waiting statements have finished, recording the step that freed them and their real result. The theater component replays exactly that: the waiting statement shows a timer until the step that released it.
📘 The 450 ms threshold is a judgment: long enough for any unblocked statement on a local container, short enough to keep a recording quick. A statement that is merely slow would be misread as blocked, so specs that time things set their own wait.
#Checks before anything is written
- Every live box is run on PGlite (the in-browser PostgreSQL of part 7) before the file is saved, so a box that would error in a reader's browser fails the recording instead. A box meant to show an error is marked
mayFail. - A missing recording fails the build. A post refers to an item as
src="acid/transfer", andlabIteminsrc/lib/dbLab.tsthrows at build time ifacid.jsonhas notransfer.
#Things that can trip you up
- Editing a generated post. The next build overwrites it. Change the spec.
- Numbers in prose after a re-run. Generated tables update; sentences that quote them don't. Re-read every changed number.
- A slow statement read as blocked. The 450 ms race can't tell slow from waiting; give timing specs a longer
wait. mayFailas a blanket. It hides every error in that box, not only the intended one. Check those boxes in a browser.- A newer engine changing a result. Pin versions in the harness, and treat a diff after an upgrade as something to explain, not to accept.
#Reproduce this yourself
- ✅ Rebuild the SQL bank.
pip install duckdbin a virtual environment, thenpython scripts/sql-bank/build.py, thengit status. No changes means every answer still produces its published output (DuckDB Python API). - ✅ Count the recordings. Each file in
content/db-lab/hastheaters,runs,charts,playgroundsanddata; counting their keys gives the numbers above. - 📜 Re-record a database item. With Docker running,
ONLY='^<item>$' node scripts/db-lab/record.mjs <spec>. The harness starts its owndbe-*containers. Then re-read the prose around the item. - 📘 Read the specs.
scripts/sql-bank/posts/p1_foundations.pyandscripts/db-lab/specs/acid.mjsare the best first examples of each kind.
#Summary
Two series on this site are generated from runs rather than written. The SQL bank compiles each question from a spec, runs its setup and answer in a fresh DuckDB database, proves re-runnable answers by running them twice, and writes the post and its step-through from the results; re-running it today reproduced every file exactly. The database series replays recordings from five real engines in Docker, where each statement races a timer so a blocked one is recorded as blocked and released at the right step, and every live box is tested on PGlite before it is saved. The prose around them is still checked by hand.
#Key takeaways
- If an output can be computed, compute it, and make the build do it.
- Run idempotent answers twice; it is the cheapest proof they are idempotent.
- Record concurrency with a timeout race, never with a blocking wait.
- An escape hatch like
mayFailneeds its own check, because it hides more than you meant.
#Concepts at a glance
| Term | In one line |
|---|---|
| Spec | The source a generator reads: question, setup, answer, trace steps |
| Fresh database per question | Each answer runs in isolation, so nothing leaks between questions |
| Idempotent | Running it twice gives the same result as running it once |
| Theater | A recorded multi-session run, replayed step by step |
Promise.race | Settles with whichever promise finishes first, here a query or a timer |
mayFail | Marks a live box whose errors are expected, so the recorder accepts them |
#Test yourself
Why does each SQL question get a fresh database?
So a question can't depend on tables another question created or changed. Each answer's output then depends only on its own setup, and the questions can be read in any order.
How does the generator prove a MERGE answer is idempotent?
Questions marked rerun=True run their answer a second time, and the build fails if the result changes. An idempotent statement leaves the same table after the second run.
Why can't the recorder just await each statement?
A statement blocked on a lock doesn't return until another session releases it, and that release is a later step the script hasn't run yet. Awaiting it would wait forever.
What did re-running the SQL bank on a newer DuckDB show?
That every published table and trace still matched: git showed no changed file. A difference would have meant a spec or engine change worth reading before committing.
#Sources
- DuckDB: Python API
- MDN: Promise.race
- PostgreSQL: Explicit locking
- Docker: docker run
- The two series this part describes: Data Engineering SQL and Database Engineering