Enterprise RAG Often Fails on Data Boundaries Before Model Quality
In enterprise RAG, the architecture question is not only which model answers best. The system must make tenant boundaries, permissions, retrieval, and operational evidence verifiable.
Model quality sets an upper bound on answers, but data isolation, auditability, and the ability to evolve safely determine whether a prototype can become a production service.
What Does a Headless RAG Architecture Connect?
A Headless architecture separates client experience from reusable knowledge and AI services while keeping identity, authorization, audit, and data scope in one governance layer.
PROCESS MAP
Headless RAG Multi-Tenant Architecture
Multiple client channels enter a common identity and tenant-governance layer before reaching enterprise knowledge and model services.
- 01STEP
Client channels
Corporate websites, mobile apps, messaging bots, and internal systems
- 02STEP
Headless API
JWT or API-key validation, tenant_id, RBAC, and audit controls
- 03STEP
Data and retrieval
Object storage, queues, PostgreSQL RLS, pgvector, and hybrid retrieval
- 04STEP
Generation and response
LLM generation, citations, Request ID, and SSE streaming
Which Multi-Tenant Data Architecture Should You Choose?
Higher isolation generally increases infrastructure, upgrade, and operating costs.
DECISION TABLE
Multi-Tenant Data Architecture Comparison
Higher isolation generally increases infrastructure, upgrade, and operating costs.
| Evaluation | Shared Schema + RLS | Schema-per-tenant | Database-per-tenant |
|---|---|---|---|
| Implementation | Shared tables separated by tenant_id | One schema per tenant in one database | One database per tenant |
| Isolation | Medium to high, depending on RLS | High | Highest |
| Unified upgrades | Easiest | More complex as tenants grow | Most complex |
| Infrastructure cost | Low | Medium | High |
| Scale for many tenants | Best fit | Moderate | Higher cost |
| Per-tenant backup and restore | More complex | Moderate | Clearest |
| Typical fit | SaaS, SMB, standardized services | Enterprise customization | High compliance, sovereignty, large enterprises |
How should the isolation model be chosen?
Choose the isolation layer from sensitivity, customization, backup and restore requirements, data-residency obligations, and operating cost—not from a single universal rule.
How PostgreSQL RLS Establishes Tenant Isolation
PostgreSQL Row-Level Security can restrict reads and writes according to the tenant context of the current database transaction. It adds a database-level control when application code misses a tenant filter.
1CREATE EXTENSION IF NOT EXISTS vector;23CREATE TABLE knowledge_chunks (4 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),5 tenant_id UUID NOT NULL,6 document_id UUID NOT NULL,7 content TEXT NOT NULL,8 embedding vector(1536),9 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()10);1112ALTER TABLE knowledge_chunks ENABLE ROW LEVEL SECURITY;13ALTER TABLE knowledge_chunks FORCE ROW LEVEL SECURITY;1415CREATE POLICY tenant_select_policy16ON knowledge_chunks FOR SELECT17USING (tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')::uuid);1819CREATE POLICY tenant_insert_policy20ON knowledge_chunks FOR INSERT21WITH CHECK (tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')::uuid);
What can RLS protect, and what can it not replace?
DECISION TABLE
PostgreSQL RLS: Control Boundaries
PostgreSQL Row-Level Security can restrict reads and writes according to the tenant context of the current database transaction. It adds a database-level control when application code misses a tenant filter.
| Control | What RLS handles | What still needs another control |
|---|---|---|
| Row-level access | Yes | Policy tests and least privilege |
| Missing tenant_id filter in application code | Risk can be reduced | Set SET LOCAL inside every request transaction |
| Connection pools and session variables | Not handled automatically | Test the real pool mode and explicit transaction boundary |
| BYPASSRLS or superuser | No | Runtime role must be NOSUPERUSER and NOBYPASSRLS |
| Default owner bypass | Conditional | Use a non-owner runtime role and FORCE RLS where appropriate |
| JWT validation | No | Validate in the API gateway or backend |
| Storage, queues, caches, and prompts | No | Apply tenant-aware policies and keys end to end |
How do connection pools and roles keep RLS effective?
An RLS policy does not create a trustworthy tenant boundary on its own. Every request must set tenant context and run tenant-scoped reads, writes, and retrieval in the same explicit transaction with a runtime role that cannot bypass RLS.
1BEGIN;2SET LOCAL app.current_tenant_id = 'tenant-uuid';34SELECT id, content5FROM knowledge_chunks6ORDER BY embedding <=> :query_embedding7LIMIT 8;89COMMIT;
SET LOCAL only applies inside the current transaction. Transaction pooling can support this pattern when BEGIN, SET LOCAL, and all queries stay together; statement pooling cannot. Superusers and BYPASSRLS roles always bypass RLS, while table owners bypass it by default unless FORCE ROW LEVEL SECURITY is enabled.
How Should a Headless RAG API Behave?
A Headless API should remain stateless and derive tenant and permission scope from trusted credentials. The client must never choose or override tenant_id.
Document Upload API
1POST /api/v1/documents2Authorization: Bearer <access_token>3Content-Type: multipart/form-data
1{2 "document_id": "d3b07384-d113-4956-a5cc-9c60012443d3",3 "status": "processing",4 "message": "The document is queued for parsing and indexing"5}
Conversation and Retrieval API
1POST /api/v1/chat/completions2Authorization: Bearer <access_token>3Content-Type: application/json
1{2 "messages": [{ "role": "user", "content": "How is annual leave calculated?" }],3 "stream": true4}
Enterprise responses should include source documents, versions, updated time, Request ID, and explicit states for no evidence or insufficient permission.
How Do Documents Enter the RAG Knowledge Base?
Ingestion should be asynchronous so parsing, OCR, chunking, and embedding do not occupy the original HTTP request.
PROCESS MAP
Knowledge-Base Ingestion Flow
Upload requests are separated from parsing, chunking, and embedding work so clients do not wait for long-running processing.
- 01STEP
Upload and validate
Validate file type, size, identity, tenant_id, and permissions.
- 02STEP
Store and schedule securely
Write originals to tenant-isolated storage and send work to a queue.
- 03STEP
Parse and chunk
Workers run OCR and layout parsing, producing traceable chunks from document structure.
- 04STEP
Embed
Call the embedding service and retain model-version and source metadata.
- 05STEP
Write through RLS
Set tenant context, write text and vectors, and update processing status.
Chunking Should Follow Document Structure, Not a Fixed Character Count
Use headings, clauses, semantic boundaries, and document type to create chunks that preserve source and version traceability.
DECISION TABLE
Chunking Strategy by Document Type
Chunking should keep the original structure and traceable metadata.
| Document type | Chunking basis | Required metadata |
|---|---|---|
| FAQ | One question and answer pair | Category, product, version |
| Policy | Article, paragraph, clause, and headings | Effective date, version, owner |
| Contract | Clauses and attachments | Version, parties, term |
| Product manual | Chapters, functions, and steps | Model, software version, page |
| Meeting record | Topics, decisions, and actions | Date, attendees, owner |
How Should Embedding Dimensions Evolve?
vector(1536) is a fixed data contract, not a placeholder for arbitrary future models. A new model can change both semantic space and output dimensions, so model name and version alone are not a migration plan.
DECISION TABLE
Embedding Dimension and Model Migration
vector(1536) is a fixed data contract, not a placeholder for arbitrary future models. A new model can change both semantic space and output dimensions, so model name and version alone are not a migration plan.
| Strategy | When it fits | Required controls |
|---|---|---|
| New embeddings table | A model or dimension changes | Dual-write, re-embed, build the new index, then switch reads |
| New vector column | A short dual-track period in one chunk table | Maintain separate dimensions, indexes, and rollback window |
| Unconstrained vector column | Long-term coexistence of several models | Use model filters plus dimension-specific partial or expression indexes |
| Overwrite the existing column | No legacy reads or a planned outage | Complete re-embedding, index rebuild, and rollback backup first |
1-- Example: a v2 model outputs 1024 dimensions2CREATE TABLE knowledge_chunk_embeddings_v2 (3 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),4 tenant_id UUID NOT NULL,5 chunk_id UUID NOT NULL,6 embedding_model TEXT NOT NULL,7 embedding vector(1024) NOT NULL,8 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()9);1011CREATE INDEX CONCURRENTLY knowledge_chunk_embeddings_v2_hnsw12ON knowledge_chunk_embeddings_v213USING hnsw (embedding vector_cosine_ops);
How Does RAG Retrieve and Generate an Answer?
Each complete query establishes identity and tenant scope before hybrid retrieval, reranking, model generation, citations, and audit logging.
PROCESS MAP
User Query and RAG Retrieval Flow
Each query establishes identity and tenant scope before retrieval, generation, and audit.
- 01STEP
Validate request
Read tenant_id, user_id, and scope from a trusted token.
- 02STEP
Limit data scope
Set tenant context in the transaction; PostgreSQL RLS isolates rows.
- 03STEP
Retrieve and rank
Combine vectors, keywords, metadata filters, and reranking.
- 04STEP
Compose and generate
Combine policy, enterprise knowledge, and the question for the LLM.
- 05STEP
Stream and audit
Return answers and citations through SSE; record model, latency, and Request ID.
Why Does Enterprise Knowledge Need Hybrid Retrieval?
Enterprise knowledge mixes semantic questions with product codes, regulations, clause numbers, and exact values. Hybrid retrieval combines vector search, keyword search, metadata filters, and reranking.
DECISION TABLE
Enterprise Retrieval Capability Comparison
Enterprise knowledge mixes semantic questions with product codes, regulations, clause numbers, and exact values. Hybrid retrieval combines vector search, keyword search, metadata filters, and reranking.
| Capability | Vector only | Keyword only | Hybrid retrieval |
|---|---|---|---|
| Semantic similarity | Strong | Weak to moderate | Strong |
| Product codes | Potentially unstable | Strong | Strong |
| Regulatory clause numbers | Potentially unstable | Strong | Strong |
| Synonyms | Strong | Requires a dictionary | Strong |
| Exact numeric values | Weaker | Strong | Strong |
| Metadata permissions | Supported | Supported | Required |
How Should Vector Indexes Be Maintained After Tenant Deletion?
Complete tenant deletion is a compliance requirement, but high-churn deletion also changes index workload. Monitor index size, latency, recall, and maintenance time—especially for HNSW—rather than checking only that rows disappeared.
DECISION TABLE
Vector Index Operations Checklist
Complete tenant deletion is a compliance requirement, but high-churn deletion also changes index workload. Monitor index size, latency, recall, and maintenance time—especially for HNSW—rather than checking only that rows disappeared.
| Event or signal | Operating action | Verification metric |
|---|---|---|
| Tenant offboarding or bulk deletion | Delete in batches and schedule index/statistics maintenance | Table and index size, completion rate, query latency |
| Slow HNSW vacuum | REINDEX INDEX CONCURRENTLY, then VACUUM (ANALYZE) | Maintenance window, disk headroom, write impact |
| Recall or latency regression | Sample approximate results against exact search | Recall, p95 latency, ef_search or probes |
| High-churn tenants | Evaluate partitions or separate tables and indexes | Effect of hot tenants on other tenants |
1REINDEX INDEX CONCURRENTLY knowledge_chunks_embedding_hnsw;2VACUUM (ANALYZE) knowledge_chunks;
What Should Be Checked Before Enterprise RAG Goes Live?
A convincing demo is not a production acceptance test. Keep evidence for the following controls before launch.
Frequently Asked Questions
1. What is a multi-tenant enterprise RAG architecture?
It allows multiple organizations to use one RAG application while isolating their documents, vectors, permissions, and query results across the database, storage, queues, caches, logs, and model inputs.
2. Is PostgreSQL suitable for enterprise RAG?
It is suitable for many small to large RAG workloads because it combines relational data, metadata, full-text search, transactions, and pgvector. The right choice still depends on scale, latency, and operating complexity.
3. What does PostgreSQL RLS protect?
RLS can limit rows according to transaction-scoped tenant context. It reduces the risk of missing application filters, but it must be paired with explicit transactions, least-privilege roles, and connection-pool tests.
4. Does Shared Schema automatically cause data leakage?
No. Leakage risk usually comes from incorrect roles, missing tenant context, or missing negative tests. RLS, least privilege, and cross-tenant tests reduce that risk.
5. When is Shared Schema a poor fit?
Evaluate stronger isolation when customers require independent backup and restore, data residency, different schemas, extensive customization, or strict compliance.
6. Why should ingestion be asynchronous?
Parsing, OCR, chunking, and embedding can take seconds or minutes. Queues and workers prevent request timeouts and improve retries, recovery, and status tracking.
7. How large should RAG chunks be?
There is no universal chunk size. Use document structure, semantic boundaries, clauses, and real questions, then tune with retrieval and answer-quality tests.
8. What is hybrid retrieval?
It combines vector search, keyword search, metadata filtering, and reranking so the system can handle both semantic questions and exact terms, models, clauses, and values.
9. Can RAG eliminate hallucinations?
No. Retrieval can improve grounding, but retrieval failures, stale documents, prompt design, and generation errors remain. Use citations, refusal conditions, and review controls.
10. What is the business value of Headless RAG?
It turns retrieval and generation into governed APIs that websites, apps, messaging bots, and internal systems can reuse with the same data and permission model.
11. Does changing an embedding model require re-embedding?
Usually. Different models can change semantic space and dimensions. Plan a new table or column, parallel indexes, re-embedding, and a controlled read cutover.
12. What proves that enterprise RAG is ready for production?
Evidence should cover tenant isolation, pool transaction scope, role bypass, document versions, real question sets, citations, refusal behavior, performance, dimension migration, and deletion-index operations.
Conclusion: Architecture Is Not About One Universal Technical Answer
For many SaaS RAG products, a Headless API, Shared Schema, PostgreSQL RLS, pgvector, and asynchronous workers are a cost-effective starting architecture.
The important decision is not a single preferred technology. It is whether the complete system can isolate safely, operate reliably, evolve without hidden data risk, and provide evidence for every answer.