To keep one customer's documents out of another customer's AI answers, enforce the separation inside the database, on every table the search reads, and prove it with tests. A prompt that says "only use this customer's files" is not a control. In a retrieval-augmented generation (RAG) assistant, a search step finds the passages most similar to the question, and a language model writes the answer from them. If the search can see every customer's rows, the model can quote any of them. Here, a tenant means one customer account in your software. This post covers where leaks happen, how to isolate tenants in Postgres with pgvector, the traps that switch protection off, and a test to run before launch.
Where can one customer's text reach another customer's answer?
Search ranks by similarity. It does not know who is allowed to read what. The closest match to "what is the notice period?" may be a clause in another customer's contract. Relevant is not the same as allowed. OWASP, the security community behind the Top 10 list for LLM applications, names this risk in its 2025 list as vector and embedding weaknesses (LLM08). It warns that when several user groups share one vector database, queries can expose another group's data, and it recommends permission-aware stores and strict separation by group.
The search is only one place. Check each of these. The list is general engineering practice, not taken from one source.
- Search query: one missed tenant filter in one code path returns everyone's rows.
- Answer cache: a cache keyed only on the question text can hand customer A's answer to customer B, who asked the same thing. Put the tenant in the cache key.
- Prompt context: chat history or saved memory held in shared state instead of per tenant.
- Logs and traces: prompts and retrieved passages are copies of customer data. Anyone who can read the logs can read them.
- Background jobs: an ingest job that loses the tenant id writes chunks with no owner or the wrong one.
- Database sessions: a pooled connection that still carries the previous request's tenant setting.
- Backups: a dump restored into a shared test environment.
Which isolation option should you choose?
Render, a hosting company, compares the options in its multi-tenant RAG guide, published on 29 September 2026. It is a vendor article and partly promotional, but the points we use agree with the PostgreSQL and pgvector documentation we read.
- Shared tables with row-level security (RLS). Every row carries a tenant_id, and the database refuses to return rows from other tenants. It is the cheapest to run and fits many small customers. Safety depends on the policies and on how the app connects.
- Partitions by tenant. Render suggests PARTITION BY LIST (tenant_id) with a vector index on each partition. The query must include the tenant id so Postgres touches one partition. Each new tenant adds planning and catalog cost, so in our reading it suits fewer, larger tenants.
- A separate database per tenant. Render describes moving a tenant that needs physical isolation onto its own stack. It is the strongest separation and the highest cost: you run and migrate many databases.
A common plan, and our general advice: shared tables with RLS for the many small customers, and a dedicated database for the few whose contracts demand one. Whichever you pick, put tenant_id on both the document table and the chunk table. Add a composite foreign key so a chunk cannot point at another tenant's document. Render recommends this too, along with generated ids instead of ids that carry meaning, so one tenant cannot learn hidden data from duplicate-key errors.

What are the Postgres traps that switch protection off?
Row-level security can fail quietly: the query still runs and returns rows. These are the traps to check.
- You are the table owner. Per the PostgreSQL documentation, table owners normally bypass row security unless the table has FORCE ROW LEVEL SECURITY. If your app connects as the role that created the tables, your policies do nothing. Render calls FORCE mandatory. As general advice, the app should use a role that does not own the tables.
- A privileged role. Superusers and roles with the BYPASSRLS attribute always bypass row security. Never connect the app with one. Backups are the exception: Render notes that pg_dump turns row security off by default, so a backup role needs BYPASSRLS. Keep that role away from the app.
- A loose policy. With RLS on and no policy, Postgres denies everything, which is safe. The opposite mistake is a policy that allows everything, such as USING (true). Our post on RLS switched on but data still public lists such patterns.
- Pooled connections. A transaction pooler such as PgBouncer assigns a server connection to a client only during a transaction, and its documentation marks plain SET as not working in that mode. A tenant set at session level can stay on the connection and serve the next request, which may belong to another tenant. Render says to keep the tenant transaction-local: set_config('app.tenant_id', value, true) applies the value only for the current transaction. Read the tenant id from the signed-in user's token, never from the request body.
- An unset tenant. current_setting('app.tenant_id', true) returns NULL when the setting is missing, and a comparison with NULL matches no rows, so the query should return nothing instead of everything. That is our reading of the documentation, so test it.
- Side channels and clever policies. Unique constraints and foreign keys bypass row security by design, and the PostgreSQL documentation warns this can leak information through "covert channels". Render also warns that sub-SELECTs in a policy can degrade plans. Keep the policy to one check on the row's own tenant_id, which the PostgreSQL documentation calls the simplest and best-performing case.

Why does a small customer get poor search results?
Filters can make a leak-proof system look broken. With an approximate vector index such as HNSW, pgvector applies the filter after the index scan. Its README says that if a condition matches 10% of rows, then with the default hnsw.ef_search of 40, only about 4 rows will match on average. A small tenant is exactly that case. The app asks for ten passages, gets four, and the answers feel thin. Do not loosen the tenant filter to fix that. Fix the recall instead.
- Iterative index scans. Added in pgvector 0.8.0, they scan more of the index until enough results are found or hnsw.max_scan_tuples (20,000 by default) is reached. Render says the setting is off by default. The README shows hnsw.iterative_scan = strict_order for exact distance order and relaxed_order for slightly out-of-order results with better recall.
- A B-tree index on tenant_id. Render suggests it when a tenant owns a small share of the rows, because ranking inside that slice is exact.
- A partial index per tenant, or partitions. The pgvector README suggests partial indexes when filtering by a few distinct values and partitioning when there are many.
The 10% figure is the README's example, not a rule for your data. Measure recall for your smallest tenant.
How do you test for leaks before a customer finds one?
Write a test that tries to leak. Create two test tenants. Give each a canary: a document with a made-up phrase that appears nowhere else, such as a nonsense product code. Sign in as tenant A and ask questions built to pull out tenant B's canary. The test fails if the phrase shows up in the answer, the cited sources, the logs or the cache. Run it in continuous integration (CI), the automatic test run on every change, with the same pooler settings as production. This is general practice.
We run SQL ownership tests in CI on Mandizo, a GST invoice generator. Our post on row-level security lessons from a real client app shows what such tests look like, and the Mandizo case study gives context. Mandizo is not a document-AI product, so only the testing approach carries over.
Ask your developer to show each of these passing. The first five come from Render's release checks.
- A valid token for tenant A cannot read tenant B's document or chunk ids.
- Updates and deletes across tenants fail when the tenant id is missing or malformed.
- The tenant setting is transaction-local behind a transaction pooler.
- FORCE ROW LEVEL SECURITY is on, and the BYPASSRLS backup role is separate from the app role.
- Unique constraints and foreign keys cannot reveal another tenant's data.
- Cache keys, chat history and log access are per tenant (our addition).
- PostgreSQL and pgvector run current minor versions (Render's advice).
- The canary tests pass in CI (our addition).

When can you skip most of this?
Not every AI assistant needs this work. You can skip most of it in these cases.
- Every customer already has a separate deployment and database. There is no shared table to filter. The cache, log and backup checks still apply.
- The assistant searches only content that every user may read, such as public help pages.
- It is a prototype with one internal user and no customer data.
The moment a second customer's private files enter the same index, come back to this list. RLS is also not the only way to enforce separation: other vector stores offer their own access controls. The principle stays the same: the store enforces permissions, not the prompt. If you are still deciding whether you need RAG at all, start with our RAG or long context decision guide.
Want a second review of your document-AI retrieval path?
If your customers chat with their own documents, we can review the retrieval path: tenant model, database roles, pooling, caches, logs and leak tests. This is advisory and scoping work. You can request a review of your retrieval path or contact us. To prepare, read our questions to ask before adding an AI feature to your SaaS. If a leak has already happened, treat it as an incident and follow a written plan such as our 72-hour breach response plan.
Common questions
Is row-level security enough to keep tenants apart in RAG?
Not on its own. It stops the database returning another tenant's rows, but only if it is forced for the table owner, the app does not connect as a bypass role, and the tenant is set per transaction behind a pooler. You still need per-tenant cache keys, chat history and log access, plus leak tests that run in CI.
Does a prompt instruction stop an AI assistant from leaking another customer's data?
No. A prompt asks the model to behave. It does not stop the search from fetching the wrong passages. Once another customer's text is in the model's context, the model can repeat it. Control access before retrieval, in the database, so the wrong rows never reach the model.
Why does my small customer get fewer or worse answers with the filter on?
With an approximate index such as HNSW in pgvector, the filter runs after the index scan, so a tenant with a small share of rows can get far fewer results than requested. The pgvector README gives about 4 rows when 10% of rows match at default settings. Iterative scans, a B-tree on tenant_id or partitions can help.



