Why Does pgvector Return Fewer Results Than LIMIT in Supabase?
You ask for 10 matches and get 3. The index finds its nearest rows first, and your tenant filter or access policy removes most of them afterwards.
Key Takeaways
- pgvector returns fewer rows than LIMIT because an HNSW index scan collects about 40 nearest rows by default, and filters are applied after that.
- If a filter matches 10% of rows, about 4 of those 40 survive. If it matches 1%, you may get none.
- The fix in pgvector 0.8.0 or later is SET hnsw.iterative_scan = strict_order. It is off by default.
- For filters that match a small share of rows, a normal index on the filter column can give exact results instead.
pgvector returns fewer results than your LIMIT when a filter runs after the vector index. An HNSW index scan collects about 40 nearest rows by default. Your WHERE clause or row-level security (RLS) policy then removes the rows that do not match. With pgvector 0.8.0 or later, turn on iterative scans:
begin;
set local hnsw.iterative_scan = strict_order;
select id, content
from document_sections
order by embedding <=> $1
limit 10;
commit;How many results should you expect?
The pgvector documentation gives the first row of this table. The others use the same arithmetic. They are averages and assume matching rows are spread evenly.
| Share of rows the user can see | Rows left from a scan of 40 | With LIMIT 10 you get |
|---|---|---|
| 10% | About 4 | About 4 |
| 25% | About 10 | About 10, often fewer |
| 1% | Less than 1 | Often nothing |
This is why the problem often appears as a product grows. With two customers, each can see about half of the table. With a hundred, each sees about 1%.
How do you confirm this is the cause?
- Check the version. Iterative scans need pgvector 0.8.0 or later.
select extversion from pg_extension where extname = 'vector'; - Compare with an exact search. Run the same query in a transaction with index scans turned off. It will be slower. If the missing rows appear, the index scan was dropping them.
begin; set local enable_indexscan = off; -- run your query here commit; - Use the right role. Run both queries as the signed-in user your app uses. An administrator role can skip RLS and hide the problem.
Which fix should you use?
| Your situation | Fix | Cost |
|---|---|---|
| Most apps on pgvector 0.8.0 or later | hnsw.iterative_scan = strict_order | Scans more of the index, up to about 20,000 rows by default |
| An older pgvector version | Raise hnsw.ef_search, for example to 200 | Slower queries, and a small customer can still get too few rows |
| The filter matches a small share of rows | A normal B-tree index on the filter column | Exact results, but the query must filter on that column directly |
| A few large customers | A separate partial vector index for each | More indexes to build and store |
| Many customers | Partition the table by customer | A larger schema change |
How do you set this in Supabase?
A setting made once on a connection may not apply to your next query when connections are shared. Supabase's production guide says to use set local in the same transaction as the query. If you search through the Data API, put the setting in the database function:
create or replace function match_sections(
query_embedding extensions.vector(384),
match_count int
)
returns setof document_sections
language sql
stable
security invoker
set hnsw.iterative_scan = 'strict_order'
as $$
select *
from document_sections
order by embedding <=> query_embedding
limit match_count;
$$;Change the table name and vector size to match yours. The operator must match your index: <=> is cosine distance. security invoker runs the function as the caller, so RLS still applies.
What if you still get too few rows?
- The scan stops at
hnsw.max_scan_tuples, 20,000 by default. Raise it if the matching rows are rare. - If that does not help, the pgvector documentation suggests raising
hnsw.scan_mem_multiplier. - Check that the user can see enough rows at all. No setting will return rows the policy forbids, and it should not.
For the wider design, see our guide to building B2B AI agents on Supabase.
Keep Reading
How to Let an AI Agent Query Supabase Without Bypassing RLS
Building B2B AI Agents with Supabase and PostgreSQL: Architecture and Trade-offs
How to Build a RAG System (Retrieval-Augmented Generation)
Supabase Is Deprecating anon and service_role Keys: What to Change
Frequently Asked Questions
What is the default hnsw.ef_search in pgvector?
The default is 40. Without iterative scans, an HNSW index scan returns at most that many rows, even when the query's LIMIT is larger. Supabase's documentation says it accepts values from 1 to 1,000.
Does row-level security affect pgvector search results?
Yes. Supabase's documentation says semantic search respects RLS policies. A policy removes rows the user may not see, so like any other filter it can leave fewer results than you asked for.
Should I use strict_order or relaxed_order?
Use strict_order when results must be in exact order of distance. Use relaxed_order when you can accept slightly out-of-order results in exchange for better recall.
Why does my query work in the Supabase SQL Editor but not in my app?
The two may run as different database roles. An administrator role or table owner can skip your access policies, so the filter that removes rows in your app is not applied. Test as a signed-in user.
Free Tools
Tell us what you're building — book a free scoping call.
Pick a time that works and walk us through your project — 30 minutes, straight to the point. You leave with a concrete plan, timeline, and cost. No sales pitch — if we're not the right fit, we'll say so.
Get new playbooks by email
Occasional, no-fluff field notes on building production AI — new guides and tools, straight to your inbox. Unsubscribe anytime.