Pick your door
20 chapters, 20 PostgreSQL superpowers
One night shift in five acts. Each chapter is one runnable script solving a problem the naive way can't.
Act I · The First Night
A spreadsheet just crashed under a mammoth. Put the hostel into PostgreSQL by dawn.
create_all()insert().returning()
Ch. 2
Partial Updates & GuardrailsPartial PATCH & Guardrails
PATCH semantics that never wipe a column by accident, and a DELETE that refuses to delete everything.
pg.UNSETRETURNING OLD/NEW
Ch. 3
The 01:00 HerdUNNEST Bulk Insert & Upsert
Insert 3 or 3,000 rows with one SQL text, one prepared statement and one round trip.
insert_many()UNNEST WITH ORDINALITY
Ch. 4
A Mammoth at the KioskTypeTable, JSONB & UUIDv7
Enums the database can enforce, typed JSONB, UUIDv7 and PostgreSQL virtual columns.
TypeTableTypeDecorator
Act II · Full House
Group bookings, double-booked penthouses, three ways to pay and a task board at 3 AM.
JOIN … USINGJoinLintError
Ch. 6
No Double BookingsWITHOUT OVERLAPS Ranges
PostgreSQL makes double bookings impossible inside the primary key itself.
TemporalPrimaryKeyWITHOUT OVERLAPS
Ch. 7
Payments & ChoresSTI & CTI Polymorphism
Polymorphic models backed by real foreign keys instead of magic strings.
discriminator()single-table inheritance
Ch. 8
The 3 AM Task BoardLocks, SKIP LOCKED & NOTIFY
A transactional work queue with no broker: PostgreSQL does the locking and the messaging.
savepoint()advisory_xact_lock()
Act III · The Wi-Fi Era
Thousands of guests, a harbour café with flaky Wi-Fi, and Management wants reports.
pg.tuple_()keyset pagination
Ch. 10
Harbour Wi-Fi1-RTT Pipelining & Streams
BEGIN, two inserts and COMMIT in a single round trip, even over terrible Wi-Fi.
pipeline()currval()
Ch. 11
The Morning Revenue DigestROLLUP, Windows & QUALIFY
Subtotals, grand totals and top-N per group without pulling a single row into Python.
FILTER (WHERE …)ROLLUP
Ch. 12
Mapping the EmpireRecursive CTEs & Set Ops
Walk a whole tree in one typed recursive query, with cycle protection built in.
Table.alias()recursive_cte()
Act IV · Expansion
Porto opens, partner feeds pour in, door scanners never sleep, and analytical pipelines connect to Polars and DuckDB.
temp=True tablespipe.stage()
Ch. 14
Lisbon Can't See PortoRLS Policies & PgBouncer
Tenant isolation enforced by PostgreSQL itself, and still one round trip behind PgBouncer.
enable_rls()create_policy()
Ch. 15
A Million Door ScansRange Partitioning
Monthly partitions with a safety-net default, and EXPLAIN proof that old months are skipped.
PartitionBy.range()create_partition()
Ch. 16
The Polar Bears ArriveZero-Copy Arrow & Polars
PostgreSQL to Polars and back over Arrow and binary COPY, with zero Python row objects.
copy_from_arrow()on_error="ignore"
Ch. 17
ETL, Lakehouse & Reverse ETLArrow C Stream & DuckDB
Zero-CSV ETL, Parquet archival, and Reverse ETL across PostgreSQL, Polars, and DuckDB.
__arrow_c_stream__ETL / EtLT
Act V · Going Live
A public API, a schema change at peak hour, and a test suite that keeps it all honest.
pg.Create[T]pg.Patch[T]
Ch. 19
Renovating While OpenLock-Safe Online Migrations
Schema changes planned into lock-friendly steps, applied while the lobby stays open.
plan_migration()sync_schema()
Ch. 20
Moby's Test Lab1-RTT & Schema Test Lab
Round-trip budgets, index assertions and SQL shape checks that run in CI.
pool.trace()tr.rtts
A tutorial that can't lie to you
examples/, and the test suite executes every script against a live PostgreSQL instance.
# => comments under each print() are diffed against the live output. If the book drifts, the build fails.
Meet the night shift
Friday, 23:45, Lisbon
A woolly mammoth just crashed the spreadsheet.
The 01:00 herd is on its way. You have one hour and one PostgreSQL.
Clock in →