Run vector similarity search and RAG on PostgreSQL
Unlock the complete study guide + 1,040 practice questions across 16 full exams.
Bundled into the existing Developing AI Cloud Solutions on Azure premium course — no separate purchase.
14-day money-back guarantee — no questions asked.
Included in this chapter:
- Writing the retrieval query
- Distance, not score
- Getting the embedding into the column
- Generating embeddings inside the database
- Filtering: predicates, joins and the post-filter cost
- Hybrid search: fusing lexical and vector rankings
- Reranking the shortlist
- The whole round trip: chunk, embed, retrieve, ground
- Exam-pattern recognition
Choosing the distance operator the query will use
| Decision axis | <-> Euclidean (L2) distance | <=> cosine distance | <#> negative inner product |
|---|---|---|---|
| What Microsoft says it computes | The straight-line distance between two vectors in the n-dimensional space | Derived from the cosine of the angle between two vectors; Azure OpenAI embeddings rely on cosine similarity to compare documents and a query | The inner product of the two vectors, returned negated so that smaller still means nearer |
| Operator class the index must be built with | vector_l2_ops | vector_cosine_ops | vector_ip_ops |
| Function form of the same computation | l2_distance(vector, vector) | cosine_distance(vector, vector) | inner_product(vector, vector) |
| Reading the value as a similarity | No conversion is published; rank on the distance itself | 1 - (embedding <=> query) | (embedding <#> query) * -1 |
Decision tree
Cheat sheet
Unlock with Premium — includes all practice exams and the complete study guide.
Also tested in
References
- Vector search in Azure Database for PostgreSQL flexible server (pgvector)
- Create a semantic search with Azure OpenAI in Azure Database for PostgreSQL
- PostgreSQL: CREATE INDEX (access methods and operator classes)
- Improve accuracy with advanced RAG architectures: chunking, hybrid search, reranking
- Choose an Azure service for vector search (capability matrix) Well-Architected
- PostgreSQL: CREATE CAST (explicit, assignment, and implicit cast contexts)
- Generate embeddings for vector search in Azure AI Search
- Understand the RAG pattern with Azure Database for PostgreSQL
- Generate vector embeddings with Azure OpenAI in Azure Database for PostgreSQL
- PostgreSQL: Introduction to full text search (tsvector, tsquery, the @@ operator)
- PostgreSQL: Controlling text search (ranking with ts_rank and ts_rank_cd)
- PostgreSQL: Preferred index types for text search (GIN versus GiST)
- AI functions in the azure_ai extension (preview), including azure_ai.rank()