AI Development

Vector Search Without a Vector Database: When Postgres Is Enough

When pgvector or even a plain in-memory scan is enough for semantic search, with a runnable demo of the filtered-search trap that returns too few rows and the pgvector 0.8 fix.

By Rajib Roy Published 5 min read

When a team decides to add semantic search or retrieval to an app, the first reflex is often to reach for a dedicated vector database. Sometimes that is the right call. But many projects store a few thousand to a few hundred thousand embeddings, already run Postgres, and would be better off without another service to deploy, secure, back up and pay for.

In this post I'll lay out a simple way to decide, show what "no vector database at all" looks like in plain JavaScript, and walk through a pgvector demo I ran, including a trap that catches almost everyone who combines a filter with an approximate index: the query that quietly returns fewer rows than you asked for.

Start with how big your data really is

Embeddings are arrays of numbers, usually a few hundred to a few thousand per item. In pgvector, each vector takes 4 * dimensions + 8 bytes. For a 384-dimension model that is 1,544 bytes per row, so 20,000 documents take about 31 MB of vector data. That fits in memory on any server you would plausibly use. Do the same arithmetic for your own case before you decide anything. If the answer is "a few hundred megabytes", you are not in the territory that justifies a specialist system.

Option 1: no database at all

Exact search means comparing the query against every vector and sorting. It sounds naive, but it is perfectly correct and surprisingly fast at small scale. I timed it in plain JavaScript on 20,000 normalized vectors of 384 dimensions, using a flat typed array and a dot product (equal to cosine similarity once vectors are normalized):

brute.mjs

// How slow is "no vector database at all"? Exact cosine search over 20,000 x 384-dim vectors, in plain JS.
const N = 20000, D = 384;
let seed = 7;
const rand = () => ((seed = (seed * 1664525 + 1013904223) % 2 ** 32) / 2 ** 32) - 0.5;
const data = new Float32Array(N * D);
for (let i = 0; i < data.length; i++) data[i] = rand();
// normalise so cosine similarity == dot product
for (let i = 0; i < N; i++) {
  let n = 0; for (let j = 0; j < D; j++) n += data[i * D + j] ** 2;
  n = Math.sqrt(n); for (let j = 0; j < D; j++) data[i * D + j] /= n;
}
const q = Float32Array.from({ length: D }, rand);
let qn = Math.sqrt(q.reduce((a, x) => a + x * x, 0)); for (let j = 0; j < D; j++) q[j] /= qn;

function top10() {
  const scores = new Float32Array(N);
  for (let i = 0; i < N; i++) { let s = 0; const o = i * D; for (let j = 0; j < D; j++) s += data[o + j] * q[j]; scores[i] = s; }
  return [...scores.keys()].sort((a, b) => scores[b] - scores[a]).slice(0, 10);
}
top10(); // warm up
const runs = 20, t0 = performance.now();
for (let r = 0; r < runs; r++) top10();
console.log(`avg ${((performance.now() - t0) / runs).toFixed(1)} ms per query over ${N} vectors x ${D} dims (single thread, this machine)`);

On my machine this took roughly 14 to 18 milliseconds per query on a single thread across a few runs. That is one data point from one laptop-class setup, not a benchmark, but it makes the point: at this size, the "hard" part of vector search is not the search. For a prototype, an internal tool or a documentation search with a few thousand chunks, a loaded array and a sort is enough, and nothing can drift out of sync because there is no index to rebuild.

Where this stops working: when the dataset reaches millions of vectors, when you need to filter by metadata at the same time, when more than one process needs the data, or when data changes constantly and you need persistence and transactions. That is exactly where Postgres comes in.

Option 2: pgvector in the Postgres you already run

pgvector adds a vector column type, distance operators (<-> for Euclidean, <=> for cosine) and two index types. Your embeddings live next to the rows they describe, in the same transaction, backed up by the same process. A query can join the vector search with your normal tables. For a lot of products this is the entire appeal: no second system whose data can disagree with your first.

The index choices are worth knowing in one sentence each. No index means exact search by scanning, always correct. HNSW builds a graph for fast approximate search and generally gives better speed and recall than IVFFlat, at the price of slower builds and more memory. IVFFlat clusters vectors into lists, builds faster and uses less memory, but needs data present when you create it. Approximate means exactly that: the index may miss a true neighbor in exchange for speed.

The trap: filters plus approximate indexes

Real queries rarely ask for "the ten nearest vectors in the whole table". They ask for "the ten nearest in this category" or "for this customer". With an approximate index, pgvector applies that filter after the index scan. The README explains the consequence: if a condition matches 10% of rows and the default hnsw.ef_search of 40 candidates is used, only about 4 rows will match on average. You ask for 10 and get fewer.

I reproduced this. The demo builds 5,000 random vectors in three categories, runs an exact filtered query, adds an HNSW index and runs the same query again:

demo.mjs

import { PGlite } from "@electric-sql/pglite";
import { vector } from "@electric-sql/pglite/vector";

// Deterministic pseudo-random numbers so the run is repeatable.
let seed = 42;
const rand = () => ((seed = (seed * 1664525 + 1013904223) % 2 ** 32) / 2 ** 32) - 0.5;
const makeVec = (d) => Array.from({ length: d }, rand);
const lit = (v) => `[${v.map((x) => x.toFixed(5)).join(",")}]`;

const DIM = 32, N = 5000;
const cats = ["history", "science", "nature"];
const db = await PGlite.create({ extensions: { vector } });
await db.exec(`
  CREATE EXTENSION vector;
  CREATE TABLE docs (
    id bigserial PRIMARY KEY,
    category text NOT NULL,
    title text NOT NULL,
    embedding vector(${DIM}) NOT NULL
  );
`);
for (let i = 0; i < N; i++) {
  await db.query("INSERT INTO docs (category, title, embedding) VALUES ($1, $2, $3)", [cats[i % 3], `doc ${i}`, lit(makeVec(DIM))]);
}

const q = lit(makeVec(DIM));
const exactSql = `SELECT id FROM docs WHERE category = 'history' ORDER BY embedding <=> '${q}' LIMIT 10`;

// 1) Exact search: no index, the planner scans the table. Correct by construction.
const exact = (await db.query(exactSql)).rows.map((r) => r.id);
const short = (s) => s.replace(/'\[[^\]]*\]'/g, "'[...]'").replace(/ +\(cost=[^)]*\)/g, ""); // keep the printed plan readable
const plan1 = short((await db.query("EXPLAIN " + exactSql)).rows.map((r) => r["QUERY PLAN"]).join("\n"));
console.log("-- plan without an index --\n" + plan1);

// 2) Approximate search: an HNSW index trades a little recall for speed at large scale.
await db.exec("CREATE INDEX docs_hnsw ON docs USING hnsw (embedding vector_cosine_ops);");
await db.exec("SET enable_seqscan = off;"); // force the planner to show us the index path
const approx = (await db.query(exactSql)).rows.map((r) => r.id);
const plan2 = short((await db.query("EXPLAIN " + exactSql)).rows.map((r) => r["QUERY PLAN"]).join("\n"));
console.log("\n-- plan with HNSW (seqscan disabled for the demo) --\n" + plan2);

const overlap = approx.filter((id) => exact.includes(id)).length;
console.log(`\nexact top-10 vs index top-10 overlap: ${overlap}/10 (rows returned by index: ${approx.length})`);

// 3) pgvector 0.8+ can keep scanning the index until the filter has produced enough rows.
await db.exec("SET hnsw.iterative_scan = strict_order;");
const iter = (await db.query(exactSql)).rows.map((r) => r.id);
const overlap2 = iter.filter((id) => exact.includes(id)).length;
console.log(`with iterative scan: ${iter.length} rows, overlap with exact ${overlap2}/10`);

The output of one run:

-- plan without an index --
Limit
  ->  Sort
        Sort Key: ((embedding <=> '[...]'::vector))
        ->  Seq Scan on docs
              Filter: (category = 'history'::text)

-- plan with HNSW (seqscan disabled for the demo) --
Limit
  ->  Index Scan using docs_hnsw on docs
        Order By: (embedding <=> '[...]'::vector)
        Filter: (category = 'history'::text)

exact top-10 vs index top-10 overlap: 9/10 (rows returned by index: 9)
with iterative scan: 10 rows, overlap with exact 10/10

Read it from the top. Without an index, the planner scans the table, filters, sorts and returns the correct ten. With HNSW, the filter sits on top of the index scan, and the query returned 9 rows, not 10. No error, no warning. Then I enabled hnsw.iterative_scan = strict_order, a feature added in pgvector 0.8.0, which keeps scanning the index until enough rows pass the filter. The query returned 10 rows and all 10 matched the exact answer.

A caveat on honesty: the vectors are random, the data is small, and I forced the planner onto the index with enable_seqscan = off. On a table of 5,000 rows, Postgres would normally just scan it. The demo exists to show the behavior, not to suggest that you need an index at that size.

How I would decide

  1. Count your vectors and dimensions. Under a few tens of thousands: try exact search first, in memory or in Postgres without an index.
  2. Do you already run Postgres? If yes, and you need filters, joins or transactions, use pgvector. It removes a whole class of sync problems.
  3. Need filtered approximate search? Use pgvector 0.8 or later with iterative scans, or partition by the filter column. Then test the number of rows returned, not just speed.
  4. Measure recall on your own data. Compare the index's results against exact search for a sample of queries. If overlap is low, raise hnsw.ef_search or rethink the index.
  5. Consider a dedicated system only when you can name the limit. Hundreds of millions of vectors, extreme query rates, or features your setup lacks are real reasons. "It's what AI apps use" is not.

Practical pitfalls

  • Dimension limits. The README says a vector column can be indexed up to 2,000 dimensions, with half-precision vectors reaching 4,000. Check your embedding model's dimensions against that before you commit.
  • Changing the model means re-embedding everything. Vectors from different models aren't comparable. Store the model name next to each vector so you can migrate in batches.
  • Test with realistic filters. A demo without a WHERE clause hides the filtered-search problem completely.
  • Chunking matters more than the database. Poor chunks give poor answers in any system. Measure retrieval quality with a small evaluation set before tuning infrastructure.

The short version

For small and medium projects, start simple: exact search, then pgvector, and move to something bigger only when your own numbers say so. Whatever you pick, check that filtered queries return as many rows as you asked for. That single test finds a bug that benchmarks of speed never will.

Build with me

Adding search or retrieval to your product?

I build AI features on boring, reliable infrastructure, with measured retrieval quality. If you want a second opinion on whether you need a vector database, get in touch.

See services Get in touch

What I ran this against

  • The pgvector demo ran in PGlite 0.3.x (Postgres compiled to WebAssembly, bundling pgvector 0.8.0) under Node.js 24.22.1 on 17 November 2026. It uses 5,000 random 32-dimension vectors in three categories, not real embeddings, so it shows behavior of the index and the planner, not search quality.
  • The brute-force timing is one measurement on my machine (a single thread, 20,000 vectors of 384 dimensions): between about 14 and 18 milliseconds per query across runs. Your hardware and your data will differ. I did not benchmark any hosted vector database, so I make no comparison claims about speed or price.
  • PGlite's planner output was used for the plans shown; I disabled sequential scans in the demo to force it to show the index path. On a real server with real data, the planner decides for itself.

Sources checked

  • pgvector README: dimension limits (2,000 for indexing vector), storage of 4 * dimensions + 8 bytes per vector, HNSW and IVFFlat, filtering applied after the index scan, and iterative index scans added in 0.8.0: github.com/pgvector/pgvector, read 17 November 2026.