Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

You must add semantic search over 20 million product descriptions. Compare using the pgvector extension in your existing PostgreSQL with a dedicated vector database.

pgvector stores embeddings in a vector column and supports HNSW and IVFFlat indexes with cosine, L2 and inner product distance, which is plenty for 20 million products. Its big advantage is one database: transactions with the product data, filters and joins in SQL, and existing backups and access control. Its costs are index build time and memory, tuning recall against speed, extra load on the primary, and recall loss with selective filters, so we test with real filters; a dedicated vector database wins mainly at much larger scale or very strict latency targets, at the price of syncing data and running another system.

Why this question is asked

Semantic search, finding products by meaning rather than exact words, has become a standard feature, and the first architecture decision is where the vectors live. Vendors of dedicated vector databases and fans of 'just use Postgres' both make strong claims. Interviewers ask this scenario to see whether we can compare them with real numbers and test what matters. In this answer, we will recap embeddings and nearest neighbour search, see what pgvector offers, size 20 million vectors, look at its costs and the filtering problem, and decide when a dedicated store is worth it.

1A Quick Recap: Embeddings and Nearest Neighbours

An embedding model turns a product description into a vector, a list of numbers such as 768 or 1,536 floats, so that descriptions with similar meaning end up close together. Semantic search embeds the user's query the same way and returns the products whose vectors are nearest to it.

Finding the exact nearest vectors among 20 million means computing 20 million distances per query, far too slow. So we use approximate nearest neighbour (ANN) indexes, which return almost all of the true nearest neighbours very quickly. Recall measures how close they come: a recall of 0.95 at 10 means that on average 9.5 of the true top 10 are returned.

2What pgvector Offers

pgvector adds a vector column type and three main distance operators: <=> for cosine distance, <-> for Euclidean (L2) distance and <#> for negative inner product. It offers two ANN index types. HNSW builds a layered graph of neighbours; it gives the best speed and recall trade-off but is slower to build and uses more memory. IVFFlat clusters vectors into lists and searches only the nearest few lists; it builds faster and is smaller, but recall depends heavily on choosing lists and probes well, and it should be built after the data is loaded.

SQL
CREATE EXTENSION vector;
ALTER TABLE products ADD COLUMN embedding vector(768);
CREATE INDEX products_embedding_hnsw ON products
USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 64);
SET hnsw.ef_search = 100; -- higher: better recall, slower queries (default 40)
SELECT id, name, embedding <=> $1 AS distance
FROM products
ORDER BY embedding <=> $1
LIMIT 10; -- 10 nearest products by cosine distance

Notice that the query is ordinary SQL. The ORDER BY on the distance operator with a LIMIT is what lets the planner use the HNSW index.

3The Big Advantage: One Database

With pgvector, the embedding lives in the same row as the product. When a description changes, we update the text and the embedding in one transaction, so search never returns a product whose vector is stale or that has been deleted. Filters and joins are plain SQL: in stock, in this category, price under 50, visible in this country. Backups, point-in-time recovery, access control, monitoring and on-call procedures already exist.

With a dedicated vector database, every one of those becomes work. We must sync products into it, through CDC or dual writes, handle deletes and failures, keep filters' metadata in both places, and run, secure and back up another system.

pgvector or a dedicated vector database
Concernpgvector in PostgreSQLDedicated vector database
Consistency with product datasame transactionsync pipeline, eventual
Filters and joinsfull SQLmetadata filters only, duplicated
Operationsexisting backups and accessa new system to run
Scalecomfortable at tens of millionsbuilt for billions, sharded
Latency at high QPSgood; competes with OLTP loadtuned for it, isolated
Filtered search recallneeds testing and tuningoften built-in filtering strategies

4The Costs: Memory, Build Time and Load

Let us size it. 20 million vectors of 768 float32 values take 20,000,000 x 768 x 4 bytes, about 61 GB, before the index. An HNSW index with m = 16 adds the graph and its own copy of the vectors, so expect something of the same order again, and for fast queries the index should fit in memory. Building it can take hours and benefits from a large maintenance_work_mem and parallel workers. Smaller types help: pgvector's halfvec stores 16-bit floats and halves the size, with little recall loss for many models (check your pgvector version).

Search also uses CPU and memory on whatever server runs it. If product search traffic is high, running it on the primary competes with checkout and order writes. A common pattern is to run vector queries on a read replica, which has the same data and index, so the primary keeps its OLTP latency.

5The Filtering Problem

Real searches are rarely 'nearest 10 among everything'. They are 'nearest 10 among in-stock shoes in Germany'. With an HNSW index, PostgreSQL scans the graph for the nearest ef_search candidates and then applies the WHERE clause. If only 1 percent of products match the filter, most of the 100 candidates are thrown away and the query returns 1 or 2 results instead of 10, or poor ones. Recall drops sharply exactly for selective filters.

There are remedies. Raising hnsw.ef_search helps up to a point; recent pgvector versions add iterative index scans that keep searching until enough rows pass the filter (check your version); partial indexes or partitioning by a common filter, such as country, give each subset its own index; and for very selective filters, an exact search over the filtered rows using a normal B-tree index can be faster and perfect. The lesson is to test recall and latency with the real filters, not with unfiltered queries.

Step throughFiltered vector query with HNSW
  1. Walk the graph to ef_search candidatesfor example 100 nearest
  2. Apply WHERE in_stock AND country = 'DE'many candidates rejected
  3. At least 10 rows left?
    YesReturn the top 10NoToo few or worse results
  4. Remedieshigher ef_search, iterative scans, partial indexes, exact search on few rows

4 steps in order. Play them, or step through them one at a time.

6When a Dedicated Store Wins, and What We Would Choose

A dedicated vector database earns its cost at much larger scale, hundreds of millions or billions of vectors that need sharding, at very high query rates with strict tail latency targets, or when its specialised filtering and hybrid search features matter more than SQL. It also isolates search load completely.

For 20 million products, we would start with pgvector, an HNSW index and halfvec if recall stays acceptable, serving queries from a replica. We would benchmark first with real queries and filters, and move to a dedicated store only if measurements show we cannot meet the latency or recall target.

7Follow-up questions

How would you benchmark recall?

Take a sample of a few hundred real or realistic queries, with their real filters. For each, compute the exact top 10 with a brute-force search (for example by disabling index scans for that session), then run the same query through the ANN index, and measure the share of the exact results it returned. Average that recall at 10 and record latency at the 50th and 99th percentile, repeating for several ef_search values and index settings, and for the dedicated store if we are comparing. Recall on unfiltered queries alone would hide the filtering problem.

Should embeddings be generated inside the database?

Usually not. The application or a pipeline calls the embedding model and writes the vector, so model calls do not hold database transactions open and can be retried. When the model changes, all vectors must be regenerated, which is a batch job with a new column or table.

Does pgvector support hybrid search with keywords?

Not as one feature, but we can combine a full-text search query and a vector query in SQL, for example with reciprocal rank fusion of the two result lists, because both live in the same database.

8Conclusion

In this answer, we compared pgvector with a dedicated vector database for 20 million product descriptions. pgvector stores vectors in a column and supports HNSW and IVFFlat indexes with cosine, L2 and inner product distance, and its main benefit is one database: transactions with product data, SQL filters and joins, and existing backups and access control. Its costs are index build time and memory, recall versus speed tuning with ef_search or lists and probes, load on the primary that a replica can absorb, and recall loss with selective filters that we must test. A dedicated store wins at very large scale or strict latency targets, at the price of syncing data and running another system.

What to say in the interviewAbout 90 seconds aloud

Open with

For 20 million products I would start with pgvector. It adds a vector column and supports HNSW and IVFFlat indexes with cosine, L2 and inner product distance. The big advantage is that it is one database: the embedding sits in the product row, so updates are transactional and search never returns a deleted product, filters and joins like in stock or category are plain SQL, and we already have backups, access control and monitoring.

Then, if they want more

The costs are real, so I would size them. 20 million 768-dimensional float vectors are about 60 GB before the index, HNSW needs memory and hours to build, and query recall versus speed is tuned with ef_search for HNSW or lists and probes for IVFFlat.

Search also adds load, so I would serve it from a read replica rather than the primary. The trap I would test for is filtered search: HNSW finds candidates first and filters afterwards, so selective WHERE clauses can lose recall, and I would benchmark with real filters, trying higher ef_search, iterative scans or partial indexes.

A dedicated vector database makes sense at much larger scale, or for strict latency targets or special filtering needs, but it means syncing data, handling deletes and running another system, so I would only move if benchmarks show pgvector cannot meet the targets.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
PostgreSQL documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "You must add semantic search over 20 million product descriptions. Compare using the...": https://scrivio-interview-library.netlify.app/databases/postgresql/6ddbf6bbf2/