The startup's Postgres survival guide
Postgres outages at scale often stem from preventable mistakes like blocking migrations and poor connection management rather than database limits.
Deep dive
- Use identity columns or UUIDs for primary keys to optimize lookups.
- Batch writes using
pgx.SendBatchto reduce round-trip overhead and connection churn. - Monitor autovacuum closely; if a process runs over an hour, re-tune settings.
- Use
FOR UPDATE SKIP LOCKEDfor robust job queues and tenant lease management. - Implement partitioning for time-series data to allow faster deletion and independent vacuuming.
- Always use
EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS)to diagnose query performance.
Decoder
- Autovacuum: A background process in Postgres that reclaims storage from 'dead' (deleted or updated) rows.
- Tuple: The Postgres term for a row of data stored in a table.
- Transaction ID Wraparound: A state where the 32-bit transaction counter resets, potentially causing massive data loss or database shutdown if not prevented by vacuuming.
Original article
The startup's Postgres survival guide
A guide to preventing Postgres from toppling over.
Over the past half year or so, I’ve been writing an internal doc for our engineers trying to distill two years of Postgres battles into a somewhat cohesive document. While I love the Postgres manual, I find it’s hard to turn to when shit hits the fan because it’s just so darn comprehensive. I thought this might be useful for others and would appreciate feedback (or other tidbits that you’ve learned running Postgres in production).
Before starting Hatchet, while I was familiar with SQL, the extent of my knowledge was basically: if a query is slow, you need an index. That’s the starting point for this doc; I’m going to assume you’re familiar with SQL basics, rows, tables, and know roughly what an index is.
And if Claude is writing all of your queries, this might be a waste of time! I recommend supabase/agent-skills
A quick note on ORMs
This guide should still be useful, but you might need to translate some of these tips into your ORM of choice. Lots of optimizations as you scale just aren’t possible with ORMs unless you can break past the abstraction layer and write SQL. You can do this gracefully or non-gracefully; Prisma TypedSQL or equivalents look interesting for this. We use sqlc at Hatchet which gets us very similar behavior; highly recommend if you’re a Go stack.
The simple stuff: good reads, writes and schemas
Let’s start with the basics: queries and schemas at low volume.
Writing a good schema
After you’re deployed, schemas are by far the hardest to change moving forward, so it’s worth spending some time on them. I’d recommend building your schema iteratively: start with a rough approximation for your tables and primary keys, then write some queries on those tables based on your application needs. You can approximate this with some questions: Is this a high-read and/or high-write table? What are the most common filters on reads? Which columns am I updating the most?
If you want to be more formal about it, you can look into database normalization into 1NF/2NF/3NF, but I’ve found normal forms to sometimes be at odds with query efficiency and ease of use, which is critical when you’re moving fast—sometimes it’s just easier to dump data into a jsonb column.
My rules of thumb for schemas are:
- Use identity columns (auto-incrementing integers, slightly more performant than
bigserial) or built-in UUIDs for primary keys - Always use
timestamptz - Always use primary keys
- Use foreign keys with cascading deletes for low-volume tables, particularly where database consistency and correctness are important. Careful at higher volume.
Writing good read queries
Let’s start with SELECT queries. A useful—albeit slightly inaccurate—mental model for fast selects is: under the hood, Postgres is either going to find a single row in a table very quickly, or it’s going to read every single row in your table using something called a sequential scan 😞.
It’s going to find a single row very quickly when you filter by:
- An explicit index
- A unique constraint (just a special case of index)
- A primary key (these are automatically indexed in Postgres)
Indexes by default use a btree implementation. It’s most helpful to think of indexes as just another table in Postgres, with data stored in a specific format which is optimized for lookups (more on this later). These trees are great because finding a single row happens in approximately log(n) time, where n is the number of rows in the table—in other words, really fast.
When Postgres can’t use an index, it’ll use something called a sequential scan, or seq scan. Seq scans are much slower than index lookups, but modern databases are so fast at loading rows into memory that you probably won’t even notice at first: seq scans on tables with less than 20k rows are pretty much instant.
Writing performant joins
For inner joins, there’s rarely an argument for not using primary keys as the inner join; it usually speaks to a schema design or normalization problem. Treat ON clauses with the same respect as a WHERE clause—the same principles apply. Use an index.
Compound indexes and aligning ORDER BY to your indexes
Often the first slow query in your application will be a list query across a large table. In this case, you can use a compound index. In more complex cases, a good rule of thumb is: the ORDER BY columns should be the last columns in the index, and you should align columns to the ordering in the ORDER BY. Note that Postgres can scan btrees in both directions, so sometimes the DESC is irrelevant—but for compound indexes it’s good practice.
Writing good write queries
The premise of successful writes is:
- Keep transactions short. Don’t go querying an external service in the middle of a transaction unless you have a really good reason to.
- Be careful of the rows you’re locking for writing; in other words, only lock what you need. Every time you update a row, you’re taking out a lock on that row for a short period of time until the transaction commits.
As your system gets busier, you’re going to start noticing the impact of locks more. In particular, you might try to create an index at some point in the future with a simple CREATE INDEX command: turns out this locks your table and prevents inserts and updates! When creating an index on an existing large table, always use CREATE INDEX CONCURRENTLY.
Migrations
Getting really good at writing migrations is an important technical advantage: it helps you iterate much faster and increases your uptime. As a starting point, try to keep migrations additive (in other words, don’t delete or remove columns) and run them in a transaction wherever possible; this will make rollbacks and partial migrations much easier to deal with.
The simplest mental model for good migrations is: does this block all of my writes, or does it not? Creating an index without CONCURRENTLY blocks all your writes, so you might see downtime. Generally, operations which call ALTER TABLE should be worth a second look; for example, adding a new check constraint to a very large table can block your writes as well (unless you add it with the NOT VALID keyword).
Connection management
Every time you execute a transaction or query against your database, you’re utilizing a connection. Connections are expensive in a number of dimensions (cpu and memory), and high connection churn can lead to a lot of unnecessary resource waste, so connections should be long-lived. Connection storms (when you start using up a ton of new connections at the same time) can also lead to very hard to debug edge cases related to internal Postgres locks.
Because of all these connection footguns, external connection poolers like pgbouncer are great! If you can’t add this for whatever reason, in-memory connection poolers are a great second option.
Intermediate: the query planner, bulk updates, and autovacuum
Introducing the leakiest of abstractions, the query planner
At a certain point, your queries might become complex enough that a simple index won’t cut it. At this point, you will need to concern yourself with the query planner. The query planner looks at the query you pass in, and it figures out how it should translate your query into a set of internal operations in the database.
This limited information is the table statistics. These statistics are collected for every ANALYZE. This also happens when autovacuum is run, so more frequent autovacuums also mean that your query statistics will be more up to date. A common reason why your query is behaving improperly is not analyzing frequently enough.
This is where EXPLAIN ANALYZE is your friend. This outputs the query plan for the query and executes the query (careful running this in production—you can use EXPLAIN without ANALYZE to get a query plan), and then compares its estimates based on the table statistics to the actual number of rows scanned.
Sometimes it just makes sense to seq scan
There are cases where you think an index should be used, but the query planner is still seq scanning anyway, despite table statistics being up to date and the index being valid. In these cases, Postgres is usually estimating that the cost of the seq scan will be smaller than the cost of the index scan. Index scans do come with some overhead; indexes are stored separately from the actual data in the table (called the heap)—finding all of the rows in the heap can be expensive!
Writing lots of data
To reduce overhead when writing a lot of data, we can pack a batch of rows into each query. The simplest way to do this is to send all queries to the Postgres server at once in an implicit transaction. Batching is very powerful: we found that it can ~10× your throughput.
Default autovacuum settings can kill your database
Autovacuum is a critical operation in Postgres databases that sometimes needs to be tuned, especially in high-write scenarios. The autovacuum daemon is responsible for a number of things, including cleaning up dead tuples and managing transaction ids.
What’s a dead tuple? A tuple is an instance of a row on the filesystem. Every time you update or delete a row, a version of that row is left in Postgres until all transactions which started before that row was updated or deleted have committed or rolled back. These rows which can no longer be read by any transactions are dead tuples.
If you’re writing data quickly enough, sometimes autovacuum can’t keep up, which will get you into a very unhealthy state. If you use up all transaction ids in the system before they can be reclaimed by autovacuum, you’ll reach a dreaded state called transaction id wraparound. This will mean a big chunk of downtime.
Other types of bloat
Besides dead tuples, there are two other kinds of bloat you’ll often encounter in a busy Postgres system:
- Table bloat caused by partially filled pages. When dead tuples are reclaimed, this can lead to pages not being entirely filled, which can increase the disk usage of Postgres.
- Index bloat is a special case of table bloat, and is similarly solved by good autovacuum settings. But Postgres has a built-in command for dealing with this, which is
REINDEX INDEX CONCURRENTLY.
Some advanced stuff
FOR UPDATE SKIP LOCKED
The best way to think about this Postgres feature is that it reserves the rows that you’re selecting for use in your transaction without interfering with other queries. It’s also very useful in cases where you’re doing many independent updates of rows, or you’re managing leases on objects in your system across many instances of your application.
Partitioning
Postgres has built-in partitioning which allows you to subdivide your tables based on row values, like timestamps or hashes. This can be incredibly useful for time-series data because:
- Each partition can be autovacuumed independently, allowing you to scale up autovacs on your table
- Deleting old data is near-instant—you simply drop the table partition, rather than iterating through rows
Tricks for large table migrations
If you try to migrate really large tables, it can take many hours to copy data in a single transaction. This isn’t good—long-running transactions prevent autovacuum from doing its job properly. One of the tricks we’ve learned is to use Postgres triggers and run a large batched backfill outside of a transaction, using unique constraints on the primary key to prevent duplicate writes.
Fresh dev news, AI-digested daily. Never miss a trend at devoured.fyi