All insights

AI security and compliance

How do you enforce tenant isolation beyond database rows?

Row level security answers "whose row is this?" Real products also need "which parts of this row may you see?"

Co-founder and CTO, Datics AI6 min read

Short answer

Tenant isolation beyond database rows means enforcing boundaries per field and per role, not only per tenant ID. The same customer record can be fully visible to one party and partly visible to another, so tenant isolation has to cover columns, caches, files, payments, background jobs and every API. Any later feature, including an AI agent for reporting or support, must respect those same boundaries, because it will read whatever your platform lets it read.

On this page
  1. 01Rows are only the first boundary
  2. 02Map the visibility rules before you write code
  3. 03Enforce tenant isolation at every layer
  4. 04Plan for AI before you add it
  5. 05Common isolation mistakes
  6. 06What we see in production
  7. Frequently asked questions

01Rows are only the first boundary

Most multi-tenant products start with a tenant column and a filter on every query. PostgreSQL row security policies make this stronger by enforcing the filter in the database itself, with a default deny when no policy matches. That is a good foundation. It is not the whole answer.

Real B2B platforms often have more than one kind of party looking at the same record. A wholesale company and its dealers, a franchisor and its locations, a platform and its resellers. In these products the question is rarely "may this tenant see this row?" It is "may this role, in this tenant, see this field of this row, for this purpose?"

AWS makes a related point in its SaaS architecture guidance: authentication and authorization are not isolation. A user can be signed in and authorized for a feature and still reach another tenant's data if isolation is not enforced separately, using tenant context on every access.

02Map the visibility rules before you write code

The most useful artifact is a visibility map: for each important entity, which roles see which fields. Draw it before building, and keep it in the repository.

Visibility map showing which fields central admins, dealer admins and end customers can see for each entity
The visibility map also becomes the test plan.
DataOwning partyCentral company adminDealer adminDealer staffEnd customer
Customer profileDealerShipping details onlyFullFullOwn profile
Product catalogCentral companyFullVisibility, pricing and descriptions onlyReadPublished items
OrderDealer, then central for fulfillmentOrder and shippingFullFullOwn orders
PaymentDealer's own accountNo access to fundsSettings and refundsPer roleOwn payments
Design filesCustomer and dealerFor productionFullFullOwn files

A map like this exposes decisions early: who owns the customer, who can change price, who can refund. It also becomes the test plan.

03Enforce tenant isolation at every layer

Rows, fields and roles need enforcement in more places than the main database. The OWASP Multi Tenant Security Cheat Sheet is a good checklist. In practice we cover these layers:

Layers where tenant isolation is enforced: request context, database, API, caches and files, jobs, and logs
Test cross-tenant denial for every role on every release.
  1. 01

    Request context.

    Set the tenant on the server for every request, and treat any tenant ID sent by the client as a selector to verify, not a fact.

  2. 02

    Database.

    Use row security for tenant scoping, and column level grants or separate views for field level rules.

  3. 03

    API responses.

    Shape responses per role, so a field the role may not see is never serialized, not just hidden in the UI.

  4. 04

    Caches and search indexes.

    Include the tenant in every cache key and index filter.

  5. 05

    Files.

    Partition storage by tenant and authorize before issuing signed URLs.

  6. 06

    Payments.

    Where each tenant uses its own payment account, keep the platform out of the money flow and store no card data.

  7. 07

    Background jobs.

    Carry verified tenant context into queued messages and check it again in the worker.

  8. 08

    Logs and exports.

    Include tenant context in audit events and restrict who can read them.

Test cross-tenant denial explicitly. For each role, write tests that try to read another tenant's data and another role's fields, and run them on every release.

04Plan for AI before you add it

AI features are a new path to the same data. A reporting agent, a support assistant or a search tool will read through whatever interface you give it. If that interface was written for internal admins, the agent will see what admins see.

Before adding AI to a multi-tenant product:

  • Give the agent its own access path that applies the same tenant, role and field rules as the user it acts for.
  • Scope retrieval to the current tenant before anything reaches the model.
  • Keep embeddings and indexes per tenant, or filter them by tenant on every query.
  • Log every agent read and write with tenant and user context.
  • Add the agent to your cross-tenant denial tests.

We go deeper on this pattern in how to add AI agents to existing SaaS without rebuilding.

05Common isolation mistakes

When we review multi-tenant products, these are the gaps we find most often:

  • Field rules enforced only in the UI, while the API returns the full record.
  • Admin endpoints that skip tenant filters because they were written for internal staff.
  • Reports and exports built on direct database queries that bypass the application rules.
  • Shared cache keys for data that differs by tenant.
  • Background jobs that run with a superuser connection, which row security does not constrain.
  • Impersonation or support tools without an audit trail.

Each one is easy to miss because the product looks correct from the screens. The fix is to test from the outside: call the API as each role in each tenant and check exactly which fields come back. Repeat those tests whenever a new endpoint, report or integration is added, and treat a failed test as a release blocker.

It also helps to name an owner for the visibility map. When the business adds a new party, such as a reseller or a partner, the map is updated first, and the code follows.

06What we see in production

For a premium custom print company with a wholesale dealer network, we replaced the dealer side of a 15 year old legacy system with a multi-tenant platform where each dealer runs a branded storefront with its own pricing, promotions and payment account, while every order flows back to the company for production and shipping.

Isolation had to work per field and per role, not just per tenant. The same customer is fully visible to the dealer and only partly visible to the central company: central admins see shipping details, not the full profile. The catalog stays central, and dealers control only visibility, pricing and descriptions. Dealers collect payment in their own accounts, so the platform holds no funds and stores no card data, which keeps it out of card data scope.

We added tenant filtering across backend and frontend, role based access for super admin, dealer admin, dealer staff and customer, and recommended snapshotting variant data into each order so later price changes never alter past orders. The platform runs on the client's own AWS account.

There was no AI in the delivered scope, but the scope document asked for an architecture ready for future agent based features such as intelligent reporting and AI customer support. Any such agent will have to respect the same field and role boundaries, which is far easier because they already exist in one place.

Frequently asked questions

Is PostgreSQL row level security enough for tenant isolation?

It is a good foundation, not the whole answer. Row security enforces the tenant filter in the database itself, with a default deny when no policy matches. It does not cover field rules, caches, files, payments or background jobs, and jobs that run with a superuser connection are not constrained by it.

How do we test tenant isolation?

Test from the outside. For each role, write tests that try to read another tenant's data and another role's fields. Call the API as each role in each tenant and check exactly which fields come back. Run those tests on every release, and treat a failed test as a release blocker.

What changes when we add an AI agent to a multi-tenant product?

The agent is a new path to the same data, and it will read through whatever interface you give it. Give it its own access path that applies the same tenant, role and field rules as the user it acts for. Scope retrieval to the current tenant, and add the agent to your cross-tenant denial tests.

How Datics helps

Datics specializes in brownfield AI: bringing production AI into the product you already operate, without forcing a rebuild.

›References (3)
  1. AWS: SaaS Architecture Fundamentals, Tenant isolation
  2. PostgreSQL: Row Security Policies
  3. OWASP: Multi Tenant Security Cheat Sheet

Related work

Case studies from our projects on the same subject.

Premium print products company

Retail & E-commerce · Multi-tenant dealer storefront platform

Branded dealer storefronts where each print dealer is paid into its own account

Dealers could not sell under their own brand on a 15 year old system; we built storefronts with direct payment and a route to production.

ModernizationMulti-tenantPermission-aware
Read case study

Transcripts U.S.

Government, Legal & Defense · Legal transcript marketplace

Court reporting agencies publish transcript metadata without the file ever leaving their browser

Agencies once uploaded full transcripts to the marketplace; now title page details are read in their own browser, and they confirm what is published.

Brownfield AIInside a live productDocument intelligence
Read case study

Regulated investment platform

Financial Services · Private investment marketplace

Pitch decks become drafted deals, and issuers approve each field before publishing

Issuers retyped decks and financials into long deal forms. Now AI suggests each field, and the issuer applies, edits or rejects it.

Brownfield AIAgentic workflowsInside a live product
Read case study

More on AI security and compliance

Your customers already expect AI from your product.

Let us make it real without rebuilding what already works.