pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #776: Iterative Scan doesn't support filtering by subquery
4+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #776: Iterative Scan doesn't support filtering by subquery
@ 2025-02-13 16:28  "natehaze (@natehaze)" <noreply+natehaze@github.com>
  0 siblings, 0 replies; 4+ messages in thread

From: natehaze (@natehaze) @ 2025-02-13 16:28 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

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
```

^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #776: Iterative Scan doesn't support filtering by subquery
@ 2025-02-13 23:34  "ankane (@ankane)" <noreply+ankane@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: ankane (@ankane) @ 2025-02-13 23:34 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @natehaze, if you use `SET enable_seqscan = off;`, it'll use the index for the last two subqueries (the first subquery is invalid). In any case, the index AM doesn't distinguish between different types of filters.

Edit: Also, for these specific queries, you'll likely see better performance without an index scan.

^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #776: Iterative Scan doesn't support filtering by subquery
@ 2025-02-13 23:34  "ankane (@ankane)" <noreply+ankane@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: ankane (@ankane) @ 2025-02-13 23:34 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #776: Iterative Scan doesn't support filtering by subquery
@ 2025-02-14 20:51  "natehaze (@natehaze)" <noreply+natehaze@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: natehaze (@natehaze) @ 2025-02-14 20:51 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

OK.  (actually as ugly as it is `(select i % 10000 = 0 where i %10000 = 0)` is a valid filter.  `SET enable_seqscan = off;` does NOT trigger the index to be used for the equivalent subquery (to `i % 10000 = 0`).  I was able to finally (with the help of `pg_hint_plan`) see this work.

To answer my questions.
1. I don't think a subquery can be applied to a `filter` in postgres.
    - Actually I now see in real data that it can be applied directly to the filter as a hashed subplan
3. A `filter` is NOT necessary for `pgvector` to perform an iterative scan.
see
https://gist.github.com/natehaze/ca6c48ab861f8252d7253c5283ea7ccf

The final two queries show that the nested loop does indeed use an iterative scan.  It is incredibly hard to discern that though.  The trick is just seeing that one query (not iterative) returns 1 row and the other (iterative) returns 10 rows.  Without digging to the depths of the query plan it's impossible to know if those 10 rows are coming from an iterative scan, OR another query plan altogether.

**@ankane Would it be possible to have a debug output from pgvector that says when it DOES perform an iterative scan?**

I understand that an iterative scan is not always going to be the best plan.  However, I DO want to get a feeling for what sort of conditions trigger one and what the performance is on our real data without necessarily getting a query plan every time.

^ permalink  raw  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2025-02-14 20:51 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-02-13 16:28 [pgvector/pgvector] issue #776: Iterative Scan doesn't support filtering by subquery "natehaze (@natehaze)" <noreply+natehaze@github.com>
2025-02-13 23:34 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-02-13 23:34 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-02-14 20:51 ` "natehaze (@natehaze)" <noreply+natehaze@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox