What PostgreSQL is and how it works
PostgreSQL is an open-source object-relational database management system (DBMS). The server stores databases and runs clients’ SQL queries, while transactions and the write-ahead log (WAL) keep data intact under concurrent access and crashes.
People choose PostgreSQL as an application’s main database when data must stay consistent: it has transactions, foreign keys, triggers, complex queries and multiversion concurrency control (MVCC), where readers and writers do not block each other. You can extend it with your own data types, functions, operators and index methods. The licence permits free use, including in commercial products. If you are choosing between PostgreSQL and a column-oriented DBMS for analytics, see the ClickHouse reference, section “What ClickHouse is and how it works”.
Processes and memory
PostgreSQL uses a client/server model, and the client can be on another machine. The first process of an instance, the postmaster, waits for connections and starts a separate server process (backend) for each one; from then on the client talks only to that process. The instance’s processes exchange data through shared memory, which holds copies of table and index pages (shared buffers) and a temporary area for WAL records.
Modified pages are not written to disk straight away. On COMMIT, only the WAL records are flushed to disk: the log is written sequentially, which is cheaper than writing every modified page. The pages themselves are written later by the background writer and checkpointer processes, and after a crash the server replays the changes from the WAL. That is why the WAL is what keeps data durable.
There are several background processes. The WAL writer moves WAL records from shared memory to files in the background, and autovacuum regularly runs vacuuming and gathers statistics. The postmaster starts and supervises all of an instance’s background processes.
The path of a query
Every query goes through four stages:
- Parser: checks the syntax and builds the query tree.
- Rewrite: applies rules, for example replacing a view’s name with its definition.
- Planner: considers the possible paths, such as a full table scan or an index lookup, and picks the cheapest by estimated cost.
- Executor: walks the plan, reads rows, sorts and joins; the result rows go to the client.
EXPLAIN shows which plan was chosen; see the section “Performance and EXPLAIN”.
Cluster, databases and schemas
One server instance manages exactly one database cluster: a set of databases and the objects they share, such as roles. The initdb program creates the cluster. Inside a database, objects live in schemas, which are namespaces, and tables live in schemas. You can run several instances on one machine as long as their TCP ports differ. For details, see the section “Data model and types”.
What you should know first
- The query language:
SELECT, joins, grouping and transactions are covered in the SQL reference. - The Linux command line, users and file permissions are covered in the Linux reference.
- Services and their logs are covered in the systemd reference.
Section sources
the section is written from these pages- 1.2. Architectural Fundamentalspostgresql.org/docs/18/tutorial-arch.html(opens in a new tab)
- 1. What Is PostgreSQL?postgresql.org/docs/18/intro-whatis.html(opens in a new tab)
- 28.3. Write-Ahead Logging (WAL)postgresql.org/docs/18/wal-intro.html(opens in a new tab)
- 51.1. The Path of a Querypostgresql.org/docs/18/query-path.html(opens in a new tab)
- Appendix M. Glossarypostgresql.org/docs/18/glossary.html(opens in a new tab)