From: natehaze (@natehaze) <noreply+natehaze@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #776: Iterative Scan doesn't support filtering by subquery
Date: Thu, 13 Feb 2025 16:28:18 +0000
Message-ID: <gh-pgvector-pgvector-776@github.com> (raw)
Please tell me if I am wrong. This is similar to
https://github.com/pgvector/pgvector/issues/722 (Index support for filters beyond equivalence)
After a bunch of research, experimentation - and no clear guidance in Postgres documentation - I believe:
1. A subquery can never be applied to the `filter` of an index scan
2. For `pgvector` to perform an iterative scan, the `where` condition must be applied by the planner to the `filter` of the index scan
It would be incredibly helpful - to people like me, dangerous enough to get in the weeds, but not knowing all the particulars of the postgres optimizer - if this were **spelled out in the pgvector documentation for iterative scan**.
Needless to say - probably similar to many - we need to filter our vector table with a sub-query based filter on the primary key. There is just no way we could denormalize enough into our table to have a non-subquery filter. **Assuming all this is true are there any plans to allow for subquery filtering in iterative scans?**
I took one of your [tests](https://github.com/pgvector/pgvector/blob/master/test/t/041_ivfflat_iterative_scan.pl) (tst table of 100k 3 dimension random vectors) and enumerated the results and what I think are the **rules**.
**DOES** are those filters that get to `filter` of the test table index scan by default - no other settings changed
**CAN** are those that are capable of `filter` in the index scan (if you force the plan like with `pg_hint_plan`)
**CANNOT** are those that I believe cannot be in the `filter` of the index scan (subqueries).
```
set ivfflat.probes = 9;
set ivfflat.iterative_scan = relaxed_order;
explain(analyze)
with
search_vector(x) as (
select array[random(), random(), random()]::vector
from (select setseed(0.42)) s
)
select
v <-> (select x from search_vector) as distance,
*
from tst
-------DOES
--where i % 10000 = 0
--where random() < 0.0001
--where is_in_set(i) -- custom function
-------CAN
--where i = any(filter_func()) -- custom function
--where i in (1, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000) - rewritten to ->
--where i = any('{0,10000,20000,30000,40000,50000,60000,70000,80000,90000,100000}'::int[])
-------CANNOT : as Filter
--where (select i % 10000 = 0 where i %10000 = 0)
where i in (select generate_series(0, 100000, 10000))
--where i = any(select generate_series(0, 100000, 10000))
order by distance
limit 11
```