Postgres does not slow down gradually for mysterious reasons. It slows down when the working set exceeds memory, when dead tuples accumulate faster than autovacuum reclaims them, or when the connection count forces the planner to spend more time scheduling than executing. All three are measurable ahead of time.
Connection math, not max_connections
The useful number is not max_connections; it is the number of concurrently active statements. Little's law gives it directly: active statements equal arrival rate times average duration. At 900 requests per second and 12 ms average query time, roughly 11 connections are active. A pool of 20 handles that with headroom. Raising the ceiling to 500 does not add capacity, it adds contention for the same CPU.
We set per-instance pool limits from that calculation and use pgbouncer in transaction mode for anything that opens connections per request. Session-level features such as advisory locks or prepared statements need a session pool, and that is a design decision, not a configuration flag.
Vacuum and bloat
Default autovacuum thresholds are tuned for small tables: 20 percent dead tuples plus 50 rows. On a 200 million row events table, 20 percent is forty million dead tuples before a vacuum even starts. We lower the scale factor per table and keep the cost limits modest so vacuum runs continuously rather than in bursts:
ALTER TABLE events SET (
autovacuum_vacuum_scale_factor = 0.02,
autovacuum_vacuum_threshold = 50000,
autovacuum_vacuum_insert_scale_factor = 0.05,
autovacuum_analyze_scale_factor = 0.01
);
After this change, bloat on the events partition fell from 61 percent to 9 percent and the weekly REINDEX maintenance window disappeared from the calendar.
Index bloat is a slow leak
Every update writes a new index entry and marks the old one dead. Under a steady write load, an index on a frequently updated column can grow far beyond its useful size, which inflates cache pressure and makes every scan slower. The fix is not a rebuild schedule; it is finding indexes that nobody reads (idx_scan = 0) and removing them, then rebuilding the survivors concurrently.
-- unused indexes: candidates for removal
SELECT relname AS table, indexrelname AS index, idx_scan,
pg_size_pretty(pg_relation_size(indexrelid)) AS size
FROM pg_stat_user_indexes
WHERE idx_scan = 0 AND indexrelid NOT IN (
SELECT conindid FROM pg_constraint
)
ORDER BY pg_relation_size(indexrelid) DESC;
Queries we keep on the wall
Capacity reviews start from three numbers, and the first is dead tuple ratio per table:
SELECT schemaname, relname, n_live_tup, n_dead_tup,
round(100 * n_dead_tup::numeric
/ greatest(n_live_tup + n_dead_tup, 1), 1) AS dead_pct,
last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 10;
REINDEX CONCURRENTLY is not free
It needs roughly twice the index size in disk and it takes as long as the original build. Schedule it per index, watch replication lag, and never start it on a primary that is already behind on WAL.
None of this is exotic. It is arithmetic, a threshold and a query - which is exactly why it belongs in the planning meeting rather than in the incident review.