A RAG app stores document chunks in PostgreSQL and must return the five most similar chunks, but only from documents a tenant owns. Which table and query design fits?
- Exam domain
- Develop AI solutions by using Azure data management services25–30% of the exam
- Difficulty
- Hard
- In this mock
- 18 questions on this domainPractise this domain
Why the exam asks this
Objective in the exam guide: Run vector similarity search, including storing embeddings, semantic retrieval, and implementing retrieval-augmented generation (RAG) patterns by using metadata filter.
Real RAG always filters by tenant, product or date. The exam checks you can model the table and write the filtered query.
Why C is the answer
A table with tenant_id, content text and embedding vector(1536), a B-tree index on tenant_id and an HNSW index on embedding, and a query WHERE tenant_id = $1 ORDER BY embedding <=> $2 LIMIT 5
A vector column with fixed dimensions can be indexed for similarity, a regular index supports the tenant filter, and one query combines the metadata filter with nearest-neighbor ordering.
Remember: RAG with filters: typed vector column, index both, filter and order in one query.
How it works
Model chunks with metadata columns (tenant_id, source), the text, and an embedding column typed as vector(n).
Index the filter column with a regular index and the embedding with HNSW, IVFFlat or DiskANN using the matching operator class.
One SQL query applies the WHERE filter and ORDER BY embedding <=> query vector LIMIT k.
Clues in the question
The words that point to the answer, and what each one tells you.
- Five most similar
- ORDER BY distance LIMIT 5.
- Only from documents a tenant owns
- Metadata filter.
In detail
- A column must have dimensions defined, such as vector(1536), to be indexed; pgvector indexes support up to 2,000 dimensions.
- The <=> operator needs an index built with vector_cosine_ops.
Side by side with look-alikes
| Element | Purpose |
|---|---|
| vector(1536) column | Typed, indexable embedding |
| B-tree on tenant_id | Fast filter |
| HNSW on embedding | Fast nearest neighbors |
| WHERE + ORDER BY <=> + LIMIT | Filtered similarity in one query |
Every option, checked
Knowing why each wrong option fails is how you rule it out on exam day.
A. One table per tenant with no indexesWrong
Unindexed tables mean full scans.
B. Filter tenants in application code after returning all chunksWrongMost tempting
Returning all chunks leaks data across tenants and wastes work.
Why it tempts: It gives the right final answer. But it moves every tenant's data to the app, which is slow and a security risk.
C. A table with tenant_id, content text and embedding vector(1536), a B-tree index on tenant_id and an HNSW index on embedding, and a query WHERE tenant_id = $1 ORDER BY embedding <=> $2 LIMIT 5Right
RAG with filters: typed vector column, index both, filter and order in one query.
D. Store embeddings as text and compare them with LIKEWrong
Text comparison cannot measure similarity.
In practice
- For very selective filters, check the plan; the planner may filter first and skip the vector index.
Use case
An illustrative scenario, not a real incident.
A legal assistant searches only one firm's documents and returns the five closest chunks in milliseconds.
See how it works
flowchart LR Q["Question embedding"] --> S["SELECT content FROM chunks"] S --> W["WHERE tenant_id = 42"] W --> O["ORDER BY embedding <=> query"] O --> L["LIMIT 5"] L --> P["Prompt for the model"]
How to read this chart
- Read it in order: Question embedding, then SELECT content FROM chunks, then WHERE tenant_id = 42, then ORDER BY embedding <=> query, then LIMIT 5, then Prompt for the model.
Related questions
Learn more
- Our lesson: Data services for AI
- Practise Develop AI solutions by using Azure data management services questions in the mock exam
- Source: Vector search in Azure Database for PostgreSQL
- Source: Optimize performance when using pgvector
- Cosmos DB indexing policies
- Cosmos DB change feed
- pgvector on Azure PostgreSQL