Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Design a partitioning standard for multi-tenant SaaS data in PostgreSQL where 5 tenants are huge and 5,000 are small.

I would list-partition by tenant_id, giving each of the 5 huge tenants its own partition and putting the 5,000 small tenants into one shared partition, itself split by hash into a fixed number of parts, which keeps the partition count in the dozens instead of thousands. Every query carries a tenant_id predicate, backed by row-level security, because pruning and isolation both depend on it. Big tenants can later move to their own cluster by detaching their partition and copying it with logical replication, and when one cluster is no longer enough, Citus or application-level sharding is the next step.

Why this question is asked

Multi-tenant SaaS data is rarely evenly spread. A few large customers often hold most of the rows, while thousands of small ones each hold a little. A standard that treats every tenant the same either creates thousands of tiny partitions or lets the big tenants dominate one table. Interviewers ask this design question to see whether we can weigh the options, keep PostgreSQL's planning cost under control, and leave a path for the day a big tenant outgrows the cluster. In this answer, we will recap the partitioning options, compare them for this skewed shape, design the standard, add tenant isolation, and plan how tenants move later.

1A Quick Recap of the Options

PostgreSQL offers three partitioning methods. List partitioning assigns explicit values to each partition, such as tenant_id IN (17). Hash partitioning spreads rows evenly over a fixed number of partitions by a hash of the key, without us choosing which tenant goes where. Range partitioning splits by ranges of values, which suits time more than tenants. A partition can itself be partitioned, so we can combine methods.

Outside PostgreSQL's partitioning, there are two more choices: separate databases or clusters for some tenants, and sharding across clusters, either with an extension such as Citus or in the application.

2Comparing the Options for 5 Huge and 5,000 Small Tenants

One list partition per tenant gives perfect separation but 5,005 partitions, which is too many, as we will see. Pure hash partitioning by tenant_id into 64 parts keeps the count low, but the 5 huge tenants land wherever the hash puts them, so one partition might hold two of them and be twenty times bigger than its neighbours. A separate database per large tenant isolates them completely, but multiplies operations, migrations and connection pools, and makes cross-tenant reporting harder.

Partitioning options for a skewed tenant mix
OptionPartitionsLarge tenantsCost
list, one per tenantabout 5,000isolatedslow planning, heavy locking
hash on tenant_id (64)64can collide in one partitionuneven sizes
list for 5 big + hashed shared default5 + 16own partition eachsmall, predictable
separate database per big tenantn/afully isolatedmore operations work
The hybrid keeps big tenants separate and the partition count small.

3The Standard: List for the Big Ones, Hash for the Rest

Our standard is a hybrid. The parent table is list-partitioned by tenant_id. Each huge tenant gets its own partition, and everyone else falls into the default partition, which is itself hash-partitioned into, say, 16 parts so the small tenants' data stays in manageable pieces.

SQL
CREATE TABLE invoices (
tenant_id bigint NOT NULL,
id bigint NOT NULL,
created_at timestamptz NOT NULL,
total numeric(12,2),
PRIMARY KEY (tenant_id, id)
) PARTITION BY LIST (tenant_id);
CREATE TABLE invoices_t17 PARTITION OF invoices FOR VALUES IN (17); -- one of the 5 huge tenants
CREATE TABLE invoices_t42 PARTITION OF invoices FOR VALUES IN (42);
CREATE TABLE invoices_small PARTITION OF invoices DEFAULT PARTITION BY HASH (tenant_id);
CREATE TABLE invoices_small_0 PARTITION OF invoices_small FOR VALUES WITH (MODULUS 16, REMAINDER 0);
-- ... remainders 1 to 15

Notice that the primary key starts with tenant_id. A unique constraint on a partitioned table must include the partition key, and putting tenant_id first also makes every index naturally scoped by tenant. The huge tenants' partitions can be tuned separately, with their own autovacuum settings and, if needed, a second level of range partitioning by created_at for retention.

4Keeping the Partition Count Under Control

Partitions are not free. For each query on the parent, the planner considers every partition that cannot be pruned, and at execution PostgreSQL may need to lock each partition the plan touches. With thousands of partitions, planning time can grow from under a millisecond to tens of milliseconds, and some operations, such as generic plans for prepared statements, can end up locking far more partitions than they read, which strains the lock table.

So the standard sets a budget: keep each table in the hundreds of partitions, low thousands at most, counting all levels. Before adopting a layout, we test it: create the real number of partitions on a test cluster and measure planning time with EXPLAIN (SUMMARY) for the common queries. A new huge tenant gets its own partition only when it passes a size threshold, for example 5 percent of the table, and that decision is part of the standard, not ad hoc.

SQL
EXPLAIN (SUMMARY) SELECT * FROM invoices WHERE tenant_id = 42 AND id = 900;
-- Index Scan using invoices_t42_pkey on invoices_t42
-- Planning Time: 0.31 ms

5Tenant Isolation and the Tenant Predicate

Pruning only works if the query includes the partition key. A query that forgets tenant_id scans every partition, which is slow, and, worse, returns other tenants' data. So the standard requires a tenant predicate on every query, and enforces isolation with row-level security as a safety net: a policy that only shows rows of the tenant set for the current transaction.

SQL
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON invoices
USING (tenant_id = current_setting('app.tenant_id')::bigint);
-- Each request, inside its transaction (safe with PgBouncer transaction mode)
SET LOCAL app.tenant_id = '42';
SELECT count(*) FROM invoices; -- only tenant 42's rows
SELECT * FROM invoices WHERE tenant_id = 42 AND id = 900; -- explicit key: pruned at plan time

In a Spring service, an interceptor or a repository base class sets app.tenant_id at the start of each transaction from the authenticated request. The policy guarantees isolation, but we still write tenant_id = ? in queries, because an explicit constant lets the planner prune at plan time, while the policy's setting is only known at execution time. RLS does not apply to the table owner or superusers by default, so the application connects as a separate role.

6Moving Big Tenants and Growing Beyond One Cluster

The layout makes a big tenant easy to move. If tenant 17 grows until it needs its own cluster, its data is already in its own table. We copy invoices_t17 to the new cluster with logical replication, publishing that partition alone, let it catch up, switch the tenant's traffic to the new cluster during a short pause, and then detach and drop the partition on the old cluster. The other tenants are untouched.

When one cluster is no longer enough for everyone, the next step is sharding. Citus distributes tables by tenant_id across worker nodes and routes each tenant's queries to its node, which fits this data model directly because every table already carries tenant_id. Application-level sharding, a routing table from tenant to cluster, is the alternative when we want plain PostgreSQL everywhere. Both are easier because the standard put tenant_id at the front of every key from the start.

Step throughLife of a tenant under the standard
  1. New tenant signs uplands in the hashed shared partition
  2. Tenant above 5 percent of the table?
    YesGive it its own list partitionNoStay in the shared partition
  3. Tenant needs its own resources?
    YesLogical replication to a new clusterNoStay on the shared cluster
  4. Switch traffic, detach and drop the old partitionother tenants untouched

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

7Follow-up questions

How do you move a tenant from the shared partition to its own partition?

Create a new list partition for that tenant as a separate table, copy the tenant's rows into it in batches, then in one short transaction delete them from the shared partition and attach the new table for that value. Because attaching a list partition with a default checks the default for matching rows, the rows must be gone from the default first. Doing it during low traffic keeps the locks brief.

Why not give every tenant its own schema instead?

Schema per tenant gives strong separation, but 5,000 schemas mean 5,000 copies of every table and index, very large catalogs, and migrations that must run 5,000 times. It works for hundreds of tenants with few tables, not for this shape.

Does row-level security slow queries down?

It adds the policy predicate to every query on the table, which is cheap when tenant_id leads the indexes, as it does here. The bigger risk is a policy function that is not simple; current_setting is stable and cheap.

8Conclusion

In this answer, we designed a partitioning standard for 5 huge and 5,000 small tenants: list partitioning by tenant_id with a partition for each huge tenant and a hash-partitioned default for everyone else, tenant_id first in every primary key, and a partition budget in the hundreds to low thousands tested for planning time. Every query carries a tenant predicate, backed by row-level security, because pruning and isolation depend on it. Big tenants can later move to their own cluster by replicating and detaching their partition, and Citus or application-level sharding takes over when one cluster is not enough.

What to say in the interviewAbout 90 seconds aloud

Open with

I would not give every tenant a partition, because 5,000 partitions make planning and locking expensive. My standard would be list partitioning by tenant_id, with one partition for each of the 5 huge tenants, and a default partition for everyone else that is itself hash partitioned, say 16 ways, so the small tenants are spread into manageable pieces.

Then, if they want more

That keeps the partition count in the dozens, and I would keep any table within the hundreds to low thousands and test planning time with the real count. tenant_id goes first in every primary key, which partitioning requires anyway, and it scopes every index. The big tenants' partitions can get their own autovacuum settings or a second level by date.

Pruning only works when the query includes tenant_id, so every query must carry it, and I would add row-level security with a policy on a per-transaction setting, set with SET LOCAL in the Spring service, as a safety net for isolation.

If a huge tenant needs its own cluster, its data is already in its own table, so I replicate that partition with logical replication, switch its traffic, and detach and drop it. When one cluster is no longer enough overall, Citus or application-level sharding by tenant_id is the next step, and the data model is already ready for it.

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.
Official documentation
PostgreSQL documentation
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 "Design a partitioning standard for multi-tenant SaaS data in PostgreSQL where 5 tenants...": https://scrivio-interview-library.netlify.app/databases/postgresql/6865e8b2f8/