Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Teams are partitioning tables because 'big tables are slow', and some partitioned tables now have 5,000 partitions with worse latency. What guidance would you give on when partitioning helps and when it hurts?

Partitioning helps with data lifecycle (dropping old data instantly), with maintenance on smaller units, and with pruning when nearly every query filters on the partition key. It hurts when queries do not filter on the key, and when partition counts reach thousands, because planning time, locks and per-backend memory grow with the number of partitions. My guidance is: index first, partition for lifecycle and access patterns, keep counts in the hundreds unless tested, automate creation and retention, and prove it with EXPLAIN and pg_stat_statements.

Why this question is asked

'Big tables are slow' is one of those beliefs that sounds true and leads teams astray. A table with a billion rows and a good index answers a point lookup in a millisecond; a table split into 5,000 partitions can take longer to plan the query than to run it. Interviewers ask this as a design question for senior engineers because the answer needs both internals and judgement, and because writing guidance for many teams is part of the job. In this answer, we will see what partitioning actually changes, when it helps, why it hurts at high partition counts, how it compares with indexing, and the guidance I would publish.

1What Partitioning Actually Changes

A partitioned table splits rows into child tables by a key, such as one partition per month of created_at, or per hash of customer_id. Each partition has its own files and its own indexes. The query planner can skip partitions that cannot match the query's condition on the key, which is called pruning.

Notice what partitioning does not change: finding one row by an indexed column. A B-tree index on a billion rows is only about four or five levels deep, so a lookup touches about five pages. Splitting the table into 100 partitions makes each index perhaps one level shallower, which saves almost nothing. So partitioning is mainly about managing data, not about making indexed lookups faster.

2When Partitioning Helps

Lifecycle is the strongest reason. If we keep 13 months of audit data and drop the oldest month every month, dropping a partition takes seconds and leaves no bloat, while deleting the same rows generates hours of WAL and vacuum work. Maintenance is the second reason: vacuum, reindex and analyze run per partition, so they finish in manageable chunks, and old partitions that no longer change need almost no vacuuming. Pruning is the third reason, and it applies only when nearly all queries filter on the partition key, for example a dashboard that always shows the last seven days. Then each query reads one or two partitions, and a sequential scan over a small partition can be very fast.

3When Partitioning Hurts

The first way it hurts is queries that do not filter on the key. If the table is partitioned by month but the main query is 'orders of customer 42', every partition must be searched, each with its own index lookup. With 60 partitions that is 60 index lookups where a plain table needed one.

The second way is high partition counts. The planner must consider each partition that survives pruning, and at execution time each partition touched needs a lock, and the backend keeps metadata for every partition it has opened in its relation cache. With thousands of partitions this adds up: planning time can grow from well under a millisecond to tens of milliseconds, each connection's memory grows, and a query that cannot prune locks thousands of relations, which can exhaust the shared lock table (max_locks_per_transaction) and slow everyone. PostgreSQL has improved a lot in recent versions, especially for prepared statements with execution-time pruning, but the cost has not disappeared, so check your version and measure.

Where partitioning helps and where it hurts
SituationEffectWhy
Drop a month of old dataHelps a lotDROP partition instead of DELETE
Query filters on partition keyHelpsPruning reads one or few partitions
Vacuum and reindex on huge tableHelpsWork done per smaller partition
Point lookup by indexed idAbout the sameB-tree depth barely changes
Query without the partition keyHurtsEvery partition is searched
Thousands of partitionsHurtsPlanning, locks, memory per backend grow
Partitioning is a data management tool first and a query speed tool only in specific cases.

This is likely what happened to the teams with 5,000 partitions. A common pattern is daily partitions kept for many years, or a hash partition per tenant. Each query either prunes poorly or spends most of its time in planning, and latency gets worse than the unpartitioned table.

4Indexing Is Not Replaced by Partitioning

When a team says a big table is slow, the first question is which query. Most slow queries on big tables are missing an index, have an index the query cannot use (a function on the column, a wrong column order), or return far more rows than needed. Those are fixed with the right index, often a composite or partial one, and the result is a thousand times faster. Partitioning in that situation adds complexity and fixes nothing. Partitioning and indexing work together: each partition still needs the right indexes, and pruning only chooses which partitions' indexes to use.

5The Guidance I Would Publish

Choose the partition key from retention and access patterns: partition by time when data expires by time and queries are by recent time ranges; consider hash or list partitioning only when maintenance on one huge table is the real pain. Keep partition counts in the hundreds rather than thousands unless the team has tested its real queries at that count; monthly partitions for ten years is 120, which is fine, while daily partitions for ten years is 3,650, which needs proof. Automate creation of future partitions and removal of expired ones, with pg_partman or a scheduled job, so nobody does it by hand.

Every partitioning proposal comes with evidence. We run EXPLAIN (ANALYZE) on the top queries against a production-sized copy and check that pruning happens. We track planning time separately from execution time: pg_stat_statements records total_plan_time when pg_stat_statements.track_planning is on. If planning time rises after partitioning, the count is too high or the queries do not prune.

SQL
-- Which statements spend the most time planning?
SELECT query, calls,
round(mean_plan_time::numeric, 2) AS plan_ms,
round(mean_exec_time::numeric, 2) AS exec_ms
FROM pg_stat_statements
ORDER BY mean_plan_time DESC
LIMIT 5;
-- a query with plan_ms 38.10 and exec_ms 1.20 is paying for too many partitions
Step throughShould this table be partitioned?
  1. Is the slow query fixed by an index?
    YesAdd the index; do not partitionNoContinue
  2. Data expires by a key, or maintenance is the pain?
    YesContinueNoDo not partition
  3. Do most queries filter on that key?
    YesContinueNoRethink the key or skip
  4. Count stays in the hundreds?
    YesPartition and automateNoCoarser ranges, or test at that count
  5. Verify with EXPLAIN and plan timeon production-sized data

5 steps in order. Play them, or step through them one at a time.

6Follow-up questions

How would you fix the existing tables with 5,000 partitions?

First measure: find the queries whose planning time dominates. Then reduce the count, usually by moving to coarser ranges such as monthly instead of daily, which means creating new partitions and moving data into them in batches, or by merging old partitions into yearly ones while recent data stays monthly. If the partitioning was per tenant, it usually should become an indexed tenant_id column instead.

Does partitioning help write throughput?

Usually not much on one server, because the same WAL and the same disks are used. It can help when inserts concentrate on a small recent partition whose indexes stay in memory, compared with a huge index where inserts touch random pages. That is a reason to measure, not a rule.

Is hash partitioning useful in PostgreSQL?

Sometimes, for spreading maintenance of one huge table that has no natural time key across, say, 16 or 32 partitions. It gives no lifecycle benefit and prunes only on equality on the key, so it is rarely the first choice.

7Conclusion

In this answer, we saw that partitioning changes how data is stored and managed, not how fast an indexed lookup is. It helps with lifecycle, maintenance and pruning when queries filter on the key, and it hurts when queries ignore the key or when there are thousands of partitions, because planning, locking and memory grow with the count. The guidance is to fix indexing first, choose the key from retention and access patterns, keep counts in the hundreds unless tested, automate creation and retention, and back every decision with EXPLAIN and planning-time data from pg_stat_statements.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start by correcting the premise: big tables are not slow by themselves. A B-tree index on a billion rows is only a few levels deep, so point lookups are fast, and most slow queries on big tables are fixed with the right index.

Then, if they want more

Partitioning is mainly a data management tool. It really helps in three cases: lifecycle, where dropping a month is a quick DROP instead of hours of DELETE and vacuum; maintenance, where vacuum and reindex work on smaller units; and pruning, when nearly every query filters on the partition key.

It hurts when queries do not filter on the key, because then every partition is searched, and it hurts at high counts like 5,000, because planning time, the locks taken per partition and the per-backend relation cache all grow with the number of partitions. That is very likely why those tables got worse.

So my guidance would be: fix indexing first; partition only for retention or access patterns that match the key; keep counts in the hundreds, such as monthly partitions, unless you have tested more with real queries; and automate partition creation and retention with pg_partman or a scheduled job.

Every proposal should come with EXPLAIN ANALYZE on the top queries and planning time from pg_stat_statements with track_planning on, before and after. For the existing 5,000-partition tables, I would merge to coarser ranges or replace per-tenant partitions with an indexed column.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Teams are partitioning tables because 'big tables are slow', and some partitioned tables...": https://scrivio-interview-library.netlify.app/databases/partitioning-and-sharding/8cf155c649/