Home›Learn›PostgreSQL 2: how it stores your data
PostgreSQL · Chapter 2

How PostgreSQL stores your data

What is running, what is in memory, and which file holds my table?

Before we follow a write, here is the whole machine: the processes, shared buffers, the WAL and the data files. Everything in later chapters happens somewhere on this picture.

The architecture: processes, memory, disk

Read each column top to bottom. A request enters at the process layer. Memory serves it when it can. Disk makes it permanent. When something is slow or broken, it lives in exactly one of these three layers. Name the layer first. That is the first diagnostic step.

Listen to the whole architecture, 4 min 49 s

The same layers as a still diagram, with every part named:

PostgreSQL: architecture

Request path

Processespostmaster + 1 backend per connection · checkpointer · bgwriter · WAL writer · autovacuum workers
→
Memoryshared_buffers (~25% of RAM) + per-operation work_mem, on top of the OS page cache
→
Disk: one data directorybase/ (table + index files) · pg_wal/ · pg_xact/ · global/pg_control
Who does what: Backends run queries. The checkpointer flushes dirty pages. The WAL writer fsyncs the log. Autovacuum removes dead row versions. A crashed backend is survivable, but not quiet: the postmaster ends every other session, runs crash recovery, and starts again. Every connection drops.
Files on disk · what ls actually shows

There is no hidden state. Every table, index, and log on this page is a file in one directory. One process owns it. You can inspect it with ls and du while the server runs. When disk fills in production, the failure is always a specific file class. It is never "the database" in the abstract. This tree tells you which file class in under a minute. Paths below are the common Linux defaults. The command under each tree asks the server for its own.

PostgreSQL: the data directory
$ ls /var/lib/postgresql/18/main
PG_VERSION        # "18": which version owns this dir
base/             # one subfolder per database
global/           # cluster-wide tables + pg_control,
                  # the file startup reads first
pg_wal/           # THE WAL: 16 MB segment files:
  000000010000000000000003
  000000010000000000000004
pg_xact/          # which transactions committed
pg_multixact/  pg_logical/  pg_stat/
postgresql.conf  pg_hba.conf
                  # (on Debian/Ubuntu the .conf files
                  # live in /etc/postgresql/18/main/)

$ ls base/16384/   # database oid 16384
16397             # ← table "users" is THIS file
16397_fsm  16397_vm # free-space + visibility maps
16405             # ← index "users_pkey": also a file
Find any table's file: SELECT pg_relation_filepath('users'); gives base/16384/16397. File names are numbers (relfilenodes), not table names. The catalog maps them.
The log files · one by one

Every durability, replication, and recovery guarantee on this page reduces to one of five logs. For each log I give you the exact file name, where it sits, the mechanism, the failure mode when it is undersized, misplaced, or allowed to fill, and the command that shows its current state. Hold this pattern. A log's size divided by the write rate is a time window. Every operational budget on this page is one of those windows: recovery time, replica downtime tolerance, checkpoint headroom.

PostgreSQL

Write-Ahead Log (WAL)

pg_wal/000000010000000000000003
Where it sits
The pg_wal/ folder inside the data directory. A chain of segment files, 16 MB each by default, with hex names: timeline plus log plus segment.
What it does
Every change is written here before the data file is allowed to change. That is the "write-ahead". COMMIT only returns after the WAL record is forced to disk (fsync). The same stream feeds replicas and, when archived, point-in-time recovery.
What breaks without it
No crash safety at all. A power cut could leave half-written pages and silently lost commits. No streaming replication either. Replicas have nothing to replay.
Look at it
$ ls $PGDATA/pg_wal $ pg_waldump pg_wal/000000010000000000000003 -- in psql: SELECT pg_current_wal_lsn();

WAL recycling: when a segment can be reused

WritesWAL records append to the current segment
→
Checkpointdirty pages flush to base/ files
→
Recyclesegments before the checkpoint are renamed and reused
→
Blockeda dead replication slot or failed archiving holds every segment after it

The other logs: MySQL · InnoDB: redo log and binary log · MongoDB · WiredTiger: journal and oplog · the pattern behind all five, and where they break

See the real thing: PostgreSQL 14 Internals by Egor Rogov, free PDF. The best single source on how Postgres actually works, and the book behind half the claims on this page.

Next · chapter 3What happens on every writeWAL, checkpoints, MVCC and vacuum← Chapter 1: Why PostgreSQL
Part 1 of 4 · Learn · 56 pagesAll pages

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.

Start 1: the basicsStart 2: ten conceptsStart 3: how a query travelsStart 4: napkin mathThe courseShort videosPostgreSQL 1: why PostgreSQLPostgreSQL 2: how it stores your dataPostgreSQL 3: what happens on every writePostgreSQL 4: high availability with PatroniPostgreSQL 5: running it as a DBREPostgreSQL 6: when it breaksPostgreSQL on Kubernetes: CloudNativePGMySQL 1: why MySQLMySQL 2: how it stores your dataMySQL 3: what happens on every writeMySQL 4: high availability with InnoDB ClusterMySQL 5: running it as a DBREMySQL 6: when it breaksMySQL failover with OrchestratorMySQL on Kubernetes: the Percona operatorsMongoDB 1: why MongoDBMongoDB 2: how it stores your dataMongoDB 3: what happens on every writeMongoDB 4: high availability with replica setsMongoDB 5: running it as a DBREMongoDB 6: when it breaksMongoDB on Kubernetes: the Percona operatorB-tree and LSM treeRedis 1: why RedisRedis 2: how it stores your dataRedis 3: high availabilityRedis 4: running it as a DBRERedis 5: when it breaksCassandra 1: why CassandraCassandra 2: how it stores your dataCassandra 3: high availabilityCassandra 4: running it as a DBRECassandra 5: when it breaksVector databases 1: why vector searchVector databases 2: how it stores your dataVector databases 3: high availabilityVector databases 4: running it as a DBREVector databases 5: when it breaksDatabase familiesEngines side by sideQueries that look fine and are notLost updates and write skewMany servers 1: keep a copy runningMany servers 2: who is the leaderMany servers 3: CAP as settingsMany servers 4: unique IDsMany servers 5: split the dataMany servers 6: across servicesMany servers 7: a thousand databasesStaff path, zero to architect