## Summary

When vector search in PostgreSQL (`pgvector`) takes seconds instead of milliseconds, the culprit is almost always a missing `HNSW` or `IVFFlat` index, an operator-class mismatch (`vector_cosine_ops` vs. `<=>`), or `work_mem` spilling sorts to disk.

If you want this tuned cleanly, SazM fixes slow `pgvector` and RAG database queries for a **fixed $99, delivered within 48 hours**, 100% in writing with zero calls. See our [AI integration services](/ai-integration-services) or [request the $99 fix](/contact?offer=emergency).

## 1. Add an `HNSW` Index Matching Your Distance Operator

`pgvector` only uses an index when the index operator class matches the query operator:

- Cosine distance (`<=>`): `CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);`
- L2 distance (`<->`): `CREATE INDEX ON items USING hnsw (embedding vector_l2_ops);`
- Check `EXPLAIN ANALYZE` to confirm an `Index Scan` instead of a `Seq Scan`.

## 2. Avoid Post-Filter Starvation

Filtering by `WHERE tenant_id = $1` after `ORDER BY embedding <=> $2 LIMIT 10` can return fewer than 10 rows or force sequential scans. Use partial indexes or `pgvector` 0.8+ iterative index scans (`SET hnsw.iterative_scan = strict_order;`).

## Need It Fixed?

We tune production PostgreSQL/`pgvector`, RAG pipelines, and Cloudflare Workers AI backends for a **fixed $99 within 48 hours**, verified before you pay and 100% async. [Start your $99 fix](/contact?offer=emergency) or explore our [AI integration services](/ai-integration-services).
