pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #781: index not used with cte
11+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #781: index not used with cte
@ 2025-02-21 16:30  "rferraton (@rferraton)" <noreply+rferraton@github.com>
  0 siblings, 0 replies; 11+ messages in thread

From: rferraton (@rferraton) @ 2025-02-21 16:30 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Index (ivfflat at least) are not used when the query is a **cte** :


```sql
WITH vars AS (
    SELECT chunk_embedding AS qvt_embedding
      FROM dbpedia_14
     WHERE chunk_index = 473679
)
SELECT     
    a.title,
    vars.qvt_embedding <-> a.chunk_embedding AS l2distance
FROM dbpedia_14 a
CROSS JOIN vars
ORDER BY l2distance asc
limit 10;
```
==>
```
Limit  (cost=83624.74..83624.76 rows=10 width=27) (actual time=3349.047..3349.060 rows=10 loops=1)
  Output: a.title, ((dbpedia_14.chunk_embedding <-> a.chunk_embedding))
  ->  Sort  (cost=83624.74..85199.92 rows=630073 width=27) (actual time=3349.046..3349.058 rows=10 loops=1)
        Output: a.title, ((dbpedia_14.chunk_embedding <-> a.chunk_embedding))
        Sort Key: ((dbpedia_14.chunk_embedding <-> a.chunk_embedding))
        Sort Method: top-N heapsort  Memory: 26kB
        ->  Nested Loop  (cost=0.42..70009.09 rows=630073 width=27) (actual time=0.105..3276.230 rows=630073 loops=1)
              Output: a.title, (dbpedia_14.chunk_embedding <-> a.chunk_embedding)
              ->  Index Scan using dbpedia_14_chunk_index_idx on public.dbpedia_14  (cost=0.42..8.44 rows=1 width=18) (actual time=0.068..0.080 rows=1 loops=1)
                    Output: dbpedia_14.label, dbpedia_14.title, dbpedia_14.content, dbpedia_14.split, dbpedia_14.content_word_count, dbpedia_14.chunk, dbpedia_14.content_n_chunks, dbpedia_14.chunk_index, dbpedia_14.chunk_word_count, dbpedia_14.chunk_embedding
                    Index Cond: (dbpedia_14.chunk_index = 473679)
              ->  Seq Scan on public.dbpedia_14 a  (cost=0.00..62124.73 rows=630073 width=37) (actual time=0.008..163.984 rows=630073 loops=1)
                    Output: a.label, a.title, a.content, a.split, a.content_word_count, a.chunk, a.content_n_chunks, a.chunk_index, a.chunk_word_count, a.chunk_embedding
Planning Time: 0.129 ms
Execution Time: 3349.075 ms
```


Whereas the index is used using a **subquery** :

```sql
SELECT     
    a.title,
    (SELECT chunk_embedding AS qvt_embedding FROM dbpedia_14  WHERE chunk_index = 473679) <-> a.chunk_embedding AS l2distance
FROM dbpedia_14 a
ORDER BY l2distance
limit 10;
```

```
Limit  (cost=12603.37..12619.43 rows=10 width=27) (actual time=15.279..15.413 rows=10 loops=1)
  Output: a.title, (((InitPlan 1).col1 <-> a.chunk_embedding))
  InitPlan 1
    ->  Index Scan using dbpedia_14_chunk_index_idx on public.dbpedia_14  (cost=0.42..8.44 rows=1 width=18) (actual time=0.020..0.021 rows=1 loops=1)
          Output: dbpedia_14.chunk_embedding
          Index Cond: (dbpedia_14.chunk_index = 473679)
  ->  Index Scan using dbpedia_14_chunk_embedding_idx on public.dbpedia_14 a  (cost=12594.93..1024652.46 rows=630073 width=27) (actual time=15.278..15.410 rows=10 loops=1)
        Output: a.title, ((InitPlan 1).col1 <-> a.chunk_embedding)
        Order By: (a.chunk_embedding <-> (InitPlan 1).col1)
Planning Time: 0.127 ms
Execution Time: 15.436 ms
```

Test on 0.80.0 pgvector and postgreSQL 15 (windows)
Test on 0.80.0 pgvector and postgreSQL 17 (linux/docker)

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

* Re: [pgvector/pgvector] issue #781: index not used with cte
@ 2025-02-21 16:42  "rferraton (@rferraton)" <noreply+rferraton@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: rferraton (@rferraton) @ 2025-02-21 16:42 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@rferraton renamed this.

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

* Re: [pgvector/pgvector] issue #781: index not used with cte
@ 2025-02-21 17:05  "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: ankane (@ankane) @ 2025-02-21 17:05 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

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

* Re: [pgvector/pgvector] issue #781: index not used with cte
@ 2025-02-21 17:05  "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: ankane (@ankane) @ 2025-02-21 17:05 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @rferraton, those queries aren't equivalent (see the output columns).

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

* Re: [pgvector/pgvector] issue #781: index not used with cte
@ 2025-02-21 17:15  "rferraton (@rferraton)" <noreply+rferraton@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: rferraton (@rferraton) @ 2025-02-21 17:15 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Sorry, i udapte the query and plan for the first one. The problem is the same

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

* Re: [pgvector/pgvector] issue #781: index not used with cte
@ 2025-02-21 17:21  "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

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

What's the explain output of the first query with `SET enable_seqscan = off;`?

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

* Re: [pgvector/pgvector] issue #781: index not used with cte
@ 2025-02-21 17:47  "rferraton (@rferraton)" <noreply+rferraton@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: rferraton (@rferraton) @ 2025-02-21 17:47 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

No, if i set the enable_seqscan = off the plan is the following one (still full scan, the cost is 10B but it does not use the index ! ): 

```
Limit  (cost=10000083624.74..10000083624.76 rows=10 width=27) (actual time=4243.354..4243.366 rows=10 loops=1)
  Output: a.title, ((dbpedia_14.chunk_embedding <-> a.chunk_embedding))
  ->  Sort  (cost=10000083624.74..10000085199.92 rows=630073 width=27) (actual time=4122.775..4122.786 rows=10 loops=1)
        Output: a.title, ((dbpedia_14.chunk_embedding <-> a.chunk_embedding))
        Sort Key: ((dbpedia_14.chunk_embedding <-> a.chunk_embedding))
        Sort Method: top-N heapsort  Memory: 26kB
        ->  Nested Loop  (cost=10000000000.42..10000070009.08 rows=630073 width=27) (actual time=0.212..4037.134 rows=630073 loops=1)
              Output: a.title, (dbpedia_14.chunk_embedding <-> a.chunk_embedding)
              ->  Index Scan using dbpedia_14_chunk_index_idx on public.dbpedia_14  (cost=0.42..8.44 rows=1 width=18) (actual time=0.122..0.133 rows=1 loops=1)
                    Output: dbpedia_14.label, dbpedia_14.title, dbpedia_14.content, dbpedia_14.split, dbpedia_14.content_word_count, dbpedia_14.chunk, dbpedia_14.content_n_chunks, dbpedia_14.chunk_index, dbpedia_14.chunk_word_count, dbpedia_14.chunk_embedding
                    Index Cond: (dbpedia_14.chunk_index = 473679)
              ->  Seq Scan on public.dbpedia_14 a  (cost=10000000000.00..10000062124.73 rows=630073 width=37) (actual time=0.016..235.199 rows=630073 loops=1)
                    Output: a.label, a.title, a.content, a.split, a.content_word_count, a.chunk, a.content_n_chunks, a.chunk_index, a.chunk_word_count, a.chunk_embedding
Planning Time: 0.324 ms
JIT:
  Functions: 6
  Options: Inlining true, Optimization true, Expressions true, Deforming true
  Timing: Generation 8.395 ms (Deform 3.368 ms), Inlining 12.487 ms, Optimization 51.771 ms, Emission 56.318 ms, Total 128.972 ms
Execution Time: 4252.306 ms
```

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

* Re: [pgvector/pgvector] issue #781: index not used with cte
@ 2025-02-21 19:15  "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: ankane (@ankane) @ 2025-02-21 19:15 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

This indicates the planner isn't considering the index. I don't immediately see why, but there's not much pgvector can do if this is the case. What's the explain output if you add `MATERIALIZED` to the CTE (with `SET enable_seqscan = off;`)? Also, is `dbpedia_14_chunk_index_idx` a unique index?

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

* Re: [pgvector/pgvector] issue #781: index not used with cte
@ 2025-02-22 18:01  "rferraton (@rferraton)" <noreply+rferraton@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: rferraton (@rferraton) @ 2025-02-22 18:01 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Materialized does not change anything (with or without enable_seqscan) :

the plan with Materialized and enable_seqscan=off; ==>

```
Limit  (cost=10000083624.76..10000083624.78 rows=10 width=27) (actual time=3701.340..3701.343 rows=10 loops=1)
  Output: a.title, ((vars.qvt_embedding <-> a.chunk_embedding))
  CTE vars
    ->  Index Scan using dbpedia_14_chunk_index_idx on public.dbpedia_14  (cost=0.42..8.44 rows=1 width=18) (actual time=0.062..0.064 rows=1 loops=1)
          Output: dbpedia_14.chunk_embedding
          Index Cond: (dbpedia_14.chunk_index = 473679)
  ->  Sort  (cost=10000083616.31..10000085191.50 rows=630073 width=27) (actual time=3526.610..3526.611 rows=10 loops=1)
        Output: a.title, ((vars.qvt_embedding <-> a.chunk_embedding))
        Sort Key: ((vars.qvt_embedding <-> a.chunk_embedding))
        Sort Method: top-N heapsort  Memory: 26kB
        ->  Nested Loop  (cost=10000000000.00..10000070000.66 rows=630073 width=27) (actual time=0.106..3446.404 rows=630073 loops=1)
              Output: a.title, (vars.qvt_embedding <-> a.chunk_embedding)
              ->  CTE Scan on vars  (cost=0.00..0.02 rows=1 width=32) (actual time=0.066..0.068 rows=1 loops=1)
                    Output: vars.qvt_embedding
              ->  Seq Scan on public.dbpedia_14 a  (cost=10000000000.00..10000062124.73 rows=630073 width=37) (actual time=0.014..130.910 rows=630073 loops=1)
                    Output: a.label, a.title, a.content, a.split, a.content_word_count, a.chunk, a.content_n_chunks, a.chunk_index, a.chunk_word_count, a.chunk_embedding
Planning Time: 0.405 ms
JIT:
  Functions: 8
  Options: Inlining true, Optimization true, Expressions true, Deforming true
  Timing: Generation 1.517 ms (Deform 0.991 ms), Inlining 101.864 ms, Optimization 43.133 ms, Emission 29.729 ms, Total 176.242 ms
Execution Time: 3880.476 ms
```

And yes the index `dbpedia_14_chunk_index_idx` is a unique index.

May be you can re-open the issue : it seams, like you said, that the planner ignore the ivfflat index in this case.



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

* Re: [pgvector/pgvector] issue #781: index not used with cte
@ 2025-02-22 18:27  "ankane (@ankane)" <noreply+ankane@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: ankane (@ankane) @ 2025-02-22 18:27 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

As I mentioned earlier, there's not much pgvector can do if the planner isn't considering the index. You'll need to write the query in a way where the planner does consider it (or get this changed in the planner code).

```sql
EXPLAIN ANALYZE WITH vars AS (
    SELECT chunk_embedding AS qvt_embedding FROM dbpedia_14 WHERE chunk_index = 473679
)
SELECT a.title, (SELECT qvt_embedding FROM vars) <-> a.chunk_embedding AS l2distance
FROM dbpedia_14 a ORDER BY l2distance LIMIT 10;
```

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

* Re: [pgvector/pgvector] issue #781: index not used with cte
@ 2025-02-22 22:56  "rferraton (@rferraton)" <noreply+rferraton@github.com>
  9 siblings, 0 replies; 11+ messages in thread

From: rferraton (@rferraton) @ 2025-02-22 22:56 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Agree !

This last query you provide (that avoid join with the cte and use a sub-select instead) works greats : 

```
Limit  (cost=1267.94..1284.18 rows=10 width=27) (actual time=15.346..16.651 rows=10 loops=1)
  InitPlan 1
    ->  Index Scan using dbpedia_14_chunk_index_idx on dbpedia_14  (cost=0.42..8.44 rows=1 width=18) (actual time=0.155..0.156 rows=1 loops=1)
          Index Cond: (chunk_index = 473679)
  ->  Index Scan using dbpedia_14_chunk_embedding_idx on dbpedia_14 a  (cost=1259.49..1024652.46 rows=630073 width=27) (actual time=15.344..16.646 rows=10 loops=1)
        Order By: (chunk_embedding <-> (InitPlan 1).col1)
Planning Time: 1.475 ms
Execution Time: 16.745 ms
```

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


end of thread, other threads:[~2025-02-22 22:56 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-02-21 16:30 [pgvector/pgvector] issue #781: index not used with cte "rferraton (@rferraton)" <noreply+rferraton@github.com>
2025-02-21 16:42 ` "rferraton (@rferraton)" <noreply+rferraton@github.com>
2025-02-21 17:05 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-02-21 17:05 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-02-21 17:15 ` "rferraton (@rferraton)" <noreply+rferraton@github.com>
2025-02-21 17:21 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-02-21 17:47 ` "rferraton (@rferraton)" <noreply+rferraton@github.com>
2025-02-21 19:15 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-02-22 18:01 ` "rferraton (@rferraton)" <noreply+rferraton@github.com>
2025-02-22 18:27 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-02-22 22:56 ` "rferraton (@rferraton)" <noreply+rferraton@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