Row-level security in ClickHouse for multi-tenant BI
The three ways to isolate tenants in one warehouse, when each is safe, and the operational cost you actually sign up for.
If you are serving multi-tenant BI on ClickHouse, tenant isolation is the security question you cannot get wrong. Every query has to be provably scoped to the tenant that authored it, an audit-passable path exists in production, and the story survives your customers scaling from 10 to 10,000.
ClickHouse gives you three real mechanisms for row-level isolation: predicate row policies, per-tenant tables, and per-tenant shards. This post walks through each, when each is safe, when each breaks, and the honest operational cost.
The threat model, plainly
Multi-tenant row-level security is protecting against three failures:
- Cross-tenant read. Tenant A’s query returns a row belonging to tenant B.
- Cross-tenant infer. Tenant A’s aggregate query returns a count or sum that includes tenant B’s rows.
- Cross-tenant side channel. Tenant A can observe tenant B’s activity through query timing, error messages, or
EXPLAINoutput.
The three mechanisms below trade off in different ways against these three failures. Which one you pick depends on which failure you can least afford.
Option 1: predicate row policies
ClickHouse ships row policies as a first-class feature. You attach a policy to a table with a predicate, and every query the target role runs gets that predicate rewritten into the WHERE clause automatically.
CREATE ROW POLICY tenant_isolation ON warehouse.fact_orders
USING tenant_id = currentUser()
TO tenant_role;
Every SELECT from fact_orders as tenant_role gets an implicit AND tenant_id = currentUser() appended. The full syntax supports multiple policies per table, permissive-vs-restrictive semantics, and role-scoped attachment.
When this is safe. When your tenants are internal (departments, business units) or your product has under a few hundred tenants and the query mix is simple. The mechanism is auditable - the policy predicate is stored in system.row_policies and can be dumped for compliance review.
When this breaks. Three failure modes:
ALIASandMATERIALIZEDcolumn bypass. Alias columns compute at query time and do not always inherit the row policy consistently across ClickHouse versions. Test your target CH version explicitly.GLOBAL INand distributed queries. Row policies apply on the local shard. AGLOBAL INsubquery evaluated on the initiator can bypass the policy. RestrictGLOBALfor tenant roles.SELECT COUNT(*)from an empty result. A policy that matches zero rows still returns “0”. If “the tenant has zero rows” is itself sensitive information (rare, but real for regulated verticals), predicate policies are not enough.
Operationally, the cost is low. One policy per table plus a role plus a SETTINGS clause that binds the user to the role at session start. Auditing is straightforward because the policies are declarative.
Option 2: per-tenant tables
Each tenant gets its own physical table: orders_tenant_00001, orders_tenant_00002, and so on. Access is via a role that can only see its own tables. Underneath, MergeTree’s file layout is per-partition anyway, so this is not always as expensive as it sounds.
When this is safe. When your tenant count is bounded (fewer than a few thousand), your tenants have wildly different data volumes and query shapes, and you need to prove isolation to a compliance officer who understands filesystems better than SQL semantics.
When this breaks. The metadata cost. Each MergeTree table has metadata that lives in memory. Past a few thousand tables, ClickHouse’s startup time and metadata-refresh cost grows unpleasantly. Query planning for “run this across all my tenants” becomes a fan-out over N tables via a MERGE engine or a UNION, which is slower than one table with a filter.
There is also the schema-migration cost. A rename or column-type change now runs N times, one per tenant. Automation is essential; without it, per-tenant tables are an operational tar pit past a few hundred tenants.
Option 3: per-tenant shards
Each tenant lives on its own shard, a physically separate ClickHouse node. Cross-tenant queries are impossible by physics; a tenant’s queries touch only their own shard’s data.
When this is safe. For very-large tenants who are effectively single-tenant deployments inside a multi-tenant product. Enterprise customers on SaaS BI with strict data-residency clauses often demand this.
When this breaks. Cost. A shard is a full ClickHouse deployment. Ten enterprise tenants at one shard each is ten ClickHouse clusters to operate. You do this only when a customer will pay for it or a regulator will demand it.
The comparison
| Option | Isolation strength | Scales to N tenants | Ops cost | Best for |
|---|---|---|---|---|
| Row policies | Logical, auditable | 10,000+ | Low | Customer-facing SaaS with many small tenants |
| Per-tenant tables | Physical partitioning | 1,000-3,000 | Medium | Fewer tenants, heterogeneous workloads |
| Per-tenant shards | Physical isolation | Tens | High | Enterprise/regulated tenants that pay for it |
Most SaaS BI products end up with row policies for the long tail and per-tenant shards for the enterprise tier. Per-tenant tables are the middle-ground option that tends to be picked when the team is not sure yet - it is a workable middle, not a strong one.
The role model that makes any of this safe
Whichever mechanism you pick, the role model has to be tight. Three rules the on-call engineer will thank you for:
1. Never let a tenant role hold SELECT on system.*. ClickHouse’s system tables leak query text, table names, and column names across tenants. Restrict system.* to admin roles.
2. Bind the tenant identity at session start, not per query. The client sets USE_TENANT_ID = 12345 once per connection; every subsequent query uses that setting. This is how you avoid an application bug that sends the wrong tenant filter.
3. Log every query with the resolved tenant id. system.query_log captures this if you configure SESSION variables to land in the log. That log is your audit trail.
When predicate policies leak, and how to catch it
The one failure mode worth walking through concretely: a GLOBAL IN subquery evaluated on the initiator, without the policy predicate. Here is the shape:
-- Innocent-looking query from a tenant with policy tenant_id = 12345
SELECT count()
FROM fact_orders
WHERE customer_id GLOBAL IN (
SELECT id FROM dim_customers
);
The GLOBAL IN subquery runs once on the initiator with the initiator’s permissions. If the policy is on fact_orders but not on dim_customers, the subquery sees every customer id, including other tenants’. The outer query then joins against a set that leaked cross-tenant.
The fix is either to policy-guard dim_customers symmetrically, or to disable GLOBAL IN for tenant roles via SETTINGS. The ClickHouse docs on distributed subqueries name this hazard explicitly.
The semantic-layer angle
If you are running a semantic layer on top of ClickHouse (see how to build a semantic layer in ClickHouse), the layer is where tenant scoping should live. The pattern that works: the compiler injects tenant_id = ? into every emitted query, the ClickHouse row policy is defence-in-depth, and neither layer is trusted alone.
That is how Goni handles multi-tenant BI - the semantic-layer YAML declares tenant scoping as a first-class policy, the compiler injects it, and the ClickHouse row policy underneath is the second lock. If the compiler ever fails to inject (bug, edge case, deleted policy), the database refuses the query.
Why this matters for a data team
Multi-tenant isolation is not a feature you bolt on. It is a property of the query path, and every mechanism that gives you it costs something - either ops complexity (per-tenant tables and shards) or a subtle threat model (row policies). Pick the mechanism whose failure mode you can afford, layer at least one defence-in-depth on top of it, and log every query with the resolved tenant id. The audit trail is what makes any of this credible to the compliance officer who eventually asks.