0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · Lesson

Choosing Distance Metrics in pgvector

Understand cosine, L2, and inner product distance operators in pgvector and how to pick the right one for your embeddings.

Choosing Distance Metrics in pgvector is a free Vector Databases: Pinecone, Weaviate & pgvector lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vector Databases: Pinecone, Weaviate & pgvector learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Distance Matters

Similarity search ranks vectors by distance. The metric you choose must match how your embedding model was trained, or results will be subtly wrong.

The Three Operators

pgvector provides three distance operators:

  • <-> Euclidean (L2) distance
  • <=> Cosine distance
  • <#> Negative inner product
SELECT '[1,2,3]'::vector <=> '[1,2,4]'::vector AS cosine_distance;

Cosine Distance

Cosine measures angle, ignoring magnitude. It is the default for most text embedding models like OpenAI and Sentence Transformers.

SELECT id, embedding <=> '[0.1,0.2,0.3]'::vector AS dist
FROM items
ORDER BY dist
LIMIT 5;

Euclidean (L2) Distance

L2 measures straight-line distance and is sensitive to magnitude. Use it when vector length carries meaning, such as raw feature vectors.

SELECT id, embedding <-> '[0.1,0.2,0.3]'::vector AS dist
FROM items
ORDER BY dist
LIMIT 5;

Inner Product

The inner product operator <#> returns the negative dot product (so smaller is more similar). It is fastest and ideal for normalized vectors.

SELECT id, (embedding <#> '[0.1,0.2,0.3]'::vector) * -1 AS similarity
FROM items
ORDER BY embedding <#> '[0.1,0.2,0.3]'::vector
LIMIT 5;

Match Metric to Model

Always check your embedding model's documentation:

  • OpenAI text-embedding-3: cosine
  • Normalized vectors: inner product (equivalent to cosine, faster)
  • Geometric/spatial features: L2

Normalization Trick

If your vectors are unit-normalized, cosine and inner product give the same ranking. Inner product is cheaper, so normalize once at insert time.

-- Normalize before insert in app code, then use <#>
UPDATE items SET embedding = l2_normalize(embedding);

Metric in Indexes

Indexes are metric-specific. You must declare the operator class so the index matches your query operator.

CREATE INDEX ON items
USING hnsw (embedding vector_cosine_ops);

Mismatched Operators

If your index uses vector_l2_ops but you query with <=>, Postgres ignores the index and does a slow sequential scan. Keep them aligned.

Verifying Index Usage

Use EXPLAIN to confirm the planner uses your vector index for the chosen metric.

EXPLAIN ANALYZE
SELECT id FROM items
ORDER BY embedding <=> '[0.1,0.2,0.3]'::vector
LIMIT 5;

Summary of Choices

Rule of thumb:

  • Text/semantic search: cosine
  • Pre-normalized vectors: inner product
  • Raw numeric features: L2

Quick Check

Pick the right metric.

Recap

You learned pgvector's three distance operators, when to use cosine, L2, and inner product, and why your index operator class must match your query operator for fast searches.

Frequently asked questions

Is the “Choosing Distance Metrics in pgvector” lesson free?

Yes — the full text of “Choosing Distance Metrics in pgvector” is free to read here on the web, and the Vector Databases: Pinecone, Weaviate & pgvector course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vector Databases: Pinecone, Weaviate & pgvector course, upgrade to CoddyKit PRO.

What will I learn in “Choosing Distance Metrics in pgvector”?

Understand cosine, L2, and inner product distance operators in pgvector and how to pick the right one for your embeddings. You practise Vector Databases: Pinecone, Weaviate & pgvector with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vector Databases: Pinecone, Weaviate & pgvector?

No prior experience is required. Vector Databases: Pinecone, Weaviate & pgvector on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Choosing Distance Metrics in pgvector” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vector Databases: Pinecone, Weaviate & pgvector lesson?

Yes. Every Vector Databases: Pinecone, Weaviate & pgvector lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Setting Up pgvector Extension
  2. Storing Vectors in PostgreSQL
  3. Performing Similarity Queries
  4. Choosing Distance Metrics in pgvector
← Back to Vector Databases: Pinecone, Weaviate & pgvector