PostgreSQL uses a process-based architecture (one OS process per client connection) backed by shared memory and dedicated background processes. Understanding these components is essential for diagnosing CPU, memory, and I/O bottlenecks.

Process overview#

Inspect running PostgreSQL processes directly from the OS:

ps -ef | grep postgres

Or query active backends and background workers from SQL:

SELECT pid, backend_type, usename, state, wait_event_type, wait_event
FROM pg_stat_activity
ORDER BY backend_type, pid;

Key background processes#

ProcessResponsibility
postmasterMain supervisor process; listens for connections, spawns backends, and restarts crashed workers.
checkpointerFlushes dirty pages from shared_buffers to disk at checkpoints and writes the checkpoint record to WAL.
background writer (bgwriter)Incrementally writes dirty buffers to disk between checkpoints to smooth out I/O spikes.
walwriterPeriodically flushes WAL buffers to disk (pg_wal/).
autovacuum launcherSpawns autovacuum worker processes to reclaim dead tuples, update statistics, and prevent XID wraparound.
archiverExecutes archive_command (or archive library) to copy completed WAL segments to archive storage.
walsender / walreceiverStreams WAL records from primary to standby replicas.

Memory architecture#

Memory is split into shared memory (allocated at startup, shared across all processes) and local per-backend memory:

  • shared_buffers: Main cache for table and index pages (commonly 25% of RAM as a starting point, relying on the OS page cache for the rest).
  • wal_buffers: Shared buffer for WAL records before they are flushed to disk.
  • work_mem: Per-operation (sort, hash join, hash aggregate) memory limit — not per connection; a complex query can allocate multiple work_mem slices.
  • maintenance_work_mem: Memory used by VACUUM, CREATE INDEX, and ALTER TABLE ADD FOREIGN KEY.

Check current memory settings:

SELECT name, setting, unit, context
FROM pg_settings
WHERE name IN (
  'shared_buffers',
  'effective_cache_size',
  'work_mem',
  'maintenance_work_mem',
  'wal_buffers'
);

Storage & PGDATA layout#

Locate the active data directory and configuration files:

SHOW data_directory;
SHOW config_file;
SHOW hba_file;

Key directories inside PGDATA:

  • base/ — Per-database subdirectories containing table and index files (named by OID / relfilenode).
  • global/ — Cluster-wide catalogs (pg_database, pg_authid, pg_control).
  • pg_wal/ — Write-Ahead Log segments (critical for crash recovery and replication; never delete manually).
  • pg_stat/ — Cumulative statistics data.
  • postgresql.conf, postgresql.auto.conf, pg_hba.conf — Server and client authentication configuration.

ALTER SYSTEM SET ... writes to postgresql.auto.conf, which always takes precedence over postgresql.conf.

How Write-Ahead Logging (WAL) works#

Step through the interactive trace below to see how PostgreSQL guarantees durability on COMMIT, survives a crash before data files are written, and syncs pages during a CHECKPOINT:

Interactive How PostgreSQL WAL Works
RAM · VOLATILE DISK · DURABLE Client UPDATE accounts TXID 501 shared_buffers Page 42: balance=900 dirty = true wal_buffers empty in memory Data Files (base/) Page 42: balance=1000 old value on disk pg_wal/ (Segments) Last chkpt: 0/1A00000 sequential append
Step 1 of 6 · Modify page in memory

Trace / State