Cosmos DB DiskANN vs pgvector: When to Use Which for AI-200
Both are exam-critical topics in AI-200. Both store vectors for semantic search. Choosing between them in a scenario question comes down to four factors — here is the mental model.
The core difference in one sentence
Cosmos DB DiskANN is a globally distributed, schema-less NoSQL store with vector indexing built in — optimised for high-throughput key lookups and low-latency vector retrieval at internet scale. pgvector is a PostgreSQL extension that adds vector types and indexes to a relational database — optimised for combining vector similarity with rich SQL queries (joins, filters, full-text search).
Cosmos DB DiskANN — what you need to know
DiskANN is Microsoft Research's disk-based Approximate Nearest Neighbour algorithm, integrated natively into the Cosmos DB NoSQL API. Key facts for the exam:
- Vector policy is defined at container creation and cannot be changed retroactively. It specifies the path (e.g.,
/embedding), dimensions (e.g., 1536), distance metric, and index type. - Distance metrics supported: cosine, dotProduct, euclidean. Cosine is standard for text embeddings (e.g., Ada-002, text-embedding-3-small).
- Index types: flat (exact, small datasets) and DiskANN (approximate, production scale).
- Partitioning matters: vector queries that include the partition key execute within a single partition (efficient). Without the partition key, the query fans out to all partitions (expensive RU cost).
- Scale: designed for billions of vectors globally with multi-region replication. Strong at low-latency point reads combined with vector search.
- Limitation: no joins, no relational model, no full-text search natively (though Change Feed + Azure AI Search can complement it).
pgvector — what you need to know
pgvector is a PostgreSQL extension that adds the vector type and ANN indexes. On Azure, it runs on Azure Database for PostgreSQL Flexible Server. Key facts:
- IVFFlat: inverted file index. Faster to build, lower memory. Tune with
lists(typically sqrt of row count). Adjust recall at query time withSET ivfflat.probes. - HNSW: Hierarchical Navigable Small World. Slower to build, higher memory, better recall at query time. Tune with
m(max connections per layer) andef_construction. Adjust at query time withSET hnsw.ef_search. - Distance operators:
<->(L2/Euclidean),<=>(cosine),<#>(negative inner product). - Hybrid search: combine pgvector similarity with
tsvectorfull-text search in a single query — a major advantage over pure vector databases. - HALFVEC: half-precision (FP16) storage — halves memory for embeddings with minimal quality loss.
- Pre-filtering: add a
WHERE category = 'X'clause before ORDER BY — pgvector HNSW supports filtered ANN search.
Side-by-side comparison
| Factor | Cosmos DB DiskANN | pgvector |
|---|---|---|
| Data model | Schema-less JSON documents | Relational tables with ACID |
| Best for | High-throughput, globally distributed vector lookup | Complex queries: joins, filters, full-text + vector |
| Scale | Billions of vectors, multi-region | Millions of vectors per instance (scale-up) |
| Hybrid search | Limited (requires AI Search integration) | Native (tsvector + pgvector in one query) |
| Partitioning | Required; cross-partition queries are expensive | Table partitioning optional; filtered indexes help |
| Index tuning | Configured at vector policy creation | IVFFlat (probes) or HNSW (ef_search) at query time |
| Cost model | RU/s based; expensive at high query rates | vCore/hour; predictable for steady workloads |
| Change feed | Built-in (triggers Azure Functions) | WAL-based logical replication |
| Managed identity auth | Entra ID (data plane RBAC preview) | Entra ID token-based auth supported |
How to choose in an exam scenario
The AI-200 exam gives you a scenario and asks which storage solution to recommend. Use these decision rules:
- Global distribution + low latency from multiple regions: Cosmos DB DiskANN
- Semantic search combined with joins, relational filters, or full-text: PostgreSQL + pgvector
- Existing PostgreSQL application adding vector capabilities: pgvector (no new service)
- Schema-less documents with metadata + embedding stored together: Cosmos DB
- Event-driven processing triggered by new embeddings: Cosmos DB (Change Feed → Azure Functions)
- Cost-sensitive, moderate scale, complex analytics: PostgreSQL + pgvector
Frequently asked questions
Can I use both Cosmos DB DiskANN and pgvector in the same application?
Yes, and this is a common pattern. Use Cosmos DB for high-throughput embedding retrieval at scale, and PostgreSQL with pgvector for analytical queries that combine vectors with complex joins and full-text search.
Which is cheaper for 10 million vectors?
pgvector on a mid-tier Flexible Server (General Purpose, 8 vCores, 32 GB RAM) is typically cheaper for read-heavy workloads. Cosmos DB costs depend on RU/s provisioned for vector queries, which can be high for cross-partition fan-out queries on large datasets.
Does the AI-200 exam test DiskANN and pgvector specifically?
Yes. The exam tests understanding of DiskANN index configuration (vector policy, distance metric), Cosmos DB partitioning strategy with vectors, pgvector index types (IVFFlat vs HNSW), and how to choose between them based on a given scenario.