QA
Beginner LevelPostgreSQL

How to use pgvector in PostgreSQL for vector similarity search with HNSW indexes?

❓ Interview Prompt: Explain the vector data type, HNSW index tuning, and distance operators (<->, <=>, <#>) in Postgres 16+.

pgvector is an open-source extension for PostgreSQL that allows storing and indexing high-dimensional vector embeddings directly inside relational tables alongside relational data.

Distance Operators:

•
'<=>': Cosine distance
•
'<->': Euclidean distance (L2)
•
'<#>': Inner product (Dot product)

HNSW Indexing:

Provides fast approximate nearest neighbor (ANN) search without building IVF clusters.

Code Implementation Example

sql
-- Enable vector extension
CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE document_chunks (
  id BIGSERIAL PRIMARY KEY,
  content TEXT NOT NULL,
  embedding vector(1536) NOT NULL
);

-- Create HNSW index for fast Cosine Distance lookup
CREATE INDEX idx_chunks_embedding_hnsw 
ON document_chunks 
USING hnsw (embedding vector_cosine_ops);
💡PostgreSQL pgvector extension setup and HNSW index query.
🎯 Interview Key Takeaway

pgvector eliminates the need for separate standalone vector databases by keeping embeddings transactional within PostgreSQL.

#PostgreSQL#pgvector#HNSW#AI Vector Search#Beginner
Back to all PostgreSQL Questions→

More PostgreSQL Interview Questions

Beginner LevelPostgreSQL

What is PostgreSQL architecture and how do client backends connect via process fork model?

Beginner LevelPostgreSQL

What special built-in data types exist in PostgreSQL (JSONB, UUID, ARRAY, INET, TEXT)?

Beginner LevelPostgreSQL

How do JSONB query operators (@>, ->, ->>, ?|) extract and filter document fields?