Back to Blog
Technical Deep Dive
11 min read

Scaling Our PostgreSQL Setup Without Losing Our Minds

David Brooks
Database Developer
December 5, 2024
PostgreSQL
Scaling
Databases

PostgreSQL is great — until you're holding back traffic because your queries are dragging and your write queue is five miles long. We hit that wall, and here's exactly how we scaled out PostgreSQL without rewriting our entire stack or breaking the bank.

The Problem We Faced

We were running a single primary database with read/write access and minimal indexing. As traffic grew, CPU usage spiked, query latency doubled, and backups took too long. We were relying on a setup meant for MVPs, not production-grade scale.

Step 1: Analyze the Query Load

Our first move wasn't to add hardware — it was to measure. We used:

  • 🔍 pg_stat_statements to find slow and frequent queries
  • 📊 auto_explain for real-time execution plans
  • 🧠 Aggregated logs to visualize query distribution

Turned out: 80% of the traffic came from 5 poorly optimized queries, mostly joins and unindexed filters on growing tables.

Step 2: Add Missing Indexes (But Carefully)

We added compound indexes on frequently queried columns. One mistake we avoided? Indexing everything. Indexes speed up reads but slow down writes and increase storage. We benchmarked every change before deploying.

Step 3: Move to Read Replicas

Next, we introduced read replicas via AWS RDS. Our app was updated to use replicas for all analytics and dashboard queries. This offloaded ~35% of the read load instantly.

Step 4: Partition Large Tables

Our largest table (transactions) had over 100M rows. We switched to range-based partitioning by month. Writes improved by 60%, and archive queries got faster due to partition pruning.

Step 5: Monitor Everything

Scaling once isn't enough. We set up Grafana dashboards for:

  • Query throughput (TPS)
  • Connection count and replication lag
  • Index hit rate and table bloat

We also added alerts for long-running queries and disk space thresholds to avoid silent failures.

Our Takeaways

  • 🧠 Don't scale blindly. Measure first.
  • 🔧 Indexing is a scalpel, not a sledgehammer.
  • 🌊 Partitioning is powerful but adds complexity — test carefully.
  • 📖 Postgres docs are underrated — dig into them.

Scaling Postgres isn't a one-time event — it's a mindset. If you treat it like a living system, not a fire-and-forget setup, it'll serve you for years to come.

David Brooks
Database Developer