Files
supabase/apps/docs/content/guides/ai/going-to-prod.mdx
supabase-vercel-tedd[bot] 36e370aa5c docs(ai): explain how hnsw.ef_search caps HNSW results and how to set it (#53997)
## Problem

The HNSW tuning section said only that a higher `ef_search` gives better
accuracy and slower queries. A reader whose query uses a `limit` above
`ef_search` gets fewer rows back. The docs didn't explain why, what the
range is, or how to change the setting.

Related to
[FDBKIN-12895](https://linear.app/supabase/issue/FDBKIN-12895/document-performance-trade-offs-of-pgvectors-hnsw-ef-search-parameter)
Closes
[DOCS-1800](https://linear.app/supabase/issue/DOCS-1800/explain-that-hnswef-search-caps-hnsw-results-and-how-to-set-it)

## Solution

The `ef_search` bullet on the "Going to production" guide now names the
`hnsw.ef_search` setting, its default, and its valid range. Plain
paragraphs after the list explain how it caps the rows an HNSW scan
returns and how to set it. They cover a session, one transaction, a
transaction-mode pooler, and a database function called through the Data
API. They also point to iterative index scans for returning more rows,
and don't repeat that explanation. The filtering section of the HNSW
indexes page now names `hnsw.ef_search` as the limit, so the two pages
agree.

## How to verify

1. Open
https://docs-git-tedd-docs-1800-supabase.vercel.app/docs/guides/ai/going-to-prod#hnsw-understanding-efconstruction--efsearch--and-m
and
https://docs-git-tedd-docs-1800-supabase.vercel.app/docs/guides/ai/vector-indexes/hnsw-indexes#filtering-with-hnsw-indexes.
2. The `ef_search` bullet states the `hnsw.ef_search` default (40) and
range (1 to 1,000), and stays one paragraph like the `m` and
`ef_construction` bullets.
3. A paragraph after the list says that without iterative index scans,
an HNSW index scan returns at most `ef_search` rows, even when the
query's `limit` is larger.
4. A SQL block shows a `set` statement and a `set local` statement
inside `begin` / `commit`. A second SQL block shows a database function
with a `set hnsw.ef_search = 100` clause.
5. The paragraph about setting the value links to "Transaction mode
limitations" and to "Querying a vector / embedding", and both links land
on those sections.
6. A paragraph links to "Filtering with HNSW indexes", and the link
lands on that section.
7. In "Filtering with HNSW indexes", the sentence about selective
filters names `hnsw.ef_search` as the limit on rows a scan returns, and
its link lands on the going-to-prod HNSW section.

## Checks

Ran `pnpm turbo run typecheck`, `pnpm turbo run lint`, and `pnpm exec
oxfmt --check` on the changed docs. Ran both SQL blocks in page order on
Postgres 18 with pgvector 0.8.1, against 20k random vectors with an HNSW
`vector_l2_ops` index and `vector` installed in the `extensions` schema.
At the default, `limit 100` returned 40 rows. With `set` or `set local
hnsw.ef_search = 100`, it returned 100 rows. The `set local` value reset
after `commit`, and `1001` was rejected as outside 1..1000. The function
with `set hnsw.ef_search = 100` returned 100 rows for `limit 200`, and
the session value stayed at 40. At `ef_search = 1000`, `limit 1500`
returned 1000 rows, and 1500 after `set hnsw.iterative_scan =
relaxed_order`. Docs tests skipped: they need Docker.

## Checklist

- [x] I have read the [Contributing
Guidelines](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] I have checked that the change works in the preview deployment

## Feedback for Tedd

To ask Tedd for a change to this PR, reply in Tedd's session on
[DOCS-1800](https://linear.app/supabase/issue/DOCS-1800/explain-that-hnswef-search-caps-hnsw-results-and-how-to-set-it).
Tedd doesn't read comments on this PR.

---------

Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
2026-11-24 20:02:50 -07:00

160 lines
11 KiB
Plaintext

---
id: 'ai-going-to-prod'
title: 'Going to Production'
description: 'Checklist for going to production with your AI application.'
subtitle: 'Going to production checklist for AI applications.'
sidebar_label: 'Going to Production'
---
This guide helps you prepare your application for production. It provides actionable steps to help you scale your application, ensure that it is reliable, can handle the load, and provide optimal accuracy for your use case.
See our [Engineering for Scale](/docs/guides/ai/engineering-for-scale) guide for more information about engineering at scale.
## Do you need indexes?
Sequential scans will result in significantly higher latencies and lower throughput, guaranteeing 100% accuracy and not being RAM bound.
There are a couple of cases where you might not need indexes:
- You have a small dataset and don't need to scale it.
- You are not expecting high amounts of vector search queries per second.
- You need to guarantee 100% accuracy.
You don't have to create indexes in these cases and can use sequential scans instead. This type of workload will not be RAM bound and will not require any additional resources but will result in higher latencies and lower throughput. Extra CPU cores may help to improve queries per second, but it will not help to improve latency.
On the other hand, if you need to scale your application, you will need to [create indexes](/docs/guides/ai/vector-indexes). This will result in lower latencies and higher throughput, but will require additional RAM to make use of Postgres Caching. Also, using indexes will result in lower accuracy, since you are replacing exact (KNN) search with approximate (ANN) search.
## HNSW vs IVFFlat indexes
`pgvector` supports two types of indexes: HNSW and IVFFlat. We recommend using [HNSW](/docs/guides/ai/vector-indexes/hnsw-indexes) because of its [performance](/blog/increase-performance-pgvector-hnsw#hnsw-performance-1536-dimensions) and [robustness against changing data](/docs/guides/ai/vector-indexes/hnsw-indexes#when-should-you-create-hnsw-indexes).
<Image
alt="dbpedia embeddings comparing ivfflat and hnsw queries-per-second using the 4XL compute add-on"
src={{
light: '/docs/img/ai/going-prod/dbpedia-ivfflat-vs-hnsw-4xl--light.png',
dark: '/docs/img/ai/going-prod/dbpedia-ivfflat-vs-hnsw-4xl--dark.png',
}}
width={1052}
height={796}
/>
## HNSW, understanding `ef_construction`, `ef_search`, and `m` [#hnsw-understanding-efconstruction--efsearch--and-m]
Index build parameters:
- `m` is the number of bi-directional links created for every new element during construction. Higher `m` is suitable for datasets with high dimensionality and/or high accuracy requirements. Reasonable values for `m` are between 2 and 100. Range 12-48 is a good starting point for most use cases (16 is the default value).
- `ef_construction` is the size of the dynamic list for the nearest neighbors (used during the construction algorithm). Higher `ef_construction` will result in better index quality and higher accuracy, but it will also increase the time required to build the index. `ef_construction` has to be at least 2 \* `m` (64 is the default value). At some point, increasing `ef_construction` does not improve the quality of the index. You can measure accuracy when `ef_search`=`ef_construction`: if accuracy is lower than 0.9, then there is room for improvement.
Search parameters:
- `ef_search` is the size of the dynamic list for the nearest neighbors (used during the search). Increasing `ef_search` improves accuracy but makes each query slower. The setting is `hnsw.ef_search`, which defaults to 40 and accepts values from 1 to 1,000.
Without iterative index scans, an HNSW index scan returns at most `ef_search` rows, even when the query's `limit` is larger.
Set `hnsw.ef_search` for the rest of the session with `set`, or for one transaction with `set local`. A [transaction-mode pooler](/docs/guides/database/connecting-to-postgres#transaction-mode-limitations) doesn't keep session settings between your transactions, so use `set local` in the same transaction as the query. For queries through the Data API, add `set hnsw.ef_search = 100` to the definition of the [database function](/docs/guides/ai/vector-columns#querying-a-vector--embedding) you call.
```sql
-- For the rest of the session
set hnsw.ef_search = 100;
-- For one transaction only
begin;
set local hnsw.ef_search = 100;
select * from items order by embedding <-> '[3,1,2]' limit 100;
commit;
```
```sql
-- For every call to a database function
create or replace function match_items(query_embedding extensions.vector(3), match_count int)
returns setof items
language sql stable
set hnsw.ef_search = 100
as $$
select * from items
order by embedding <-> query_embedding
limit match_count;
$$;
```
To return more rows than `ef_search`, including more than 1,000, turn on iterative index scans with `hnsw.iterative_scan`. Iterative scans need `pgvector` 0.8.0 or later. See [Filtering with HNSW indexes](/docs/guides/ai/vector-indexes/hnsw-indexes#filtering-with-hnsw-indexes).
<Image
alt="dbpedia embeddings comparing hnsw queries-per-second using different build parameters"
src={{
light: '/docs/img/ai/going-prod/dbpedia-hnsw-build-parameters--light.png',
dark: '/docs/img/ai/going-prod/dbpedia-hnsw-build-parameters--dark.png',
}}
width={1052}
height={796}
/>
## IVFFlat, understanding `probes` and `lists`
Indexes used for approximate vector similarity search in pgvector divides a dataset into partitions. The number of these partitions is defined by the `lists` constant. The `probes` controls how many lists are going to be searched during a query.
The values of lists and probes directly affect accuracy and queries per second (QPS).
- Higher `lists` means an index will be built slower, but you can achieve better QPS and accuracy.
- Higher `probes` means that select queries will be slower, but you can achieve better accuracy.
- `lists` and `probes` are not independent. Higher `lists` means that you will have to use higher `probes` to achieve the same accuracy.
You can find more examples of how `lists` and `probes` constants affect accuracy and QPS in [pgvector 0.4.0 performance](/blog/pgvector-performance) blog post.
The chart below shows how the IVFFlat lists count affects accuracy and queries-per-second.
<Image
alt="Chart showing how the IVFFlat lists count affects query accuracy and queries-per-second."
src={{
light: '/docs/img/ai/going-prod/lists-count--light.png',
dark: '/docs/img/ai/going-prod/lists-count--dark.png',
}}
width={1467}
height={808}
/>
## Performance tips when using indexes
First, a few generic tips which you can pick and choose from:
1. The Supabase managed platform will automatically optimize Postgres configs for you based on your compute add-on. But if you self-host, consider **adjusting your Postgres config** based on RAM & CPU cores. See [example optimizations](https://gist.github.com/egor-romanov/323e2847851bbd758081511785573c08) for more details.
2. Prefer `inner-product` to `L2` or `Cosine` distances if your vectors are normalized (like `text-embedding-ada-002`). If embeddings are not normalized, `Cosine` distance should give the best results with an index.
3. **Pre-warm your database.** Implement the warm-up technique before transitioning to production or running benchmarks.
- Use [pg_prewarm](https://www.postgresql.org/docs/current/pgprewarm.html) to load the index into RAM `select pg_prewarm('vecs.docs_vec_idx');`. This will help to avoid cold cache issues.
- Execute 10,000 to 50,000 "warm-up" queries before each benchmark or prod. This helps to use cache and buffers more efficiently.
4. **Establish your workload.** Fine-tune `m` and `ef_construction` or `lists` constants for the pgvector index to accelerate your queries (at the expense of a slower build times). For instance, for benchmarks with 1,000,000 OpenAI embeddings, we set `m` and `ef_construction` to 32 and 80, and it resulted in 35% higher QPS than 24 and 56 values respectively.
5. **Benchmark your own specific workloads.** Doing this during cache warm-up helps gauge the best value for the index build parameters, balancing accuracy with queries per second (QPS).
## Going into production
1. Decide if you are going to use indexes or not. You can skip the rest of this guide if you do not use indexes.
2. Over-provision RAM during preparation. You can scale down in step `5`, but it's better to start with a larger size to get the best results for RAM requirements. (We'd recommend at least 8XL if you're using Supabase.)
3. Upload your data to the database. If you use the [`vecs`](/docs/guides/ai/python/api) library, it will automatically generate an index with default parameters.
4. Run a benchmark using randomly generated queries and observe the results. Again, you can use the `vecs` library with the `ann-benchmarks` tool. Do it with default values for index build parameters, you can later adjust them to get the best results.
5. Monitor the RAM usage, and save it as a note for yourself. You would likely want to use a compute add-on in the future that has the same amount of RAM that was used at the moment (both actual RAM usage and RAM used for cache and buffers).
6. Scale down your compute add-on to the one that would have the same amount of RAM used at the moment.
7. Repeat step 3 to load the data into RAM. You should see QPS increase on subsequent runs, and stop when it no longer increases.
8. Run a benchmark using real queries and observe the results. You can use the `vecs` library for that as well with `ann-benchmarks` tool. Tweak `ef_search` for HNSW or `probes` for IVFFlat until you see that both accuracy and QPS match your requirements.
9. If you want higher QPS you can increase `m` and `ef_construction` for HNSW or `lists` for IVFFlat parameters (consider switching from IVF to HNSW). You have to rebuild the index with a higher `m` and `ef_construction` values and repeat steps 6-7 to find the best combination of `m`, `ef_construction` and `ef_search` constants to achieve the best QPS and accuracy values. Higher `m`, `ef_construction` mean that index will build slower, but you can achieve better QPS and accuracy. Higher `ef_search` mean that select queries will be slower, but you can achieve better accuracy.
## Useful links
Don't forget to check out the general [Production Checklist](/docs/guides/deployment/going-into-prod) to ensure your project is secure, performant, and will remain available for your users.
You can look at our [Choosing Compute Add-on](/docs/guides/ai/choosing-compute-addon) guide to get a basic understanding of how much compute you might need for your workload.
Or take a look at our [pgvector 0.5.0 performance](/blog/increase-performance-pgvector-hnsw) and [pgvector 0.4.0 performance](/blog/pgvector-performance) blog posts to see what pgvector is capable of and how the above technique can be used to achieve the best results.
The chart below plots requests-per-second against compute size.
<Image
alt="Chart plotting requests-per-second against Supabase compute size."
src={{
light: '/docs/img/ai/going-prod/size-to-rps--light.png',
dark: '/docs/img/ai/going-prod/size-to-rps--dark.png',
}}
width={1427}
height={862}
/>