Prologue — Night Shift Begins#
Friday, 23:45, Lisbon. Two weeks into her first job, Maya is handed the front-desk laptop, a lanyard, and a sticky note.
Management: "The spreadsheet crashed when a woolly mammoth tried to book a top bunk. Put the hostel in PostgreSQL before the 01:00 herd arrives."
Maya: Me? Tonight? The last database query I wrote was on an exam. I got a B-minus.
Ferris: Then you came to the right basement. I live down here with the Elephant. You write Python; I get it to PostgreSQL in as few round trips as physics allows.
Welcome to The Elephant Dormitory, a hostel in Lisbon whose guests weigh between two and eight tons. Over one very long night (and the six months that follow it), you'll help Maya replace a crashed spreadsheet with a real application, built on PostgreSQL and pgdorm.
Each chapter is a problem the hostel actually has: double-booked penthouses, a herd that arrives all at once, housekeepers fighting over the same task, a partner feed of ten thousand guests at midnight. Each problem gets solved with one runnable Python script.
Who this book is for#
You're starting your very first project. You'll learn what a table, a query and a transaction are by watching them solve real problems, in an order that builds on itself. Every term is explained the first time it shows up, and key abbreviations like RTT or CTE link directly to the Glossary (with hover tooltips on desktop). If you've never installed PostgreSQL, start with Before Your First Shift.
You're thinking about switching stacks. The chapters double as a tour of what you get when an ORM commits to one database instead of supporting five: PostgreSQL temporal keys, MERGE … RETURNING, row-level security, pipelined round trips, zero-copy Arrow. Coming From Another ORM? maps your existing vocabulary onto pgdorm in one page, and the Story Map lets you jump straight to the feature you care about.
How to read a chapter#
Every chapter follows the same rhythm, so you always know where you are:
| You'll see | What it is |
|---|---|
| A comic strip | The problem, as it arrives at the front desk. |
| 🐼 The Panda's plan | The naive approach we've all written once, and what it costs in round trips, races or lost data. |
| Numbered steps | The pgdorm solution, one function at a time, each followed by What just happened. |
| 🦀 Ferris' basement | Optional deep dives into the wire protocol, the planner and PostgreSQL internals. Skip them on a first read. |
| 🔭 What PostgreSQL received | Every SQL statement the chapter sent, captured live. |
| 📋 Shift report and 🧪 Try it yourself | A recap and one small experiment to run on your own machine. |
| 📝 Sticky notes | Management's next demand. They're never optional. |
Info
Every chapter's Python listing, output, and wire SQL transcript is live-verified. Each numbered code block is copied verbatim from a script in examples/. The test suite runs every script against a live PostgreSQL and fails if any # => output comment disagrees with reality, or if a single captured SQL statement changes. Highlighted lines in code blocks are exactly those verified outputs.
The guests#
Four species check into Lisbon every week, and each one brings its own operational headache:
| Species | Typical guest | Why it matters to the database |
|---|---|---|
| Savanna | Jumbo Kalahari, five tons, impeccable manners | Heavy. Needs reinforced floors. |
| Forest | Koko Odzala, from Congo | Travels with family, so group bookings. |
| Pygmy | Pip Kinabatangan, from Borneo | Small, chatty, edits her profile constantly. |
| Mammoth | Bram Pleistocene, eight tons of optimism | Wants the top bunk. Breaks every rule. |
In the database, species won't be free-text strings that someone eventually misspells. They become a lookup table that PostgreSQL itself enforces. You'll see how in Chapter 4.
Why pgdorm, in one paragraph#
pgdorm is a typed Python ORM that targets exactly one database, PostgreSQL, and refuses to pretend otherwise. You describe tables as Python classes and build queries from typed expressions; a compiler written in Rust turns them into canonical SQL, and a Rust wire client sends them with as few round trips as possible. There is no raw SQL escape hatch: if pgdorm can express it, it's typed, and if it's typed, your editor catches mistakes before PostgreSQL does.
Ferris' basement: what's a round trip, and why do we keep counting them?
A round trip (RTT) is one request from your app to PostgreSQL plus the answer coming back. On a laptop it takes a fraction of a millisecond; across a data centre it's often 1–2 ms; over the harbour café's Wi-Fi in Chapter 10 it's 15 ms. Most slow database code isn't slow because PostgreSQL is slow. It's slow because it asks a hundred small questions one after another. pgdorm makes round trips visible (pool.trace() counts them) and gives you tools to spend fewer of them.
Sticky note from Management
"Herd arrives 01:00. First guest already in the lobby. He is VERY polite. Do not keep him waiting."