Why does a commit only write the log, and where do old row versions go?
Four ideas explain almost every PostgreSQL behavior you will see in production. Each one exists to solve a problem the one before it created: the WAL makes commits fast and safe, checkpoints keep the WAL bounded, MVCC lets readers and writers stop waiting, and vacuum cleans up after MVCC.
Watch it first, then read the details:
A committed change has to survive a crash. Writing every changed 8 KB page to its place in the table file at COMMIT would mean several random writes per transaction. That is slow, and a crash halfway through leaves a half-written page.
The write-ahead log is an append-only log in pg_wal/. Every change is described there before the data page counts. At COMMIT, PostgreSQL flushes only the WAL. The data pages are written later by the checkpointer. After a crash, PostgreSQL replays the WAL from the last checkpoint.
UPDATE changes the page in shared buffers and adds a WAL record to the WAL buffers.COMMIT flushes the WAL to pg_wal/, records the commit in pg_xact/, and only then answers the app.For your app: every write transaction waits for one WAL flush. On network storage that flush is the floor for commit latency. Putting many rows in one transaction pays for the flush once.
archive_command holds WAL forever. max_slot_wal_keep_size caps what a slot can hold.max_wal_size is too small (pg_stat_checkpointer on 17+, pg_stat_bgwriter before).full_page_writes), so frequent checkpoints make more WAL.SELECT wal_records, wal_fpi, pg_size_pretty(wal_bytes) FROM pg_stat_wal; -- 14+
SELECT slot_name, active,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS held
FROM pg_replication_slots;
The same idea elsewhere: MySQL calls it the redo log. MongoDB calls it the journal.
Watch it first, then read the details:
COMMIT writes only the WAL, so changed pages pile up in shared buffers. The WAL cannot grow forever, and after a crash PostgreSQL has to replay all of it. Something has to write the pages out and say where recovery may start.
A checkpoint writes every dirty page in shared buffers to its table file, flushes them to disk, and then writes a checkpoint record to the WAL. Recovery starts from that record, and older WAL files can be recycled. It starts every checkpoint_timeout (5 min by default) or when the WAL passes max_wal_size (1 GB by default).
UPDATE changes a page in shared buffers. The page is now dirty.COMMIT flushes only the WAL.checkpoint_completion_target), so the disk sees a trickle, not a spike.full_page_writes), to guard against torn pages.For your app: checkpoints are why commits can be fast and the WAL still stays bounded.
max_wal_size.checkpoint_completion_target or a disk near its limit.max_wal_size means more WAL to replay after a crash. That is the trade.SELECT num_timed, num_requested, write_time, sync_time FROM pg_stat_checkpointer; -- 17+ SELECT checkpoints_timed, checkpoints_req FROM pg_stat_bgwriter; -- 16 and older
The same idea elsewhere: InnoDB flushes dirty pages in the background and checkpoints against its redo log size. MongoDB's WiredTiger takes a checkpoint every 60 seconds.
Watch it first, then read the details:
A report reads every order while someone updates one of them. If reads and writes had to lock the same row, one of them would wait. On a busy system that is a lot of waiting.
PostgreSQL keeps more than one version of a row. Each version carries xmin, the transaction that created it, and xmax, the one that replaced or deleted it. Each query takes a snapshot when it starts and sees only versions committed before that snapshot. Readers do not block writers, and writers do not block readers.
UPDATE: version 1 gets xmax = 200, and a new version 2 is written with xmin = 200.For your app: reads never wait for writes, and a long report sees one consistent picture.
fillfactor leaves that room.SELECT pid, state, xact_start, now() - xact_start AS open_for, left(query, 60) FROM pg_stat_activity WHERE xact_start IS NOT NULL ORDER BY xact_start LIMIT 5;
The same idea elsewhere: InnoDB updates the row in place and keeps old versions in the undo log. MongoDB's WiredTiger keeps a chain of updates in memory.
Watch it first, then read the details:
Every update and delete leaves a dead row version in the table. Left alone, tables and indexes grow and scans step over more and more garbage. Transaction IDs are 32 bits and wrap around, so old rows also need marking before that happens.
VACUUM removes row versions no transaction can see, removes their index entries, and records the free space so new rows can reuse it. It also updates the visibility map, which index-only scans rely on, and freezes old rows so wraparound never confuses them. It does not shrink the file. VACUUM FULL does, under a lock that blocks all reads and writes.
autovacuum_vacuum_threshold, autovacuum_vacuum_scale_factor).For your app: vacuum keeps tables from growing on every update, but only removes what no one can still see.
hot_standby_feedback running a long query.age(datfrozenxid). If freezing falls far behind, PostgreSQL stops accepting new transactions to protect the data.pg_repack online, not VACUUM FULL on a live table.SELECT relname, n_live_tup, n_dead_tup, last_autovacuum FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 10; SELECT datname, age(datfrozenxid) FROM pg_database ORDER BY 2 DESC;
The same idea elsewhere: InnoDB's purge threads clear old versions from the undo log. MongoDB's WiredTiger reuses space inside its files.
How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.