Quick revision
The short answer
Yes, tenant_id should lead composite indexes by default: it matches the dominant access pattern, keeps each tenant's entries together, and lets row-level security predicates use the index. The cost falls on cross-tenant queries, which belong on a replica or analytics store rather than shaping OLTP indexes. Partitioning can make tenant_id implied in local indexes only when a partition holds a single tenant, very large tenants need plans tested against their own data, and the standard should be written as defaults plus reviewed exceptions, enforced with migration lint rules.
What to remember
In this answer, we saw that tenant_id should lead composite indexes in a shared multi-tenant schema because it matches the dominant access pattern, keeps each tenant's entries together and lets row-level security predicates become index conditions. Cross-tenant queries pay the price and belong on replicas or analytics stores, partitioning removes the need for tenant_id only in non-unique local indexes on single-tenant partitions, the largest tenants need plans tested against their own data, and the standard works best as defaults plus reviewed exceptions enforced by migration lint rules.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Yes, tenant_id should lead composite indexes by default: it matches the dominant access pattern, keeps each tenant's entries together, and lets row-level security predicates use the index. The cost falls on cross-tenant queries, which belong on a replica or analytics store rather than shaping OLTP indexes. Partitioning can make tenant_id implied in local indexes only when a partition holds a single tenant, very large tenants need plans tested against their own data, and the standard should be written as defaults plus reviewed exceptions, enforced with migration lint rules.
Why this question is asked
In a shared multi-tenant schema, nearly every query starts with 'for this tenant'. When 25 teams add indexes independently, some will forget that, and the result is indexes that serve one team's report and slow everybody's writes, or queries that read other tenants' entries to find their own. A standard settles this once. Interviewers ask this to see whether we can weigh the default against its exceptions: cross-tenant work, partitioning, row-level security and skewed tenants. In this answer, we will see why tenant_id should lead, how it works with RLS, what it costs, how partitioning changes it, the large-tenant problem, and how to write and enforce the standard.
1Why tenant_id Should Lead
In a composite index, the leading column decides where entries are grouped. With tenant_id first, all of one tenant's entries sit together, sorted inside by the next columns. A query for one tenant descends to that tenant's part of the index and reads only it. If created_at led instead, each day's entries would mix all tenants, and a query for one tenant's last day would read every tenant's entries for that day and discard most of them.
Since application queries are almost always for one tenant, tenant_id first matches the dominant access pattern. It also makes the indexes behave the same way for small and medium tenants, keeps each tenant's hot pages close together in the cache, and makes the database's work proportional to the tenant's size rather than to the whole table.
2How It Helps Row-Level Security
With row-level security, PostgreSQL adds the policy condition, such as tenant_id = current_setting('app.tenant_id')::uuid, to every query on the table. current_setting is a stable function, so its value is fixed for the duration of the query, and the planner can use the condition as an index condition on a tenant-leading index. That means the security filter is also the cheapest part of the plan: the query starts by jumping to the tenant's entries.
Notice that the query itself never mentions the tenant. The policy added it, and the tenant-leading index turned it into a seek. If the index led with created_at, the same query would scan all tenants' recent orders and filter them.
3The Cost: Cross-Tenant Queries
The default hurts queries that span tenants: a support tool searching an order number across all customers, an internal dashboard of yesterday's orders, or an analytics job. With tenant_id leading, these queries cannot seek on created_at or order_number alone; they scan or need extra indexes.
The standard's answer is that cross-tenant workloads should not drive OLTP index design. They run on a read replica, which can carry its own indexes only if it is a separate logical copy, or more often in an analytics store fed by change data capture, where columnar storage handles wide scans well. For the few cross-tenant lookups the product genuinely needs, such as finding a tenant from a globally unique invoice id, a single targeted index is an approved exception.
4What Partitioning Changes
Partitioning splits a table into smaller tables by a partition key. With list partitioning, each partition can hold exactly one tenant, which is common for the few largest tenants. Inside such a partition, every row has the same tenant_id, so a local index does not need tenant_id at all; the planner prunes to the right partition first. With hash partitioning, each partition holds many tenants, so tenant_id is still needed in local indexes to separate them.
One more rule from PostgreSQL: unique constraints and primary keys on a partitioned table must include the partition key, so if we partition by tenant_id, unique indexes keep tenant_id regardless. The standard can therefore say: tenant_id leads every index, except local non-unique indexes on single-tenant partitions.
| Table layout | tenant_id in local indexes | Why |
|---|---|---|
| Not partitioned | yes, leading | all tenants share each index |
| Hash partitioned by tenant_id | yes, leading | each partition holds many tenants |
| List partition with one tenant | no, for non-unique indexes | every row has the same tenant_id |
| Any partitioning by tenant_id | yes, for unique indexes | unique keys must include the partition key |
5Very Large Tenants Skew the Plans
Statistics describe the whole table. If one tenant has 40 percent of the rows and the other 2,000 share the rest, a plan that suits an average tenant can be bad for the large one, and the other way round. A query that reads a small tenant's entries through the index in a millisecond may need a different plan, or a different index, for the large tenant's millions of rows.
Prepared statements add a twist in PostgreSQL. After several executions the plan cache may switch to a generic plan that ignores the actual tenant value, which is usually fine for small tenants and occasionally terrible for the big one. The standard therefore requires performance tests with the largest tenant's data volume, not only with fixtures, and allows targeted fixes such as plan_cache_mode = force_custom_plan for specific queries, extended statistics, or moving the largest tenant to its own partition or database.
6Writing and Enforcing the Standard
The standard is short: defaults, the reasons behind them, and how to request an exception. Defaults: tenant_id leads every primary key, unique constraint, foreign key and index; equality columns follow, then the range or sort column. Exceptions, such as a global lookup index or an index on a single-tenant partition, are proposed in the migration and reviewed by a database guild, a group of experienced engineers from several teams who meet weekly.
Enforcement is automatic where possible. A lint rule in CI parses migrations and fails any CREATE INDEX on a tenant table whose first column is not tenant_id, unless the migration carries an approved exception marker. Teams get fast feedback, and the guild spends its time on genuine exceptions rather than routine reviews.
- Parse CREATE INDEX in the migration
- Is the table tenant-scoped?YesCheck the first columnNoApply general rules only
- Does tenant_id lead?YesPassNoLook for an exception marker
- Approved exception recorded?YesPass, link the reviewNoFail with a link to the standard
4 steps in order. Play them, or step through them one at a time.
7Follow-up questions
Does tenant_id first make indexes larger?
Slightly, by the size of the column in each entry, 16 bytes for a uuid. PostgreSQL 13 and later deduplicate repeated leading values in B-tree leaf pages in many cases, which reduces the cost. The benefit for every tenant query far outweighs it.
Should tenant_id be a uuid or a small integer?
A small integer, such as an int, keeps every index entry smaller, which adds up across dozens of indexes. Many systems use an integer internally and keep a uuid or slug as the public tenant identifier.
What about a query that filters on a globally unique column, like an API key?
That is a classic approved exception: a unique index on the key alone, used to find the tenant at login, after which every other query is tenant-scoped again.
8Conclusion
In this answer, we saw that tenant_id should lead composite indexes in a shared multi-tenant schema because it matches the dominant access pattern, keeps each tenant's entries together and lets row-level security predicates become index conditions. Cross-tenant queries pay the price and belong on replicas or analytics stores, partitioning removes the need for tenant_id only in non-unique local indexes on single-tenant partitions, the largest tenants need plans tested against their own data, and the standard works best as defaults plus reviewed exceptions enforced by migration lint rules.
Open with
My default would be yes, tenant_id leads every composite index, primary key, unique constraint and foreign key. Almost all application queries are for one tenant, so with tenant_id first the database jumps straight to that tenant's entries, and the work is proportional to the tenant's size, not the table's.
Then, if they want more
It also fits row-level security: the policy adds tenant_id equals current_setting to every query, and with a tenant-leading index that security filter becomes the index condition. The cost is cross-tenant queries, like support searches or internal reports.
I would not let those drive OLTP indexing; they go to a replica or an analytics store, and the few genuine global lookups, like finding a tenant by API key, become approved exceptions.
Partitioning changes it only in one case: with list partitions holding a single tenant, local non-unique indexes don't need tenant_id, but with hash partitioning each partition has many tenants, and unique indexes must include the partition key anyway.
Large tenants skew statistics and generic plans, so the standard requires performance tests with the largest tenant's data and allows targeted fixes like custom plans or moving them to their own partition. I would write it as defaults plus exceptions reviewed by a database guild, and enforce it with a CI lint rule on migrations.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 13
- 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, MySQL reference manual
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 "Your company is designing the indexing standard for a shared multi-tenant schema used by...": https://scrivio-interview-library.netlify.app/databases/indexing/ca6516c660/