Sub topic 2
Choosing a Distance Metric: L2, Cosine, and Inner Product
Assume you know basic PostgreSQL, including SELECT, ORDER BY, and LIMIT, and that an embedding is a numeric vector representing text, images, or other data. This chapter explains how pgvector compares those vectors and how to choose the comparison that matches your model.
Three ways to compare vectors
A vector distance or similarity function produces a score for each stored vector relative to a query vector. The database orders the rows by that score and returns the nearest neighbors.
| Metric | pgvector operator | What it measures | Smaller score means |
|---|---|---|---|
| L2, or Euclidean distance | `<->` | Straight-line distance | More similar |
| Cosine distance | `<=>` | Difference in direction | More similar |
| Negative inner product | `<#>` | Opposite of the dot product | More similar |
L2 distance
L2 distance measures the straight-line distance between two points. It considers both direction and magnitude. Use it when the embedding model was trained for Euclidean distance or when vector length carries useful meaning.
SELECT id, content, embedding <-> '[3,1,2]' AS l2_distance
FROM items
ORDER BY embedding <-> '[3,1,2]'
LIMIT 5;The query vector must have the same number of dimensions as the embedding column.
Cosine distance
Cosine distance compares direction rather than length. It is useful for many text embeddings, where the angle between vectors represents semantic similarity and vector magnitude is less important.
Cosine similarity ranges from -1 to 1. pgvector stores the inverse relationship as cosine distance:
cosine distance = 1 - cosine similarity
Therefore, a smaller <=> result indicates a more similar vector. To display similarity, subtract the distance from 1, but keep the distance expression in ORDER BY:
SELECT id, content,
1 - (embedding <=> '[3,1,2]') AS cosine_similarity
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;Keeping the operator directly in ORDER BY also makes the query compatible with the matching vector index.
Inner product
The inner product, also called the dot product, multiplies corresponding values and adds the products. Unlike cosine distance, it is affected by vector magnitude. A larger magnitude can produce a larger score even when the direction is less aligned.
pgvector's <#> operator returns the negative inner product. PostgreSQL normally sorts in ascending order, so negating the score allows the most similar vectors to appear first:
SELECT id, content, embedding <#> '[3,1,2]' AS negative_inner_product
FROM items
ORDER BY embedding <#> '[3,1,2]'
LIMIT 5;Choosing the metric
Follow the metric used by the embedding model's documentation. If the model normalizes every vector to length 1, cosine similarity and inner product produce the same ranking. Normalized vectors also make L2 and cosine rankings equivalent, although their numeric scores differ.
Exercise
Which operator should you use to rank vectors by cosine similarity, and why does the query sort by that operator instead of by 1 minus the operator?
Show answer
Use <=>, because it returns cosine distance and smaller values are better. Sorting directly by <=> preserves the expression expected by a matching pgvector index. The displayed similarity can be calculated as 1 - (embedding <=> query).