Skip to content

Story Map#

Every chapter of The Elephant Dormitory is a standalone, runnable PostgreSQL script in examples/. Read straight through from Friday night to sunrise, or jump straight to the superpower your app needs today.

36 runnable programs · 217 outputs verified on every test run · 3,700 SQL statements captured live from PostgreSQL

How progress tracking works

Reaching the bottom of any chapter ticks it off in your sidebar and upgrades your loyalty tier (CALF → SILVER → GOLD → MATRIARCH). Progress stays strictly in your browser's localStorage and never leaves your machine.

Start from the very beginning with the Prologue — Night Shift Begins, or pick an act below:

Act I — The First Night#

Night 1. A spreadsheet just crashed under a mammoth. Put the hostel into PostgreSQL by dawn.

Ch. Shift What you'll get Superpowers
1 First Check-In Insert a row and get the ID, UUIDv7, defaults and computed columns back in the same round trip. create_all() insert().returning() one_or_none()
2 Partial Updates & Guardrails PATCH semantics that never wipe a column by accident, and a DELETE that refuses to delete everything. pg.UNSET RETURNING OLD/NEW UnsafeDmlError
3 The 01:00 Herd Insert 3 or 3,000 rows with one SQL text, one prepared statement and one round trip. insert_many() UNNEST WITH ORDINALITY in_any() → = ANY($1)
4 A Mammoth at the Kiosk Enums the database can enforce, typed JSONB, UUIDv7 and PostgreSQL virtual columns. TypeTable TypeDecorator uuidv7()

Act II — Full House#

Week 2. Group bookings, double-booked penthouses, three ways to pay and a task board at 3 AM.

Ch. Shift What you'll get Superpowers
5 Group Bookings & Folios Joins that write themselves, refuse to be ambiguous, and load parents plus children in 2 round trips. JOIN … USING JoinLintError link tables
6 No Double Bookings PostgreSQL makes double bookings impossible inside the primary key itself. TemporalPrimaryKey WITHOUT OVERLAPS Range[date]
7 Payments & Chores Polymorphic models backed by real foreign keys instead of magic strings. discriminator() single-table inheritance class-table inheritance
8 The 3 AM Task Board A transactional work queue with no broker: PostgreSQL does the locking and the messaging. savepoint() advisory_xact_lock() retry_transaction()

Act III — The Wi-Fi Era#

Month 2. Thousands of guests, a harbour café with flaky Wi-Fi, and Management wants reports.

Ch. Shift What you'll get Superpowers
9 The Endless Guest List Page 10,000 is as fast as page 1, and the planner's index choice is asserted in tests. pg.tuple_() keyset pagination explain()
10 Harbour Wi-Fi BEGIN, two inserts and COMMIT in a single round trip, even over terrible Wi-Fi. pipeline() currval() pg.prepare()
11 The Morning Revenue Digest Subtotals, grand totals and top-N per group without pulling a single row into Python. FILTER (WHERE …) ROLLUP GROUPING()
12 Mapping the Empire Walk a whole tree in one typed recursive query, with cycle protection built in. Table.alias() recursive_cte() SEARCH DEPTH FIRST

Act IV — Expansion#

Month 6. Porto opens, partner feeds pour in, door scanners never sleep, and analytical pipelines connect to Polars and DuckDB.

Ch. Shift What you'll get Superpowers
13 The Nightly Feed Stage a whole partner feed and MERGE it with full OLD/NEW reporting in one round trip. temp=True tables pipe.stage() pg.merge()
14 Lisbon Can't See Porto Tenant isolation enforced by PostgreSQL itself, and still one round trip behind PgBouncer. enable_rls() create_policy() local_settings=
15 A Million Door Scans Monthly partitions with a safety-net default, and EXPLAIN proof that old months are skipped. PartitionBy.range() create_partition() default partition
16 The Polar Bears Arrive PostgreSQL to Polars and back over Arrow and binary COPY, with zero Python row objects. copy_from_arrow() on_error="ignore" pool.arrow()
17 ETL, Lakehouse & Reverse ETL Zero-CSV ETL, Parquet archival, and Reverse ETL across PostgreSQL, Polars, and DuckDB. __arrow_c_stream__ ETL / EtLT Reverse ETL

Act V — Going Live#

Launch Day. A public API, a schema change at peak hour, and a test suite that keeps it all honest.

Ch. Shift What you'll get Superpowers
18 The Booking Desk API FastAPI request models derived from your tables, and JSON encoded directly from wire bytes in Rust. pg.Create[T] pg.Patch[T] pool.json()
19 Renovating While Open Schema changes planned into lock-friendly steps, applied while the lobby stays open. plan_migration() sync_schema() check_drift()
20 Moby's Test Lab Round-trip budgets, index assertions and SQL shape checks that run in CI. pool.trace() tr.rtts assert_uses_index()

And when the last shift ends, read the Epilogue — Sunrise Over the Tagus — or dig into Field Notes · ORM Design, starting with Field Note 1 — Unit of Work & Identity Map (live side-by-side comparison against SQLAlchemy 2 AsyncSession).