Row-level security for SaaS is a database control that filters query results per user, so each person sees only the rows they are allowed to see. In a multi-tenant product it is the mechanism that guarantees Customer A’s dashboard shows Customer A’s data and nothing else, enforced at the data layer rather than trusted to application code to remember every time.

Most SaaS teams ship their first customer-facing dashboard with tenant filtering written in application code. It works, demos well, and passes the first round of code review. Then a developer adds a new endpoint, forgets the tenant clause on one join, and two accounts briefly share a chart. Row-level security exists to make that class of bug structurally impossible instead of rare.

This guide explains what row-level security is, how it compares to the other isolation strategies, where application-level filtering quietly fails, and how to implement it for customer dashboards on PostgreSQL and on a managed analytics layer.


Secured server rack illustrating row-level security isolating tenant data in a SaaS multi-tenant database
Photo: Kevin Ache / Unsplash

What is row-level security in a SaaS context?

Row-level security (RLS) restricts which rows of a table a given database user or session may read or write. The rule lives in the database itself, not in your application. Every query against the table is automatically filtered by the policy, no matter who issued it: your main app, a reporting tool, a cron job, or a developer poking around with psql.

In a multi-tenant SaaS product, the pattern looks like this:

  • Every tenant-owned row carries a tenant identifier such as organization_id, account_id, or tenant_id.
  • The application sets the current tenant in the database session before running any query, usually from the authenticated user’s identity.
  • A security policy on each table filters every read and write to rows matching that session value.

The important property is placement. Because the filter runs inside the database, no query path can skip it by accident. The Azure multitenant architecture guide describes the same principle at the storage layer: isolation mechanisms belong where data is read, not only in the service code above it.

How is row-level security different from the other tenant isolation models?

Isolation for multi-tenant SaaS is a spectrum. Each point trades cost against blast radius, and row-level security sits in the middle for good reason.

ModelHow it isolatesCostBlast radius if bypassed
Separate database per tenantPhysical: one DB instance per customerHighest; ops load grows linearly with customersOne tenant
Schema per tenantLogical: same DB, separate namespacesMedium; migrations multiply by schemasOne tenant (if schema permissions are right)
Shared tables + row-level securityPolicy: one table, filtered per tenantLowest infra cost; policy authoring effortPotentially all tenants if the policy is wrong
Application-level filteringConvention: every query adds a WHERE clause by handNo setup cost at allAll tenants, and it only takes one forgotten clause

Shared tables with RLS is the default choice for most SaaS products below enterprise scale, because infrastructure stays flat while protection moves down a layer. The Azure multitenant overview lays out the same tradeoff: pool resources where tenants are similar, isolate physically only when compliance or performance demands it.

Why does application-level filtering fail in practice?

Application-level filtering means every query that touches tenant data carries a tenant condition written by hand. The failure is not conceptual; it is combinatorial. Every new endpoint, report, background job, export script, and admin panel is a fresh chance to forget the clause. Postgres documents the risk of relying on application discipline directly: row security policies exist because application-level checks “are only effective if all access to the table’s data is forced through them,” which any bypass path (a DBA login, a reporting tool, a leaked connection string) defeats.

The concrete failure modes repeat across SaaS teams:

  • The forgotten clause. A new analytics endpoint joins orders to customers and skips the tenant filter. Two accounts see each other’s numbers in one chart, and support tickets tell you before monitoring does.
  • The direct DB path. The dashboard tool, the BI tool, and the on-call engineer with psql all connect directly, bypassing the ORM where the filters live.
  • The shared cache. A query cache keyed by endpoint, not by tenant, serves Customer A’s response to Customer B. The application code was correct; the cache in front of it was not.
  • The migration gap. A new table ships without tenant scoping and inherits no protection from its neighbors.

Row-level security closes all four at once, because the policy applies to every connection that is not explicitly exempt. OWASP’s Top 10 lists broken access control among the most damaging application risks year after year, and RLS is server-side enforcement of exactly that category: the check cannot be skipped by choosing a different door.

How do you implement row-level security on PostgreSQL?

The canonical implementation is three statements per table plus one session setting per request. Assume every tenant-owned table has an organization_id column and your app connects as a non-superuser role.

-- 1. Enable enforcement on the table
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;

-- 2. Force it for the table owner too, so nothing slips past
ALTER TABLE orders FORCE ROW LEVEL SECURITY;

-- 3. The policy: a session can only touch rows matching its tenant claim
CREATE POLICY tenant_isolation ON orders
    USING (organization_id = current_setting('app.organization_id')::uuid);

Then, at the start of every request, the application sets the tenant context from the authenticated user, not from a client-supplied parameter:

BEGIN;
SET LOCAL app.organization_id = '9f1c-...-a3';  -- set by your server from the session
-- ... run any query you like; results are scoped automatically
COMMIT;

A few production notes that separate a demo from a deployment:

  • Set the claim server-side. The tenant value must come from your authenticated session, never from a URL parameter. A client-controlled tenant id is an IDOR bug wearing a security costume.
  • Use one application role, not per-tenant roles. Policies keyed to a session setting scale to thousands of tenants; per-tenant database roles do not.
  • Index the tenant column. Every filtered query hits it. A composite index starting with organization_id keeps tenant-scoped reads fast as tables grow, and partial indexes handle hot tenants if one account dominates traffic.
  • Exempt roles deliberately. Superusers and BYPASSRLS roles skip policies. Keep that list tiny, audited, and separate from the app connection string.
  • Test it as an attacker. Add a CI test that connects with tenant A’s context, queries tenant B’s rows by id, and asserts zero results. Make the leak test part of the pipeline so regressions fail loudly.

Supabase popularized this exact pattern for Postgres apps by wiring RLS policies to JWT claims, which is why the topic comes up constantly in modern SaaS stacks: the session claim arrives with the token, and the policy reads it.

Where does row-level security fit in an embedded dashboard stack?

Here is the part most dashboards get wrong. Your product enforces RLS on your primary application role, then you connect a dashboard tool directly to the database with a read-only connection that has broad access, because “it’s just reports.” That read-only connection is now the widest door in the system: one shared link serves every tenant’s data to anyone who finds it.

Customer-facing dashboards need the same identity chain as the rest of the product. The viewer authenticates in your app, your server issues a short-lived signed token carrying the tenant claim (JWT is the standard shape for this), and the analytics layer scopes every query by that claim before results reach the browser. A JWT proves identity; it does not enforce data boundaries on its own, which is why the scoping step has to be verified server-side. The JWT authentication guide covers that handoff in detail.

There are three ways to wire a dashboard layer into tenant isolation, listed from most fragile to most robust:

  1. One dashboard per tenant. Isolation is real but operations collapse: hundreds of dashboards to build, migrate, and debug. This never survives past your fiftieth customer.
  2. One shared dashboard, filtered by a tenant parameter in the embed URL. Simple, and only as safe as the parameter. If the id is guessable, every customer can read every other customer’s dashboard by editing one number.
  3. One shared dashboard, scoped by a signed server-side claim. The tenant identity travels in a signed token your server minted from the session, not in a user-editable parameter. This is the model behind proper multi-tenant analytics: one dashboard definition, per-tenant data, enforced at the layer that runs the queries.

The same principle applies to self-hosted BI tools. Metabase ships row-level and role-level security in its paid tiers, which means the scoping rules are authored and maintained by your team, on your upgrade path, next to the query language they apply to. A managed platform that bakes tenant scoping into the embed layer moves that entire surface off your backlog.

What are the common row-level security mistakes?

  • Enabling RLS without writing a policy. Enforced with no permissive policy, RLS denies everything; teams “fix” it by adding a policy that matches all rows and forget to tighten it. Write the real policy in the same migration that enables RLS.
  • Trusting the client’s tenant id. Any endpoint that accepts ?org_id= from the browser and sets it as the session claim has rebuilt the leak one layer down.
  • Forgetting FORCE ROW LEVEL SECURITY. Policies do not apply to the table owner by default. If your app connects as the owner, RLS is decorative.
  • Leaving analytics connections on a BYPASSRLS role. The dashboard connection is the one attackers and curious customers actually reach. It should carry the tenant claim like every other request.
  • No leak test in CI. If you have not proven that tenant A cannot read tenant B through every access path, assume a leak is pending, not absent.

The material and view mechanisms referenced above are documented by PostgreSQL itself: row security policies and the security_barrier view option. For the threat model behind tenant isolation, OWASP lists broken object level authorization in its API Security Top 10: OWASP API1.

Frequently asked questions

Is row-level security enough on its own?

No. RLS scopes rows for connections that obey it. You still need authentication, authorization at the API layer, and scoped credentials for every tool that connects to the database. Treat RLS as the last line of defense, not the only one.

Does row-level security slow queries down?

With a proper index on the tenant column, the policy filter is just a predicate the planner optimizes like any WHERE clause. The performance work is the same as always: composite indexes starting with organization_id, and checking plans for your hot queries.

Should every table have row-level security?

Every table that carries tenant-owned data, yes. Reference tables (plans, feature flags, public catalogs) do not need policies. The discipline is tagging each new table as tenant-owned or shared in review, so the decision is explicit.

How does RLS work when the dashboard tool has its own connection?

The analytics layer must pass a per-viewer tenant claim into the session before running queries, exactly like your application does. If the tool cannot do that with signed claims, it should not connect to production tenant data at all.

Where Embedful fits

If you are building the dashboard layer in-house, RLS plus JWT scoping is a weekend of policy work followed by an ongoing stream of edge cases: new endpoints, cache keys, exports, and every BI connection that needs its own scoped role. Embedful collapses that surface for customer-facing reporting. On Pro ($71 per month billed annually), you get up to 25 customer dashboards with unlimited customer viewers, white-label branding, and direct PostgreSQL, MySQL, or Firebase connections; Agency ($159 per month) raises that to 100 customer dashboards. Dashboards are shared definitions scoped per tenant, so one dashboard serves every customer with only their own rows, and the viewer needs no engineering work.

If your current isolation plan is “the ORM remembers,” start by testing the assumption: connect to your database the way your dashboard tool does, set a tenant context, and try to read another tenant’s rows. If anything comes back, you have found your next sprint item. Create an Embedful account and see how scoped customer dashboards work with no isolation code at all.


Related Articles