PostgreSQL Reference
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
Welcome to the PostgreSQL reference. PostgreSQL is an open-source object-relational
database: it stores data in typed tables, speaks SQL with a very broad implementation of the standard plus its own
extensions (arrays, ranges, jsonb, full-text search, user-defined types), guarantees ACID transactions through
multiversion concurrency control and a write-ahead log, and grows through extensions such as PostGIS and pgvector.
This section is written for developers and engineers who build on PostgreSQL or run it: it explains how the server
works, the SQL and index features that are specific to it, how to read query plans, how concurrency behaves, and how to
administer, back up, replicate and tune a deployment. It is verified against
the PostgreSQL documentation.
If you are new to PostgreSQL, start with Getting Started, which runs a server
in Docker, introduces psql and loads the shop dataset used by every page. Engine-agnostic SQL (DDL, queries, joins, window
functions) is documented once in the sibling SQL Reference, which these pages link to rather
than repeat.
Version baseline
This section documents PostgreSQL 18, the current major version, and every example was executed against PostgreSQL 18.6
from the official postgres:latest Docker image (Debian build, arm64). Where a feature depends on the version, the page
says which release introduced it: for example MERGE (15), JSON_TABLE and MERGE ... RETURNING (17), and virtual generated
columns, uuidv7(), skip scans on multicolumn B-tree indexes and OLD/NEW in RETURNING (18). The community releases one
major version a year, supports each for five years and publishes minor releases quarterly; see the
versioning policy and the
release notes.
What’s covered
Getting started
-
Getting Started — what PostgreSQL is and how it is distributed, running it in Docker,
psqlbasics, connecting, loading the shop seed (shown in full), a first session, and a version timeline of the features the section uses.
Internals
-
Architecture — the postmaster and per-connection backend processes, the background processes, shared buffers and the OS cache, the cluster and
PGDATAlayout, heap pages and TOAST, MVCC row versions (xmin/xmax) inspected withpageinspect, the write-ahead log, checkpoints and crash recovery.
Data types, SQL and JSON
-
Data Types & PostgreSQL-specific SQL — numeric, text and timestamp types, arrays, enums, ranges, domains, identity and generated columns,
RETURNING, upserts withON CONFLICT,MERGE,DISTINCT ON,LATERAL,FILTER, CTEs andMATERIALIZED,COPY, and thepsqlmeta-commands that script SQL. -
JSON & JSONB — the
jsonandjsonbtypes, thejsonboperators,jsonpath, indexingjsonb, and the SQL/JSON functions.
Indexing and query planning
-
Indexing — B-tree (multicolumn, covering, partial and expression indexes), hash, GIN, GiST, SP-GiST and BRIN, unique and exclusion constraints,
CREATE INDEX CONCURRENTLY, index-only scans and the visibility map,REINDEXand bloat, and full-text search basics, each withEXPLAINevidence on generated data. -
Query Planning & EXPLAIN — the planner stages, statistics and extended statistics,
EXPLAINoptions and how to read scan, join and aggregate nodes, the cost model, theenable_*switches for diagnosis, generic versus custom plans, parallel query and partition pruning.
Transactions and concurrency
-
Transactions, Isolation & MVCC — snapshots,
READ COMMITTED,REPEATABLE READandSERIALIZABLE(SSI) with real two-session demonstrations, write skew and serialization-failure retry, row and table locks,SKIP LOCKEDqueues, advisory locks, deadlocks,pg_locksandpg_stat_activity, savepoints and the hazards of idle-in-transaction sessions.
Operations
-
Administration — roles and privileges, row-level security,
pg_hba.conf, the configuration files andALTER SYSTEM,VACUUMand autovacuum, the monitoring views, and routine maintenance. -
Backup & Restore —
pg_dump,pg_restoreandpg_dumpall, physical base backups, WAL archiving, and point-in-time recovery. -
Replication & High Availability — streaming and logical replication, replication slots, failover, an overview of Patroni, and connection pooling with pgBouncer.
-
Performance Tuning — memory settings, checkpoint tuning,
pg_stat_statementsusage, connection pooling, partitioning and bulk loading.
Extensions and reference
-
Extensions — how extensions are installed and managed, and a tour of
pg_stat_statements, pgvector, PostGIS and other commonly used ones. -
Cheat Sheet — a one-page quick reference to the commands and queries of this section.
Deliberately out of scope. This section is a reference for the PostgreSQL server, its SQL and its operation, not a tutorial for one framework, installer or cloud product. The following topics appear in most books on the subject but live elsewhere on this site or are intentionally not covered:
-
Installers and packages for each operating system — the download page is authoritative; this section runs the server in a container, see Getting Started and Docker.
-
PL/pgSQL internals beyond the basics, procedural languages and writing your own extension — procedural code is introduced in Triggers & Procedures; writing a C extension is not covered.
-
Rules, event triggers and logical replication of DDL — row-level triggers are in the SQL pages; the legacy rule system and event triggers are not covered, and logical replication is described for data changes only in Replication & High Availability.
-
Sharding and distributed PostgreSQL (Citus and similar) — intentionally not covered beyond a mention in Extensions; table partitioning inside one server is in Performance Tuning.
-
Cloud-managed services (Amazon RDS and Aurora, Azure, Google Cloud SQL and AlloyDB) — see the cloud sections of this site, for example PostgreSQL and MySQL on Azure and AlloyDB for PostgreSQL.
-
Major-version upgrades with
pg_upgrade, log analysis tools (pgBadger) and auditing frameworks — only mentioned; thepg_upgrademanual page is the reference. -
Migrating from other databases (pgloader, Debezium) — see Database Migration and, for a complete walk-through, Worked Example: Oracle to PostgreSQL.
-
Object-relational mapping and application frameworks — see Hibernate and Spring Data JPA.
Reading path
Developers can stop after the first seven pages; operators should continue through administration, backup, replication and
tuning. The cheat sheet is meant to be kept open next to a psql session.
Running example
The worked examples share one running example, a small online shop in a schema named shop: four tables with 4 customers,
5 products, 8 orders and 11 order lines. The full seed script and the Docker commands to load it are on
Getting Started; the same data set backs the
MongoDB Reference, so the two engines can be compared on identical data. Pages that need volume
(indexing, planning, locking) generate extra rows with generate_series in a scratch schema instead of changing the shop.
A customer places orders (status is open, shipped or cancelled); an order has one or more lines in order_items, each
pointing at a product by sku with a quantity and the price at the time of purchase. products carries a tags array and an
attrs jsonb document, which give the array, GIN and JSON examples something to work on. The outputs on the pages were produced by
running the code on PostgreSQL 18.6 in a container, so every example can be reproduced step by step.
Related material on this site
-
SQL Reference — standard SQL (DDL, queries, DML, transactions, triggers, access control) that this section does not repeat.
-
MongoDB Reference — the document-database counterpart, on the same shop data.
-
Vector Databases & RAG — including PostgreSQL & pgvector for similarity search inside PostgreSQL.
-
Database Migration — change data capture with Debezium, including Oracle to PostgreSQL.
-
Evolving the Database Model — safe schema changes and migrations.
-
Diagnosing Slow Queries — a method for slow queries across engines.
-
Pagination Strategies — offset, keyset and cursor pagination.
-
Choosing the Right Database — when PostgreSQL is, and is not, the right store.
-
Database IDEs & Tools — graphical clients for PostgreSQL and other engines.
-
Hibernate and Spring Data JPA — object-relational mapping on top of PostgreSQL.
-
Docker — containers, the way these pages run the server.
Bibliography
-
PostgreSQL Documentation (current) — the source every page in this section is written and verified against; see in particular Data Types, The SQL Language, Indexes, Full Text Search, Concurrency Control, Performance Tips, Using EXPLAIN, Statistics Used by the Planner, Explicit Locking, Transaction Isolation, Server Configuration, Reliability and the Write-Ahead Log, Database Physical Storage, psql, MERGE and Table Partitioning.
-
PostgreSQL release notes — the authority for which version introduced which feature, including PostgreSQL 18.
-
The official
postgresDocker image — the image the examples run on, with its environment variables and initialisation hooks. -
The contrib module pages that the internals examples rely on: pageinspect, pg_walinspect and pgstattuple.
-
Learn PostgreSQL: Use, Manage and Build Secure and Scalable Databases with PostgreSQL 16, 2nd ed. Packt Publishing, 2023. ISBN 978-1-83763-564-1. Consulted for topic coverage and ordering only (its chapters run from installation and cluster layout through SQL, server-side programming, partitioning, security, transactions and MVCC, extensions, query tuning, logging, backup, configuration, replication and useful tools); no text or examples are reproduced, every example in this section is original and was executed against a real server. See the publisher’s site.
-
The Internals of PostgreSQL (Hironobu Suzuki) — further reading on the process, storage, WAL and concurrency internals summarised in Architecture.
-
Use The Index, Luke — a database-agnostic guide to indexing and query performance, complementary to Indexing.
-
The PostgreSQL wiki — community notes, including Serializable Snapshot Isolation.
-
pgvector — vector similarity search for PostgreSQL, used in PostgreSQL & pgvector.