Architecture & Processes
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#
| Process | Responsibility |
|---|---|
| postmaster | Main supervisor process; listens for connections, spawns backends, and restarts crashed workers. |
| checkpointer | Flushes 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. |
| walwriter | Periodically flushes WAL buffers to disk (pg_wal/). |
| autovacuum launcher | Spawns autovacuum worker processes to reclaim dead tuples, update statistics, and prevent XID wraparound. |
| archiver | Executes archive_command (or archive library) to copy completed WAL segments to archive storage. |
| walsender / walreceiver | Streams 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 (commonly25%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 multiplework_memslices.maintenance_work_mem: Memory used byVACUUM,CREATE INDEX, andALTER 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 topostgresql.auto.conf, which always takes precedence overpostgresql.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: