We get asked, often, what we do to make Postgres "fast." For mid-market apps — say, 100GB of data and a few thousand active users — the answer is unromantic.
1. Get the indexes right
This is 80% of it. Most apps we inherit have one of two index pathologies: too few (full table scans on hot queries) or too many (write amplification, planner confusion). Both are symptoms of nobody looking at the query plans.
The drill we use:
- Turn on
pg_stat_statementsfrom day one. - Once a quarter, pull the top 20 queries by total execution time. Look at them as a human.
- For each, run
EXPLAIN (ANALYZE, BUFFERS)and decide if the plan is acceptable.
We do not chase query speed in isolation — we chase the queries that consume the most cumulative time. A 1ms query run a million times a day matters more than a 50ms query run twice.
2. Use the right column types
JSONB is amazing and is also overused. The rule we apply: if a key is queried in a WHERE clause, it is a column. If a key is read but not queried, it can live in JSONB.
Other quick wins:
bigintfor any column that might cross 2 billion. The migration cost later is real.timestamptz, nevertimestamp. Naive timestamps are a footgun across timezones.text, notvarchar(N). The N rarely matches the actual constraint.
3. Connection pooling at the right layer
Laravel applications open connections per worker. At 10 workers and 50 queue workers and 5 cron processes, that's 65 connections. Postgres on a 4-core box wants fewer. PgBouncer in transaction mode in front of Postgres takes the connection-explosion problem and turns it into a non-issue.
What we don't do
- We don't pre-emptively shard. Sharding doubles or triples the operational complexity, and the threshold for actually needing it is high.
- We don't pre-emptively use read replicas. Until reads are unambiguously a problem, the additional consistency reasoning isn't worth it.
- We don't optimize before we measure.
pg_stat_statementsfirst; opinions second.
The unromantic answer keeps working much longer than people expect. We have apps that have been on a single Postgres instance for four years.