Sub topic 6
Where PostgreSQL records commit and abort
Earlier chapters showed that every row version carries an xmin and an xmax. Those fields name transactions, but they don't say whether each transaction committed. Visibility checks need that answer. This chapter covers where PostgreSQL stores it, how it avoids looking it up on every read, and which side effects follow. It assumes you can read xmin and xmax and know that a snapshot lists the transactions that were still running when it was taken.
The commit log: pg_xact
PostgreSQL keeps the fate of every transaction ID in a small structure called pg_xact. It lives in the pg_xact directory under the data directory. Older versions called it pg_clog, the "commit log".
pg_xact is a lookup table indexed by transaction ID. It stores two bits per transaction, which allows four states:
| State | Meaning |
|---|---|
| in progress | The transaction has not finished. This is the initial state, all zero bits. |
| committed | The transaction committed. Its row versions count as real. |
| aborted | The transaction rolled back, or was never completed. Its row versions count as if they never existed. |
| sub-committed | A subtransaction (savepoint) has committed, but its parent has not finished yet. |
Two bits per transaction keeps the structure small. A billion transactions need about 256 MB. The file is split into 256 kB segments, and recent pages stay in shared memory, so a lookup is usually a memory read.
There is no undo log. A rollback writes no old data back to the table. It only changes the transaction's pg_xact state to aborted, so the row versions it created become invisible to everyone. The space they occupy is reclaimed later by vacuum.
What happens at commit
A commit has an order that matters for durability:
- The backend writes a commit record to the write-ahead log (WAL).
- It flushes the WAL to disk. With
synchronous_commit = on(the default), the commit waits for this flush. - It sets the transaction's
pg_xactstate to committed. - It removes the transaction from the list of running transactions in shared memory, so new snapshots no longer count it as in progress.
If the server crashes before step 2 completes, the transaction is lost. If it crashes after, WAL replay restores the committed state. A transaction that was running at the time of a crash never got a committed mark, so it reads as aborted after recovery. No cleanup pass is needed.
Checking the status yourself
Since PostgreSQL 10, pg_xact_status() reads the state of a recent transaction. This example uses the accounts table from earlier chapters. The psql command \gset stores a result in a variable.
BEGIN;
INSERT INTO accounts VALUES (2, 50);
SELECT pg_current_xact_id() AS txid \gset
SELECT pg_xact_status(:'txid'); -- in progress
COMMIT;
SELECT pg_xact_status(:'txid'); -- committed
BEGIN;
INSERT INTO accounts VALUES (3, 75);
SELECT pg_current_xact_id() AS txid \gset
ROLLBACK;
SELECT pg_xact_status(:'txid'); -- abortedpg_xact_status() returns NULL when the transaction is old enough that pg_xact has been truncated. It also returns NULL for IDs that were never assigned. The function is meant for one job: after a lost connection, asking whether your last commit went through.
Hint bits: caching the answer in the row
Consulting pg_xact for every row version a query touches would be costly. A table scan may examine millions of tuples, and each xmin would need a lookup. PostgreSQL avoids this by caching the answer in the tuple header.
The header has a t_infomask field with four flag bits, called hint bits, that record what is already known about xmin and xmax:
| Flag | Value | Meaning |
|---|---|---|
| `HEAP_XMIN_COMMITTED` | 256 (0x0100) | The inserting transaction is known to have committed. |
| `HEAP_XMIN_INVALID` | 512 (0x0200) | The inserting transaction is known to have aborted. |
| `HEAP_XMAX_COMMITTED` | 1024 (0x0400) | The deleting or updating transaction is known to have committed. |
| `HEAP_XMAX_INVALID` | 2048 (0x0800) | There is no `xmax`, or the transaction that set it aborted. |
When a visibility check meets a tuple with no relevant hint bit, it finds the transaction's state, using the snapshot for transactions that may still be running and pg_xact for finished ones. If the transaction is finished, the check sets the matching hint bit in the tuple. Later readers skip the lookup.
Nothing sets the bits at commit time. The committing transaction would have to revisit every page it touched, which is expensive. The work is deferred to whichever query reads the tuple first. You can watch this with pageinspect:
CREATE EXTENSION IF NOT EXISTS pageinspect;
CREATE TABLE hint_demo (id int);
INSERT INTO hint_demo VALUES (1);
SELECT t_xmin,
(t_infomask & 256) <> 0 AS xmin_committed,
(t_infomask & 512) <> 0 AS xmin_aborted
FROM heap_page_items(get_raw_page('hint_demo', 0));
-- xmin_committed is false: nobody has read the row yet
SELECT * FROM hint_demo;
SELECT t_xmin,
(t_infomask & 256) <> 0 AS xmin_committed,
(t_infomask & 512) <> 0 AS xmin_aborted
FROM heap_page_items(get_raw_page('hint_demo', 0));
-- xmin_committed is now trueThe row never changed. The plain SELECT rewrote part of its header.
Consequences of hint bits
- A read can dirty a page. The first scan after a large load can produce disk writes, which is surprising for a query that only reads. Running
VACUUMafter a bulk load sets these bits ahead of time. - Hint bits can generate WAL. If data checksums are on, or
wal_log_hintsis enabled, the first hint-bit change to a page after a checkpoint writes a full-page image to WAL. Ordinary hint bits are otherwise not WAL-logged. - A hint bit is only set when the answer is final. For a committed transaction, PostgreSQL waits until the commit record is flushed to WAL. This matters with
synchronous_commit = off, where a recent commit may not be marked yet. The checks are then repeated, and hint bits appear a little later. - Hint bits are an optimization, not the source of truth. If a bit is missing, the answer comes from
pg_xact. If the bit is set, the answer is the same onepg_xactwould give.
Aborts and subtransactions
An aborted transaction's tuples stay in the heap. Readers see xmin belongs to an aborted transaction, set HEAP_XMIN_INVALID, and skip the tuple. The tuple becomes dead space that vacuum removes later, as described in the vacuum chapter.
Savepoints create subtransactions, each with its own transaction ID. Their handling differs from top-level transactions:
ROLLBACK TO SAVEPOINTmarks the subtransaction's ID aborted inpg_xactimmediately, even though the outer transaction continues. Its tuples are invisible to all other sessions, and to the current session afterward.RELEASE SAVEPOINTmarks the subtransaction sub-committed. Its rows become visible to others only when the top-level transaction commits.- A separate structure called
pg_subtransmaps each subtransaction ID to its parent. A reader that finds a sub-committed state follows the link to the parent and uses the parent's outcome.
Why old transaction IDs lose their status
pg_xact cannot grow forever. After vacuum freezes old tuples, which the wraparound chapter covers, nothing needs the old statuses. PostgreSQL then truncates the oldest pg_xact segments. This is why pg_xact_status() returns NULL for old transactions. Frozen tuples are visible to everyone and never trigger a pg_xact lookup again.
Summary of the lookup order
When a query examines a tuple, the check follows roughly this order:
- Read the hint bits. If they already give the answer, stop.
- If
xminis the current transaction, apply the rules for your own changes. - Otherwise decide whether the transaction is running, using the snapshot, or finished, using
pg_xact. If it finished, set a hint bit for next time. - Apply the snapshot rules covered in the visibility chapter.
Exercise
A table was bulk-loaded an hour ago, and the first SELECT count(*) on it afterward is much slower than the second, and causes disk writes. Why, and how could you have avoided the slowdown?
Show answer
The first scan found tuples with no hint bits, looked up each xmin in pg_xact, and set HEAP_XMIN_COMMITTED in the page, which dirtied many pages. The second scan found the bits already set. Running VACUUM (or VACUUM FREEZE) right after the load sets the bits in advance, so the first user query does not pay the cost.
Exercise
A client sends COMMIT and the connection drops before it receives a reply. How can it find out whether the transaction committed, and why might the answer be NULL?
Show answer
Before the transaction, or inside it, the client records pg_current_xact_id(). After reconnecting, it calls pg_xact_status() with that value. The result is committed or aborted if the transaction is recent enough. It is NULL if the ID is so old that pg_xact was truncated after freezing, or if the ID was never assigned.