Overview
PostgreSQL is a process-per-connection relational database. A supervisor process (the postmaster) accepts connections and forks one backend process per client; background processes handle writing, logging and cleanup; all of them share a block of memory and a set of files on disk.
Why it matters
Almost every operational question (why connections are expensive, why VACUUM exists, what a checkpoint is, why replicas lag) follows from this layout.
Core concepts
flowchart TB C1[client] --> PM[postmaster] C2[client] --> PM PM --> B1[backend 1] PM --> B2[backend 2] subgraph shared memory SB[(shared_buffers)] WB[(WAL buffers)] end B1 --> SB B2 --> SB B1 --> WB BG[background writer / checkpointer] --> SB WW[WAL writer] --> WB SB --> D[(data files)] WB --> W[(WAL files)] AV[autovacuum launcher + workers] --> D
- Backend: executes one session’s queries. Its private memory holds
work_memareas for sorts and hashes, andmaintenance_work_memforVACUUMand index builds. - shared_buffers: the shared page cache (default 128 MB). PostgreSQL also relies heavily on the operating system page cache, so data is often cached twice;
effective_cache_sizetells the planner how much cache to expect in total. - WAL (write-ahead log): every change is logged before the data page is written (pg-wal-checkpoints). The WAL writer flushes it; the checkpointer and background writer write dirty data pages.
- Autovacuum: a launcher starts worker processes that remove dead row versions and refresh statistics (pg-vacuum-bloat).
- Pages: tables and indexes are stored in 8 kB pages. A row is addressed by its
ctid= (page number, line pointer). Large values are compressed or moved out of line by TOAST. - Logical layout: a cluster (one server instance) holds several databases; each database holds schemas with tables, indexes, views and functions. A tablespace maps objects to a directory.
- System catalogs (
pg_class,pg_attribute,pg_index…) are ordinary tables describing everything else.
Worked example
SHOW data_directory; -- where the cluster lives
SELECT pg_relation_filepath('orders'); -- file backing a table, e.g. base/16384/16397
SELECT pg_size_pretty(pg_database_size(current_database()));
SELECT pid, backend_type, state FROM pg_stat_activity; -- the processes described abovepg_stat_activity lists client backends and background workers (checkpointer, walwriter, autovacuum launcher…).
Pitfalls and production notes
- Each connection is a full OS process with its own memory, so thousands of direct connections hurt. Use a pooler (pg-connection-pooling).
work_memis allocated per sort/hash node per backend, not per connection: a complex query on 200 connections can use many times the setting (pg-tuning).- Do not size
shared_buffersto “all the RAM”; the OS cache is part of the design. - Version: this note is checked against PostgreSQL 17. Major versions are supported for five years; plan upgrades (
pg_upgradeor logical replication).
Practice questions
- Why does a crash of one backend make the postmaster restart all of them?
- What does
ctididentify, and why does it change after anUPDATE? - Where would you look to see which process is writing dirty pages?
References
- PostgreSQL manual, Server Setup and Operation and Database Physical Storage: https://www.postgresql.org/docs/current/storage.html
Related
pg-mvcc · pg-wal-checkpoints · pg-connection-pooling · transactions-acid