Series · 40 parts
PostgreSQL Masterclass
A deep, practical path through production PostgreSQL: data types and schema design, indexing and query performance, transactions and concurrency, JSONB and full-text search, and scaling with partitioning, replication, and backups.
Choosing the Right PostgreSQL Data Types
Most schema problems don't announce themselves on day 1.
Part 1 of 40 · BackendSchema Design and Normalization in PostgreSQL
Normalization has a reputation for being an academic topic, something you memorize for an exam and then forget.
Part 2 of 40 · BackendPrimary Keys in PostgreSQL: UUID vs BIGINT
Every table in the last article had a primary key, and I quietly used `BIGINT GENERATED ALWAYS AS IDENTITY` without explaining the choice.
Part 3 of 40 · BackendConstraints: Your Last Line of Data Integrity
Application code is where you validate input. Constraints are where you make sure the database refuses bad data even when the application code is wrong, and it will be wrong eventually.
Part 4 of 40 · BackendSQL Joins in PostgreSQL, Explained Properly
Joins are where SQL stops being a lookup language and starts being a query language.
Part 5 of 40 · BackendAggregations, GROUP BY, and HAVING in PostgreSQL
Aggregation is how you turn a table full of rows into a number a human actually wants: total revenue, orders per customer, average response time.
Part 6 of 40 · BackendWindow Functions in PostgreSQL
There's a class of question that `GROUP BY` can't answer without contortions. Rank each customer's orders from newest to oldest. Show each order next to that customer's running total.
Part 7 of 40 · BackendCTEs and Recursive Queries in PostgreSQL
A common table expression is a named subquery you define with `WITH` and use in the main query.
Part 8 of 40 · BackendHow PostgreSQL Indexes Actually Work
Every "why is this query slow" investigation ends in the same place: a query is reading far more rows than it returns, because it has no index to jump straight to the ones it needs.
Part 9 of 40 · BackendChoosing the Right PostgreSQL Index Type
The B-tree handles most queries, but Postgres ships five other index types, and each exists because a B-tree can't do a specific job well.
Part 10 of 40 · BackendComposite and Covering Indexes in PostgreSQL
A single-column index is the easy case.
Part 11 of 40 · BackendPartial and Expression Indexes in PostgreSQL
A regular index covers every row and indexes the raw column value. That's often more than a query needs, and occasionally the wrong thing entirely.
Part 12 of 40 · BackendReading EXPLAIN and EXPLAIN ANALYZE in PostgreSQL
Every article in this module so far has ended with the same promise: confirm it with `EXPLAIN`. This is where we cash that in.
Part 13 of 40 · BackendPostgreSQL Query Optimization Techniques
Once you can [read a query plan](/blog/postgresql-explain-analyze), the next skill is knowing what to do about a bad one.
Part 14 of 40 · BackendFinding and Fixing Slow Queries in PostgreSQL
Reading a plan tells you why one query is slow. It doesn't tell you which queries to look at in the first place.
Part 15 of 40 · BackendIndex Maintenance, Bloat, and REINDEX in PostgreSQL
Indexes and tables in PostgreSQL are not static. Because of how Postgres handles updates and deletes, both accumulate dead space over time, called bloat, that slows queries and wastes disk.
Part 16 of 40 · BackendTransactions and ACID in PostgreSQL
A transaction is a group of statements that either all happen or none happen.
Part 17 of 40 · BackendPostgreSQL Isolation Levels Explained
Isolation is the "I" in ACID, and it's the one that causes real bugs in production, the kind that only appear under concurrent load and are almost impossible to reproduce locally.
Part 18 of 40 · BackendLocks in PostgreSQL and How to Avoid Contention
Locks are how PostgreSQL stops two transactions from stepping on each other. Most of the time they work invisibly, and you never think about them.
Part 19 of 40 · BackendUnderstanding and Preventing Deadlocks in PostgreSQL
A deadlock is two transactions each holding a lock the other needs, both waiting forever.
Part 20 of 40 · BackendOptimistic vs Pessimistic Locking in PostgreSQL
When two users edit the same record at once, one of them is about to overwrite the other's changes without knowing it.
Part 21 of 40 · BackendSELECT FOR UPDATE and Row-Locking Patterns in PostgreSQL
`SELECT ... FOR UPDATE` is the workhorse of correct concurrent writes in PostgreSQL.
Part 22 of 40 · BackendMVCC, Dead Tuples, and VACUUM in PostgreSQL
Everything in this module (isolation levels, non-blocking reads, snapshots) rests on one mechanism: multi-version concurrency control.
Part 23 of 40 · BackendWorking with JSONB in PostgreSQL
JSONB is the feature that lets PostgreSQL store and query semi-structured data without giving up on being a relational database. It's genuinely useful and genuinely easy to misuse.
Part 24 of 40 · BackendIndexing and Querying JSONB Efficiently in PostgreSQL
A JSONB column with no index is a slow column. Every containment query scans the whole table, parsing JSON on each row. That's fine for a lookup table and a disaster for a million-row events table.
Part 25 of 40 · BackendArrays, Enums, and Composite Types in PostgreSQL
Between plain scalar columns and full JSONB, Postgres has a middle tier of types that most developers underuse: arrays, enums, and composite types.
Part 26 of 40 · BackendFull-Text Search in PostgreSQL
When users need to search text, the reflex is to reach for a separate search engine like Elasticsearch. For a lot of applications that's premature.
Part 27 of 40 · BackendViews in PostgreSQL: When They Help and When They Hurt
A view is a saved query you can select from as if it were a table.
Part 28 of 40 · BackendMaterialized Views and Refresh Strategies in PostgreSQL
A materialized view is a view that stores its result.
Part 29 of 40 · BackendTriggers and Stored Procedures in PostgreSQL
Triggers and stored procedures let you run logic inside the database, automatically on data changes or as callable routines.
Part 30 of 40 · BackendRow-Level Security for Multi-Tenant SaaS in PostgreSQL
In a multi-tenant SaaS, the worst bug you can ship is one tenant seeing another tenant's data.
Part 31 of 40 · BackendTable Partitioning in PostgreSQL
Some tables grow without bound.
Part 32 of 40 · BackendConnection Pooling with PgBouncer in PostgreSQL
PostgreSQL connections are expensive. Each one is a separate operating-system process with its own memory, and Postgres runs well with a few hundred of them, not thousands.
Part 33 of 40 · BackendStreaming Replication and Read Replicas in PostgreSQL
Once a single database can't keep up with read traffic, or you need a standby ready to take over if the primary fails, replication is the answer.
Part 34 of 40 · BackendHigh Availability and Failover in PostgreSQL
Replication gives you standby copies of your database. High availability is what turns those standbys into a system that survives the primary failing without a human scrambling at 3am.
Part 35 of 40 · BackendBackups and Point-in-Time Recovery in PostgreSQL
Replication protects you from a server dying.
Part 36 of 40 · BackendTuning Autovacuum for Production in PostgreSQL
Part 37 of 40 · BackendMonitoring PostgreSQL in Production
You can't operate a database you can't see.
Part 38 of 40 · BackendScaling PostgreSQL: Vertical, Read Replicas, and Sharding
Most scaling advice jumps straight to the exotic options, sharding, distributed SQL, exotic architectures, when the honest truth is that the large majority of applications never need them.
Part 39 of 40 · BackendThe PostgreSQL Production Checklist
This series covered a lot of ground, from choosing a data type to sharding a cluster.
Part 40 of 40 · Backend