Sub topic 6
Where PostgreSQL Records Commit and Abort
This chapter assumes you know basic SQL and transaction commands such as BEGIN, COMMIT, and ROLLBACK. It also assumes you have seen PostgreSQL system columns such as xmin and xmax.
A tuple, PostgreSQL's name for a row version, does not directly contain the words “committed” or “aborted.” Its xmin and xmax fields contain transaction IDs, or XIDs. PostgreSQL must look up the status of those XIDs before deciding whether a tuple is visible.
The `pg_xact` status store
PostgreSQL records the outcome of transactions in pg_xact. This is an internal status store managed in small fixed-size pages called SLRU pages. Older PostgreSQL documentation and tools may call this area pg_clog.
For each relevant XID, pg_xact records a small status value:
- In progress
- Committed
- Aborted
- Sub-committed, for a transaction savepoint or subtransaction
The database writes the commit or abort result to its write-ahead log, or WAL. WAL provides crash recovery. PostgreSQL also updates the transaction status information used by visibility checks. A transaction that aborts makes tuple versions created by that transaction invisible.
A transaction can remain in progress while another session reads the tuple. The reader uses its snapshot to decide whether that XID is active, too new, or already committed. It does not treat an in-progress creator as committed merely because the tuple exists on disk.
Hint bits: cached status in the tuple
Repeatedly reading pg_xact for every tuple would be expensive. PostgreSQL can set hint bits in the tuple header after it learns that the xmin transaction committed or aborted. Future readers can use the hint instead of consulting pg_xact.
Hint bits are a performance cache, not a separate transaction history. They do not change the logical result. If a hint is absent, PostgreSQL can consult the authoritative transaction-status information. A crash can also leave some hint bits unset, which is safe because PostgreSQL can reconstruct the answer from WAL and transaction-status data.
CREATE TABLE orders (
id integer PRIMARY KEY,
state text NOT NULL
);
BEGIN;
INSERT INTO orders VALUES (1, 'paid');
SELECT xmin, xmax, ctid, * FROM orders;
ROLLBACK;
SELECT * FROM orders; -- returns no rowsThe inserted tuple may have existed physically until cleanup, but its creator transaction ended aborted. Therefore, normal snapshots reject it.
Cleanup and freezing
Transaction status cannot be retained forever. Once old tuples and old snapshots no longer need an XID, VACUUM can remove old status pages. Before that happens, PostgreSQL may freeze old tuple XIDs. A frozen XID means the tuple is known to be old and committed, so visibility checks no longer need the original transaction status.
This cleanup is essential because transaction IDs are finite. Vacuuming prevents old status data from becoming unusable and protects the database from transaction ID wraparound.
Exercise
An inserted tuple has xmin = 900, and transaction 900 aborted. Should a normal snapshot return that tuple?
Show answer
No. The tuple may remain on the heap temporarily, but its aborted creator makes the tuple version invisible.