Why this matters
The vector store holds your users' knowledge. Picking one is an architecture decision with operational, cost and security consequences, and multi-tenant leakage through retrieval is one of the most serious AI bugs.
Q4. pgvector vs Pinecone vs Weaviate vs Qdrant?
| Store | Pick when | Watch for |
|---|---|---|
| pgvector | Postgres is the system of record; joins, transactions, RLS for tenants | Index build and memory at very large scale; filtered HNSW (Q8) |
| Pinecone | Zero-ops managed scale, serverless | Cost model, lock-in, residency |
| Weaviate | Built-in hybrid and modules | One more system to run |
| Qdrant | Heavy payload filtering, quantization, self-host | One more system to run |
Say: "I default to pgvector for operational simplicity and Postgres-side security, and I've shipped on Pinecone and Qdrant when the workload called for it." (Casebook: P4, P3.)
The pgvector schema
The full schema and queries are in the code kit, C4. Its key decisions:
tenant_idanddocument_idon every chunk;embed_modelrecorded per row.- An HNSW index for cosine distance, a GIN index on a generated
tsvectorfor lexical search, and a(tenant_id, document_id)B-tree. - Row-level security as a second wall: the policy compares
tenant_idwith a per-transaction setting. - Operators:
<=>cosine distance (similarity = 1 − distance),<->L2,<#>negative inner product,<+>L1.
Q8. Multi-tenancy?
Authorization before retrieval, in server code; RLS as a second wall; cache keys scoped by authorization context; never let the client supply the filter. With pgvector, filtered HNSW queries can return too few rows; iterative index scans (0.8+) address it. Code: C4.
Why filtered ANN is tricky
An HNSW search finds the nearest N vectors, then the WHERE tenant_id = … filter removes other tenants' rows, possibly leaving too few results. Options: iterative scans (keep scanning until enough rows pass), partial indexes per large tenant, partitioning by tenant, or a store with native filtered search.
Common mistakes
- Filtering by tenant only in the application, with no database-level backstop.
- The application role owning the table, which bypasses RLS unless
FORCE ROW LEVEL SECURITYis set. - Forgetting the lexical index, then blaming embeddings for missed codes.
Interview follow-ups
- "When would you move off pgvector?" When vector volume or QPS outgrows what your Postgres can serve with acceptable recall and latency, or you need features like heavy filtered ANN or quantization at scale, proven by benchmarks on your data.
- "How do you re-embed 50M vectors without downtime?" Build a new index or column in the background, dual-write new documents, evaluate both, switch reads atomically, then drop the old one.
- "HNSW parameters?"
m(graph connectivity) andef_constructiontrade build time and memory for recall;ef_searchtrades query latency for recall at query time.
Check yourself
Why might a filtered HNSW query return only 3 rows when you asked for 50?
The index returns the nearest candidates first and the filter removes most of them (other tenants). Use iterative index scans, raise ef_search, partition or index per tenant, or choose a store with filter-aware ANN.
Name three layers that prevent cross-tenant leakage in RAG.
Server-side tenant/permission filters in every retrieval query; Postgres RLS as a second wall; cache keys scoped by tenant/user; plus tests that try to retrieve another tenant's document.
Build it
Create the C4 schema, load two tenants' documents, enable RLS, and write a test that tenant A can never retrieve tenant B's chunks, even with a query that matches B's text exactly.