Sub topic 6
The distance operators: <->, <=>, <#>, and <+>
This chapter assumes you can write basic SQL, have a table with a vector column (such as the items table from earlier chapters), and know that a distance metric turns two vectors into one number. Here you will learn what each pgvector operator computes, how to read its output, and where each one fails.
Operators return distances, not similarities
pgvector adds infix operators that take two vectors of the same dimension and return a double precision value. Every operator returns a distance: smaller means closer. Sort ascending, which is the default, to get nearest neighbors first.
| Operator | Computes | Range of result | Index operator class |
|---|---|---|---|
| `<->` | Euclidean (L2) distance | 0 and up | `vector_l2_ops` |
| `<=>` | Cosine distance, 1 minus cosine similarity | 0 to 2 | `vector_cosine_ops` |
| `<#>` | Negative inner product | any real number | `vector_ip_ops` |
| `<+>` | Taxicab (L1) distance | 0 and up | `vector_l1_ops` (HNSW only) |
The <+> operator was added in pgvector 0.7.0. Check your version with SELECT extversion FROM pg_extension WHERE extname = 'vector'; if it is missing. The bit and sparse types add <~> (Hamming) and <%> (Jaccard), which a later chapter covers.
Comparing all four on the same data
Use the items table from earlier: a = [1,2,3], b = [4,5,6], c = [1,1,1]. The query vector is [3,1,2].
SELECT content,
embedding <-> '[3,1,2]' AS l2,
embedding <=> '[3,1,2]' AS cosine_distance,
(embedding <#> '[3,1,2]') * -1 AS inner_product,
embedding <+> '[3,1,2]' AS l1
FROM items
ORDER BY content;The values, rounded:
| content | l2 | cosine_distance | inner_product | l1 |
|---|---|---|---|---|
| a | 2.449 | 0.214 | 11 | 4 |
| b | 5.745 | 0.117 | 29 | 9 |
| c | 2.236 | 0.074 | 6 | 3 |
The same three rows rank differently depending on the operator.
- L2 and L1 rank
c,a,b. Rowbis far from the query in absolute terms. - Cosine ranks
c,b,a. Rowbpoints in a direction close to the query even though it is far away, because cosine ignores length. - Inner product ranks
bfirst. It rewards large magnitudes, so the long vectorbwins even thoughcis closer in direction and in position.
Pick the operator that matches how your embedding model was trained. Most text embedding models document cosine similarity or inner product on normalized vectors. Use whichever the model's documentation names.
Reading each operator's output
`<->` Euclidean distance
<-> is the straight-line distance: the square root of the sum of squared differences. It is sensitive to magnitude, so two vectors with the same direction but different lengths are not close. It works well when the model was trained with an L2 objective, or when vector length carries meaning.
`<=>` cosine distance
<=> returns 1 - cosine similarity. Identical directions give 0, orthogonal vectors give 1, and opposite directions give 2. To display similarity, subtract the distance from 1, but keep the raw operator in ORDER BY:
SELECT id, content, 1 - (embedding <=> '[3,1,2]') AS cosine_similarity
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;An index can serve the ORDER BY only when it sees the bare operator expression. Ordering by 1 - (embedding <=> ...) defeats the index.
`<#>` negative inner product
The inner product (dot product) is larger for more similar vectors. PostgreSQL index scans only return rows in ascending order, so pgvector returns the negative inner product. Sorting it ascending puts the largest inner product first. To report the real inner product, multiply by -1:
SELECT id, content, (embedding <#> '[3,1,2]') * -1 AS inner_product
FROM items
ORDER BY embedding <#> '[3,1,2]'
LIMIT 5;A common mistake is writing ORDER BY embedding <#> ... DESC. That returns the least similar rows and cannot use an index.
For vectors normalized to length 1, the inner product equals cosine similarity, and ranking by <#> gives the same order as ranking by <=>. Inner product skips the length computation, so it is slightly cheaper. Some embedding providers return normalized vectors, which makes <#> a safe choice.
`<+>` taxicab distance
<+> is the L1 distance: the sum of absolute differences per dimension. It grows more slowly than L2 when one coordinate differs a lot, so it is less dominated by a single outlier dimension. Use it only when you have a reason, such as a model or feature set built for L1.
Choosing an operator
Errors and edge cases
Dimension mismatch
Both operands must have the same number of dimensions. Otherwise PostgreSQL raises an error instead of padding:
SELECT embedding <-> '[1,2]' FROM items;
-- ERROR: different vector dimensions 3 and 2This usually means the application switched embedding models, or truncated a vector, without changing the column.
Zero vectors and cosine distance
Cosine similarity divides by the vector lengths, and a zero vector has length 0. The result is NaN (not a number):
SELECT '[0,0,0]'::vector <=> '[1,2,3]'::vector AS d;
-- d = NaNIn PostgreSQL, NaN sorts after every ordinary number, so a zero vector lands at the end of an ascending ordering. pgvector also refuses to put such rows in a cosine index. Skip them at insert time, or filter them with WHERE embedding <> '[0,0,0]' if they can occur in your data.
NULL vectors
If either operand is NULL, the operator returns NULL. In an ascending sort, NULL sorts last, so rows without embeddings drop to the end of the results rather than disappearing. Add WHERE embedding IS NOT NULL when you want them gone. An index never contains them.
Distance thresholds
You can filter by distance, for example to return only close matches:
SELECT id, content
FROM items
WHERE embedding <=> '[3,1,2]' < 0.1
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;This is correct, but an approximate index scans in a fixed order and returns its top candidates first. The WHERE clause is then applied to those candidates. The query can return fewer than five rows even when more rows qualify, so it is no substitute for a true range search. A later chapter on filtered search covers this.
Operators and indexes
An index serves a query only if its operator class matches the operator in ORDER BY. A vector_cosine_ops index helps <=> queries and does nothing for <-> queries. Without a matching index, PostgreSQL falls back to an exact scan of the table. The results stay correct, but the query is slower. The operator class chapter covers this in detail.
Exercise
A table has a normalized embedding column, and you want the five most similar rows by inner product, with the real inner product shown in the output. Write the query for query vector '[0.6,0.8]'. Why is the result column multiplied by -1, and why must the ORDER BY not use DESC?
Show answer
SELECT id, (embedding <#> '[0.6,0.8]') * -1 AS inner_product FROM items ORDER BY embedding <#> '[0.6,0.8]' LIMIT 5; The <#> operator returns the negative inner product so that an ascending sort puts the most similar rows first and an index scan can serve it. Multiplying by -1 restores the real inner product for display. Adding DESC would return the least similar rows first and could not use the index.
Exercise
Row x is [10,10,10] and row y is [1,2,3]. The query is [1,1,1]. Which row does <=> rank closer, and which does <-> rank closer? Explain the difference.
Show answer
Cosine (<=>) ranks x closer, because [10,10,10] points in exactly the same direction as [1,1,1], giving distance 0, while y has a larger angle. L2 (<->) ranks y closer, because y is about 1.41 away from the query while x is about 15.6 away. Cosine ignores length and L2 does not.