# Notebook 8: pgvector for agent memory # run at: 2026-11-19 02:14 UTC # runner: Linux-7.0.0-1019-azure-x86_64-with-glibc2.43 | 4 vCPU # PostgreSQL: 18.7 (Debian 18.7-1.pgdg12+2) # pgvector: 0.8.7 Data: 100,000 memories of 384 dimensions, generated around 500 topics in a 64-dimension space and projected up, because real embeddings cluster by subject and use far fewer directions than they have. 100 tenants (agents or customers), 1,000 memories each. Generated vectors are not real embeddings, so treat recall as the shape of the curve, not a promise. Timings are from a client on the same machine and include the round trip. ## 1. What it costs to store loaded 100,000 rows in 11.0 s table: 157 MB, 1641 bytes per memory (a 384-dimension vector is 1536 bytes of floats) HNSW index (m = 16, ef_construction = 64): built in 10.5 s, 195 MB, 2048 bytes per memory table plus HNSW index: 3689 bytes per memory, 2.4 times the raw floats ## 2. Recall against speed: top 10 nearest memories, 200 searches each exact search (no index, reads every row): 21.7 ms per search plan with the index: Index Scan using nb_memory_hnsw hnsw.ef_search = 10 recall@10 0.797 median 0.60 ms p95 1.10 ms hnsw.ef_search = 20 recall@10 0.863 median 0.59 ms p95 0.85 ms hnsw.ef_search = 40 recall@10 0.950 median 0.65 ms p95 1.08 ms hnsw.ef_search = 80 recall@10 0.985 median 0.71 ms p95 2.17 ms hnsw.ef_search = 160 recall@10 1.000 median 0.81 ms p95 1.40 ms hnsw.ef_search = 400 recall@10 1.000 median 2.64 ms p95 3.62 ms ## 3. Filtering by tenant: one tenant's memories are 1% of the table planner's choice for the filtered query: HNSW index, then filter HNSW, ef_search 40, then filter: rows returned 1.3 of 10 recall@10 0.130 median 0.76 ms HNSW, ef_search 400, then filter: rows returned 4.8 of 10 recall@10 0.477 median 3.89 ms HNSW, ef_search 40, iterative_scan = relaxed_order: rows returned 10.0 of 10 recall@10 0.906 median 8.96 ms tenant index, then exact distance on 1,000 rows: rows returned 10.0 of 10 recall@10 1.000 median 2.66 ms Read it this way: with the filter applied after the index, the index hands back about ef_search candidates and 99% of them belong to other tenants. Raising ef_search or scanning iteratively fixes it at a cost; for small tenants an exact search inside the tenant is both exact and fast.