Skip to content

PostgreSQL

Scope: designing and running PostgreSQL databases for applications, on the major versions the PostgreSQL project currently supports. Architecture-level data rules, such as data ownership, consistency between services, the transactional outbox and versioned migrations, are in general/data.md; this file covers how to apply them in PostgreSQL. For secrets, see general/security.md, and for connecting from serverless functions, see Serverless and edge hosting.

Entries are sorted by score from 1 to 10 (criteria): 9โ€“10 start here or strongly recommended, 6โ€“8 recommended, 1โ€“5 further reading or conditional.

Adopted references

  • 9 PostgreSQL Documentation (PostgreSQL Global Development Group): the baseline. Read the manual for the version you run; the chapters on data definition, indexes, concurrency control, performance tips, routine maintenance, backup and restore, client authentication and database roles matter most for application work.
  • 8 Backup and Restore (PostgreSQL docs): SQL dumps, file system backups and continuous archiving with point-in-time recovery, and the trade-offs between them.
  • 7 Don't Do This (PostgreSQL wiki): a short list of common mistakes in data types, SQL constructs, tools and authentication, with the rare cases where each is still the right choice.
  • 7 Versioning Policy (PostgreSQL): how long each major version is supported, what minor releases contain, and the end-of-life date of every release.
  • 7 Performance Tips (PostgreSQL docs): reading query plans with EXPLAIN, planner statistics, and populating a database efficiently.
  • 7 PgBouncer features (PgBouncer): the session, transaction and statement pooling modes and which PostgreSQL features each one supports.

Key rules

Schema and data types

Indexes and queries

  • 7 PG-5 Add indexes for the queries the application actually runs: find the costly ones with pg_stat_statements, check their plans with EXPLAIN ANALYZE on realistic data, and drop indexes that are never used. Every index slows down writes. PostgreSQL doesn't index the referencing columns of a foreign key, so index them yourself when you join on them or delete referenced rows. (PostgreSQL, Examining Index Usage, Indexes: Introduction, pg_stat_statements, Foreign Keys)

Schema migrations

  • 8 PG-6 Create and drop indexes on tables in use with CREATE INDEX CONCURRENTLY and DROP INDEX CONCURRENTLY. A plain CREATE INDEX blocks writes to the table until it finishes. The concurrent forms can't run inside a transaction block. A failed concurrent build leaves an INVALID index behind: drop it and try again. (PostgreSQL, CREATE INDEX: Building Indexes Concurrently, DROP INDEX)
  • 7 PG-7 Set a short lock_timeout at the start of each migration, and retry the migration when it times out. Many ALTER TABLE forms need an ACCESS EXCLUSIVE lock; while the statement waits behind a long-running transaction, all later reads and writes on the table wait behind it. (PostgreSQL, lock_timeout, Table-Level Locks, Squawk, Applying migrations safely)
  • 7 PG-8 Add foreign key and check constraints to existing tables as NOT VALID, then run VALIDATE CONSTRAINT in a separate transaction. Adding the constraint then skips the table scan, and validation takes a lock that lets writes continue. PostgreSQL 18 also allows this for not-null constraints. (PostgreSQL, ALTER TABLE: Notes)

Connections and transactions

Backup and recovery

  • 9 PG-11 Test restores regularly, and check that the restored database works and contains the correct data. pg_verifybackup checks a base backup against its manifest, but the PostgreSQL docs say it doesn't replace a test restore. (PostgreSQL, pg_verifybackup, Backup and Restore)
  • 8 PG-12 Back up production databases with continuous archiving, base backups plus archived WAL, so that you can restore to any point in time, not only to the last dump. Set up and test WAL archiving before you take the first base backup. (PostgreSQL, Continuous Archiving and Point-in-Time Recovery)

Security

Versions

  • 9 PG-16 Run a supported major version, apply each minor release soon after it ships, and upgrade to a newer major version before the current one reaches end of life. Each major version is supported for five years, and the project recommends always running the current minor release. Major upgrades need pg_upgrade or a dump and restore. (PostgreSQL, Versioning Policy, pg_upgrade)

How to enforce

Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.

Tutorials and further reading

  • 7 Tutorial (PostgreSQL docs): the official introduction to PostgreSQL, relational concepts and SQL.
  • 7 Use The Index, Luke (Markus Winand): a free online book on SQL indexing and query tuning for developers, with notes for PostgreSQL.
  • 5 PostgreSQL 14 Internals (Egor Rogov, Postgres Professional): a free book on MVCC, vacuum, the buffer cache, WAL, locks, query execution and index types.