AlgoViz

PostgreSQL

medium 32 min

PostgreSQL is an open-source relational database that never overwrites a row: every change adds a new version, readers pick the version their snapshot may see, and vacuum clears away versions nobody needs.

See it run

Row versions, the log, pooled connections, failover and index choice, one step at a time.

Operations

Update and read versions

One row, two readers and a vacuum. The old version stays until no snapshot can need it.

Step 1 of 7

One version

valuexminxmax
slot 12 boats10-
slot 2---
slot 3---

One live tuple

Transaction 10 inserted the row and committed.

Step 1: One version

def update(table, xid, key, value):    old = visible_version(table, xid, key)    old.xmax = xid    table.append(Version(key, value, xmin=xid, xmax=None))def visible(v, snapshot):    return v.xmin in snapshot and v.xmax not in snapshotdef vacuum(table, snapshots):    for v in table:        if all(v.xmax in s for s in snapshots):            table.free(v)
Cost
Dead tuples
You get
No waiting

Say the price together with the benefit: no waiting for readers, work for vacuum.

In the real world

The same postgresql doing real jobs. Pick one and step through it.

Friday evening: a developer opens a transaction and goes home.

Step 1 of 4

18:00

livedeadheld by
orders1 million20,000session 7

A session runs BEGIN and one SELECT at repeatable read, then nothing more.

Step 1: 18:00

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • A connection per request

    critical

    Each connection is a process. A spike exhausts memory and the connection limit.

    Wrong
    def handle(req):    conn = connect()
    Right
    pool = Pool(max_size=20)def handle(req):    with pool.connection() as conn: ...
  • Leaving transactions open

    critical

    The snapshot holds back vacuum in the whole database, and locks stay.

    Wrong
    with transaction():    row = select()    call_payment_api()   # 30 s
    Right
    row = select()result = call_payment_api()with transaction():    save(result)
  • Using a replica for your own write

    major

    The replica may not have replayed it. The user's change seems lost.

    Wrong
    primary.save(profile)return replica.load(profile.id)
    Right
    primary.save(profile)return primary.load(profile.id)
  • No retry after 40001

    major

    At repeatable read and serializable the database may refuse a correct transaction. It must be run again.

    Wrong
    run(transfer)
    Right
    for attempt in range(5):    try:        return run(transfer)   # whole transaction    except SerializationFailure:        backoff(attempt)

Interview tips

  • How to explain it

    • Versions instead of overwriting.
    • Log first, pages later.
    • One process per connection, so pool.
    • Vacuum pays the bill.
  • Say the trigger

    • Replicas: reads exceed one server.
    • Partitions: old data leaves in blocks.
    • Shards: writes exceed the largest server.
  • Watch

    • Oldest transaction and slot.
    • Dead tuples and id age.
    • Replication lag.
    • Slowest queries by total time.
  • Do not promise

    • Zero loss with asynchronous standbys.
    • Exactly-once effects from a jobs table.
    • Automatic failover without a tool.