Database / pgvector basics Interview Questions
What are best practices for a production pgvector deployment?
A checklist of best practices covers schema design, indexing, performance, operations, and application integration for reliable, performant pgvector deployments.
| Area | Best practice |
|---|---|
| Schema | Store embeddings in the same table as the content for easy JOINs; use ON DELETE CASCADE for chunk tables |
| Data types | Use halfvec instead of vector when storage matters; use sparsevec for sparse embeddings |
| Indexing | Create HNSW index after bulk loading data; use correct operator class matching your distance metric |
| Build tuning | Set maintenance_work_mem high (2-8GB) and max_parallel_maintenance_workers before building indexes |
| Query recall | Tune hnsw.ef_search upward if recall is insufficient with metadata filters |
| Filtering | Use partial indexes for frequently-filtered column values |
| Bulk load | Load data first, then build indexes; use COPY for large imports |
| Monitoring | Monitor index build with pg_stat_progress_create_index; check pg_indexes for existence |
| Statistics | Run ANALYZE after large data loads to keep query planner statistics fresh |
| Application | Use the pgvector package's register_vector() for proper Python type conversion |
-- Production-ready setup summary: -- 1. Enable extension CREATE EXTENSION IF NOT EXISTS vector; -- 2. Create schema with appropriate type CREATE TABLE documents ( id BIGSERIAL PRIMARY KEY, content TEXT NOT NULL, category TEXT, created_at TIMESTAMPTZ DEFAULT NOW(), embedding HALFVEC(1536) -- halfvec for 2x storage savings ); -- 3. Add metadata index for filtering CREATE INDEX ON documents (category); -- 4. Bulk load data (skip vector index during this phase) -- COPY documents FROM '/data/docs.csv'; -- 5. Update statistics after load ANALYZE documents; -- 6. Build vector index with tuned memory/parallelism SET maintenance_work_mem = '4GB'; SET max_parallel_maintenance_workers = 7; CREATE INDEX ON documents USING hnsw (embedding halfvec_cosine_ops) WITH (m = 16, ef_construction = 64); RESET maintenance_work_mem; RESET max_parallel_maintenance_workers; -- 7. Verify index is used: EXPLAIN SELECT id FROM documents ORDER BY embedding <=> '[...]'::halfvec LIMIT 5;
More Related questions...