RadarTrek
Home/Courses/PostgreSQL Deep Dive/How Postgres Stores Data and MVCC
🐘PostgreSQL Deep DiveFree

How Postgres Stores Data and MVCC

12 min read

Everything Is a Heap

Postgres stores table rows in heap files β€” pages of 8KB each. Rows are not stored in any particular order. Understanding this helps you understand why indexes matter and why sequential scans exist.

Pages and Tuples

  • Page β€” the fundamental unit of storage; 8KB by default
  • Tuple β€” a single row version within a page
  • ItemId β€” a pointer array at the top of each page pointing to tuples
  • TOAST β€” oversized values (>2KB) are compressed and stored in a separate table

MVCC: Multi-Version Concurrency Control

Postgres never overwrites a row in place. Instead, it creates a new version of the row for each UPDATE, and marks the old version as dead. This allows readers to see consistent snapshots without blocking writers.

  • xmin β€” the transaction ID that created this row version
  • xmax β€” the transaction ID that deleted or updated this row version (0 if current)
  • Readers use their snapshot to determine which row versions are visible to them
  • Dead row versions accumulate over time β€” this is why VACUUM exists
πŸ’‘

MVCC is like a document editor with version history. Instead of erasing a change, every edit creates a new version. Readers see the version that was current when they opened the document.

Transaction Isolation Levels

  • READ COMMITTED (default) β€” each query sees rows committed before it began
  • REPEATABLE READ β€” the whole transaction sees the same snapshot from its start
  • SERIALIZABLE β€” full isolation; behaves as if transactions ran one after the other
!

Use READ COMMITTED for most web app queries. Use REPEATABLE READ for transactions that do multiple reads and need a consistent view. Use SERIALIZABLE only when correctness is critical and you can handle serialisation failures.

Lesson quiz

4 questions. Get 3 right to pass. Passing every lesson quiz unlocks the course certificate.

  1. 1. What is the default Postgres page size?

  2. 2. What does Postgres do on an UPDATE?

  3. 3. What does a row’s xmax hold?

  4. 4. Why does VACUUM exist?

RadarTrek Intel β€” monthly score updates

We track 200+ tools across 35 categories so you don't have to. Score changes, new tools, and new guides β€” once a month, no spam.