Skip to content

100% PostgreSQLTyped PythonRust wire client

The PostgreSQL ORM that never takes a night off.

pgdorm is a typed Python ORM built for exactly one database, and it shows: one-round-trip pipelines, modern PostgreSQL features on day one, and not a single string of raw SQL. Learn it the fun way, over one very long night shift at a hostel for elephants.

36 runnable chapters · 217 verified outputs · 3,700 SQL statements captured live

Jumbo: Tonight's partner feed has updates, newcomers, and needs an old → new audit log!

Panda: Easy! SELECT every row, diff in Python, then loop UPDATE and INSERT.

Ferris: Or stage in pg_temp, MERGE … RETURNING OLD/NEW, and commit in 1 RTT.

A 30-second taste#

Chapter 13, verbatim: stage a nightly partner feed in pg_temp, reconcile inserts and updates with MERGE … RETURNING OLD/NEW, and commit — 7 statements in 1 single round trip (Benchmark #7: 1 RTT vs. 5–7 RTTs). Highlighted lines are outputs verified live against PostgreSQL.

examples/092_staging_and_merge_import.py · sync_ota_feed
async def sync_ota_feed(pool: pg.Pool, feed_rows: list[StageGuestFeed]) -> None:
    merge_stmt = (
        pg.merge(Guest)
        .using(StageGuestFeed)
        .on(Guest.email)
        .when_matched_update(last_name=StageGuestFeed.last_name, country=StageGuestFeed.country)
        .when_not_matched_insert(by_name=True)
        .returning(pg.merge_action(), Guest.email, pg.old(Guest.last_name), pg.new(Guest.last_name))
        .tag("examples.merge_ota_guests")
    )

    # BEGIN + CREATE TEMP (ON COMMIT DROP) + UNNEST + INDEX + ANALYZE + MERGE + COMMIT = 1 RTT!
    async with pool.trace() as tr, pool.transaction() as tx, tx.pipeline(commit=True) as pipe:
        pipe.stage(StageGuestFeed, feed_rows, index_on=(StageGuestFeed.email,), analyze=True)
        h_deltas = pipe.add(merge_stmt)

    deltas = sorted(h_deltas.all(), key=lambda r: r[1])
    print(f"merged {len(deltas)} feed rows in {tr.rtts} RTT ({tr.statement_count} stmts):")
    # => merged 2 feed rows in 1 RTT (7 stmts):
    for action, email, old_last, new_last in deltas:
        print(f"  {action} {email}: {old_last} -> {new_last}")
        # =>   UPDATE [email protected]: Kalahari -> Kalahari-Okavango
        # =>   INSERT [email protected]: None -> Serengeti

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.

A tutorial that can't lie to you

1 Every chapter runs Each code block is pulled verbatim from a standalone script in examples/, and the test suite executes every script against a live PostgreSQL instance.
2 Every output is checked The # => comments under each print() are diffed against the live output. If the book drifts, the build fails.
3 Every SQL statement is real The "What PostgreSQL received" panels are captured from the wire and snapshotted, so a compiler change shows up in code review.

Meet the night shift

Maya You, the reader
Jumbo Your first real user
Bram Edge cases and heavy data
Pip Partial updates and fickle customers
Management Requirements (and deadlines)
Ferris pgdorm's Rust core and PostgreSQL wire protocol
Panda Naive patterns we all wrote once (never a real product)
Polar Bear Polars DataFrames
Duck DuckDB
Moby Docker and testcontainers
Rocco PgBouncer in transaction pooling mode

Full cast list →

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 →