Sub topic 3
Concurrent updates, row locks and serialization failures
This chapter assumes you know basic SQL, transactions (BEGIN, COMMIT, ROLLBACK), snapshots, and how to open two psql sessions against one database.
Concurrent updates and row locks
When two transactions update the same row, the second update waits until the first one commits or rolls back. In Read Committed, the waiting update then applies its change to the newest committed version. In Repeatable Read, it fails instead, because the row changed after its snapshot was taken.
-- Session A
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM accounts WHERE id = 1;
-- Session B
UPDATE accounts SET balance = balance + 10 WHERE id = 1;
-- Session A
UPDATE accounts SET balance = balance - 5 WHERE id = 1;
-- ERROR: could not serialize access due to concurrent update
ROLLBACK;Dead tuples, VACUUM and autovacuum
A dead tuple is an old row version that no current or future snapshot can see. Each UPDATE and DELETE leaves one behind. VACUUM removes dead tuples and marks their space reusable. Autovacuum is the background process that runs VACUUM when enough rows change.
UPDATE accounts SET balance = balance + 1 WHERE id = 1;
SELECT relname, n_live_tup, n_dead_tup, last_autovacuum
FROM pg_stat_user_tables
WHERE relname = 'accounts';
VACUUM (VERBOSE) accounts;Long-running transactions and bloat
VACUUM can remove a dead tuple only when no open transaction could still need it. A transaction left open for hours holds that boundary back, so dead space builds up. This growth is called bloat. Find the oldest holder like this:
SELECT pid, state, xact_start, backend_xmin
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL
ORDER BY age(backend_xmin) DESC;HOT updates and fillfactor
A HOT (heap-only tuple) update stores the new version on the same page as the old one. It skips index changes, which requires that no indexed column changes and that the page has free space. Fillfactor sets how full a page may get when filled. Lowering it leaves room for HOT updates.
ALTER TABLE accounts SET (fillfactor = 80);This setting applies to pages written after the change, so existing pages keep their layout until they are rewritten.
The visibility map and index-only scans
The visibility map records which pages hold only rows visible to every transaction. VACUUM sets these bits. An index-only scan can skip reading the table for those pages and read only the index.
Transaction ID wraparound and freezing
Transaction IDs are 32-bit numbers, so they eventually run out and restart. If old rows kept their original IDs, PostgreSQL could mistake them for future rows. Freezing has VACUUM mark old row versions as visible to all, which prevents this. Check how close each database is:
SELECT datname, age(datfrozenxid) AS xid_age
FROM pg_database
ORDER BY xid_age DESC;Serializable Snapshot Isolation
Serializable uses Serializable Snapshot Isolation (SSI). It takes snapshots the same way Repeatable Read does, and it also tracks which transactions read data that others write. When it finds a pattern that no one-at-a-time order could produce, it aborts one transaction with a serialization failure. Serializable therefore raises more of these errors than Repeatable Read, so retry logic matters even more.
Exercise
In Repeatable Read, session A reads a row. Session B updates and commits that row. Session A then updates the same row. What happens, and what should the application do?
Show answer
Session A fails with "could not serialize access due to concurrent update". The application should roll back and retry the whole transaction.