Skip to content
datastudyguides

AI-200 · 2.2.5 · Develop AI solutions by using Azure data management services

PostgreSQL RAG search

Run vector similarity search, including storing embeddings, semantic retrieval, and implementing retrieval-augmented generation (RAG) patterns by using metadata filter

Filter metadata, rank chunks by vector distance, apply a relevance threshold, and pass only grounded context into the prompt.

Retrieve before you generate

RAG flow with PostgreSQL: embed the question, filter and rank chunks with pgvector, return relevant source chunks, then send them with the question to the language model.

PostgreSQL is the retriever in a pipeline. Embed the question, filter to the right metadata, rank chunks by vector distance, and pass only relevant results to the language model.

SELECT id, title, content, embedding <=> $1 AS distance
FROM doc_chunks
WHERE category = 'policy'
  AND embedding <=> $1 < 0.5
ORDER BY distance
LIMIT 5;

A narrows the candidate set. A prevents the query from returning unrelated rows just to fill LIMIT.

Return source metadata such as title and URL with each chunk so the application can cite its grounding. If approximate search misses useful chunks, increase the relevant query-time recall setting.

Key points

  • Top k: `ORDER BY <=> $1 LIMIT k`; smaller is closer
  • : plain WHERE; B-tree index filtered columns, `ANALYZE` after loading
  • : drops weak matches, so the app can return nothing
  • : creates embeddings in SQL with `azure_openai.create_embeddings`

Click an underlined term for a one-line explanation, or see them all in the glossary .

Exam trap

LIMIT alone always returns k rows, relevant or not; without a the model gets unrelated context and may answer from it.

Sources · 3 Microsoft Learn pages Show ▾

Checked on October 4, 2026. Report a problem

Go deeper on Microsoft Learn

Checked against Microsoft Learn on October 4, 2026. Report a problem with this card

How well do you know this?