Your database is getting too big for one server: the options in order of cost

What to try before sharding: clean up, index, archive, scale up, add read replicas, partition, then shard. Each step costs more than the last.

The short answer

Work down this list and stop when the problem is solved: fix queries and indexes, remove bloat and archive old data, move big files out of the database, scale the server up, add read replicas, partition large tables, and only then shard. Each step costs more in complexity than the one before. Most products never need to go past the middle of the list.

On this page
  1. 1. Fix the queries and indexes
  2. 2. Find out what is actually using space
  3. 3. Archive and delete old data
  4. 4. Scale up the server
  5. 5. Add read replicas
  6. 6. Separate workloads
  7. 7. Partition the big tables
  8. 8. Shard
  9. How do you decide?
  10. What about backups?
  11. What about managed databases?
  12. How big is too big?
  13. Working with us

“Our database is getting too big” can mean several different problems: it runs out of disk, queries are slow, backups take too long, or writes cannot keep up. Name which one it is first. The right answer for a full disk differs from the right answer for slow writes.

Here are the options in order of cost, counting engineering time and the complexity you will carry afterwards.

1. Fix the queries and indexes

Often “too big” really means “slow”. Missing indexes, N+1 queries and unbounded result sets cause most of it; see where the time usually goes. Fix these before changing hardware.

2. Find out what is actually using space

Measure table and index sizes. Typical surprises:

  • Bloat: dead rows that were not cleaned up. Check that autovacuum is keeping pace, and reclaim space on the worst tables.
  • Unused indexes that take space and slow writes.
  • Log and audit tables that grew without limit.
  • Files stored in the database. Move uploads and generated documents to S3-compatible object storage and keep only references in the table. This is the single biggest saving in many apps, and it is one of the rules we follow anyway; see designing a SaaS that can move servers.

3. Archive and delete old data

Do you need five years of events online? Decide a retention rule for each large table. Move old rows to cheaper storage, or aggregate them. Check legal retention duties and your own privacy promises first, since deleting data also matters in the other direction: some of it you must not keep.

4. Scale up the server

More memory, faster disks and more CPU on the same machine. It is the simplest option because the application does not change. Its limits are the biggest machine available and the cost curve. It buys time, often a lot of it, so it is a legitimate step once the cheap fixes are done. Do not use it to avoid steps 1 to 3.

5. Add read replicas

If the load is mostly reads (dashboards, reports, search), send those to one or more copies that follow the primary. The primary handles the writes.

What it costs you:

  • Replication lag. A replica may be a moment behind. A user who saves something and immediately reads it from a replica can see old data. Read their own writes from the primary.
  • Application changes to route queries.
  • More servers to back up, monitor and fail over.

It also gives you a second copy for failover, if you plan that properly.

6. Separate workloads

Move heavy reporting and analytics off your main database to a copy, or to a separate analytics store. Run long reports against a replica, never against the primary at peak time. Search can often stay in PostgreSQL with full-text search; add a search engine only when you have measured a need, because each extra stateful service is one more thing to back up and restore.

7. Partition the big tables

Partitioning splits one large table into pieces, usually by time or by tenant, inside the same database. Queries that filter on the partition key touch less data, and dropping an old partition is instant compared with deleting millions of rows.

Cost: you choose a key that must match how you query, and constraints and migrations get more complicated. It works well for append-only data such as events and logs.

8. Shard

Sharding spreads data across multiple independent databases, for example each group of customers on its own server. It is the only option that scales writes beyond one machine, and it is by far the most expensive: cross-shard queries and transactions get hard, migrations run many times, and operations multiply.

If you do reach it for a multi-tenant product, per-tenant placement is the natural key, and having a tenant id on every row from the start makes it possible. See multi-tenant or single-tenant.

How do you decide?

Symptom Try first
Slow queries Indexes, query fixes
Disk filling Bloat, files out of the database, archiving
Slow reports Replica or separate analytics copy
Reads dominate Read replicas
Huge append-only tables Partitioning
Writes saturate the machine Scale up, then sharding
Backups and restores take too long Partition, archive, then re-check restore time

What about backups?

Size changes recovery. A restore that took ten minutes at 10 GB can take hours at 1 TB. Test a restore at your current size on a schedule, and write down how long it takes; that number is your real recovery time. See nightly restore drills.

What about managed databases?

A managed PostgreSQL service handles replicas, failover and backups for you, for a fee. It is a fair choice, especially for a small team. Check what you can export, and keep the application able to move, which means standard SQL and no provider-specific features in your core.

How big is too big?

There is no single number. A well-tuned PostgreSQL server holds a lot of data on current hardware, and the point at which size becomes a problem depends on the shape of your queries, not only the gigabytes. Treat these as signals instead: queries that cannot be fixed with an index, maintenance such as vacuuming or backups that no longer fits in its window, a restore time that is longer than your customers would accept, or writes that saturate the machine. Measure each one, and move to the next option only when a measurement says the current one has run out.

Working with us

If your database is under strain, we can measure where the space and time go, apply the cheaper fixes first and plan any larger step with a rehearsal on a copy and a written way back. Tell us what you see through contact, or read about our SaaS platform work.

Keep reading

Tell us what you want to build.

A product, a website, a search problem or a system that should work better. We reply with a plain answer and the first step.

A few sentences are enough (at least 20 characters).

We use your details only to answer your inquiry. See our privacy policy.