pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #821: avg_width for Vector is 18 and for HalfVec is 1544
7+ messages / 3 participants
[nested] [flat]
* [pgvector/pgvector] issue #821: avg_width for Vector is 18 and for HalfVec is 1544
@ 2025-04-14 11:39 "idobenamram (@idobenamram)" <noreply+idobenamram@github.com>
0 siblings, 0 replies; 7+ messages in thread
From: idobenamram (@idobenamram) @ 2025-04-14 11:39 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hey guys I am seeing something weird.
I was migrating one of our tables that had an embedding column with type vector(768) to halfvec(768). same hnsw index same everything, the only difference is the halfvec.
I notices that my knn query was a little bit slower which was weird considering it should be doing less work on halfvec, and when i ran analyze on the query it looked like it wasn't using the hnsw index at all but doing a seq scan instead.
i have no idea why as there is no difference, but i saw that when i query pg_stat i see something weird:
```
SELECT *
FROM pg_stats
WHERE (tablename = 'embeddings_table_half' or tablename = 'embeddings_table')
```
i see
---------------------------------------------------------------------------------
| schemaname | tablename | attname | inherited | null_frac | avg_width | n_distinct |
|----------|----------------------|-----------|----------|----------|----------|----------|
| public | embeddings_table | embedding | false | 0.0 | 18 | -1.0 |
| public | embeddings_table_half| embedding | false | 0.0 | 1544 | -1.0 |
for some reason the avg_width of the columns are very different. the halfvec size makes sense (768 * 2 + overhead) but the vector size makes no sense... i was wondering if this might be the reason the planner is choosing not to use the hnsw index on the halfvec column.
here is how i create the index:
```
CREATE INDEX idx_embeddings_table_half_embeddings ON embeddings_table_half USING hnsw (embedding halfvec_ip_ops) WITH (m = 16, ef_construction = 64);
CREATE INDEX idx_embeddings_table_embeddings ON embeddings_table USING hnsw (embedding vector_ip_ops) WITH (m = 16, ef_construction = 64);
```
is this ok? does it matter at all?
versions
PostgreSQL 17.4 on x86_64-pc-linux-gnu, compiled by Debian clang version 12.0.1, 64-bit
pgvector 0.8.0
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #821: avg_width for Vector is 18 and for HalfVec is 1544
@ 2025-04-14 14:16 "jkatz (@jkatz)" <noreply+jkatz@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: jkatz (@jkatz) @ 2025-04-14 14:16 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Did you run `ANALYZE` on `embeddings_table_half`? Also, did your query explicitly cast `halfvec(768)` for your parameter, e.g. `SELECT ... ORDER BY $1::halfvec(768) <#> embedding;`? (In case the implicit cast didn't take; I'm not sure how you're passing the data in).
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #821: avg_width for Vector is 18 and for HalfVec is 1544
@ 2025-04-14 14:31 "idobenamram (@idobenamram)" <noreply+idobenamram@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: idobenamram (@idobenamram) @ 2025-04-14 14:31 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
here is the query i used, the only difference is the table i query and the way i cast
```
explain (analyze, buffers) WITH query_embeddings AS (
SELECT ordinality AS query_id,
embedding
FROM unnest(array[
'[...]'::halfvec
]) WITH ORDINALITY AS q(embedding, ordinality)
)
SELECT qe.query_id,
sp.product_id,
sp.similarity
FROM query_embeddings qe,
LATERAL (
SELECT product_id,
(embedding <#> qe.embedding) AS similarity
FROM embeddings_table_half
WHERE
network_id = '1c81823c-9152-4fd1-a0f6-ad5d7beb6192'
ORDER BY embedding <#> qe.embedding ASC
LIMIT 5
) sp;
```
explain output for half
```
Nested Loop (cost=300.98..301.05 rows=5 width=32) (actual time=1.049..1.053 rows=5 loops=1)
Buffers: shared hit=273
-> Function Scan on unnest q (cost=0.00..0.01 rows=1 width=40) (actual time=0.008..0.009 rows=1 loops=1)
-> Limit (cost=300.97..300.99 rows=5 width=24) (actual time=1.037..1.038 rows=5 loops=1)
Buffers: shared hit=273
-> Sort (cost=300.97..303.19 rows=885 width=24) (actual time=1.035..1.036 rows=5 loops=1)
Sort Key: ((embeddings_table_half.embedding <#> q.embedding))
Sort Method: top-N heapsort Memory: 25kB
Buffers: shared hit=273
-> Seq Scan on embeddings_table_half (cost=0.00..286.27 rows=885 width=24) (actual time=0.015..0.906 rows=885 loops=1)
Filter: (network_id = '1c81823c-9152-4fd1-a0f6-ad5d7beb6192'::uuid)
Buffers: shared hit=273
Planning:
Buffers: shared hit=1
Planning Time: 0.301 ms
Execution Time: 1.088 ms
```
explain output for vector
```
Nested Loop (cost=20.08..31.40 rows=5 width=32) (actual time=0.681..0.731 rows=5 loops=1)
Buffers: shared hit=410
-> Function Scan on unnest q (cost=0.00..0.01 rows=1 width=40) (actual time=0.007..0.008 rows=1 loops=1)
-> Limit (cost=20.08..31.29 rows=5 width=32) (actual time=0.671..0.718 rows=5 loops=1)
Buffers: shared hit=410
-> Index Scan using embeddings_table_1c81823c_embedding_idx on embeddings_table_1c81823c embeddings_table (cost=20.08..2449.81 rows=1084 width=32) (actual time=0.668..0.714 rows=5 loops=1)
Order By: (embedding <#> q.embedding)
Filter: (network_id = '1c81823c-9152-4fd1-a0f6-ad5d7beb6192'::uuid)
Buffers: shared hit=410
Planning:
Buffers: shared hit=1
Planning Time: 0.209 ms
Execution Time: 0.764 ms
```
again the only difference between the tables is the embedding column is halfvec instead of vector. same indices, same everything.
you can achieve the same result with a simpler knn query like:
```
select product_id, -1 * (embedding <#> $1) as similarity, from embeddings_table
where network_id = '1c81823c-9152-4fd1-a0f6-ad5d7beb6192'
order by embedding <#> $1 asc
limit 5
```
these checks were done by explicitly passing a vector like `'[floats...]::halfvec/vector'`
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #821: avg_width for Vector is 18 and for HalfVec is 1544
@ 2025-04-14 18:46 "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2025-04-14 18:46 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @idobenamram, the difference in `avg_width` is due to [TOAST](https://www.postgresql.org/docs/current/storage-toast.html) (the vector column is being stored out-of-line). Also, since the table will be smaller with halfvec, a sequence scan can be cheaper for the same number of vectors.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #821: avg_width for Vector is 18 and for HalfVec is 1544
@ 2025-04-14 18:46 "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2025-04-14 18:46 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #821: avg_width for Vector is 18 and for HalfVec is 1544
@ 2025-04-15 08:04 "idobenamram (@idobenamram)" <noreply+idobenamram@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: idobenamram (@idobenamram) @ 2025-04-15 08:04 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
ah interesting thank you for the information, i didn't know about toast.
one last question: what i don't understand is then why does the planner pick the index for the vector type and not for the halfvec type? when i try to force it to use the index (i was able to achieve this by setting `SET random_page_cost = 1.1;`)
it comes out mostly faster than the seq scan.
again whats not clear to me is why for the vector type works with the default settings and halfvec doens't.
thanks again for the quick reply and all the help! @jkatz @ankane
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #821: avg_width for Vector is 18 and for HalfVec is 1544
@ 2025-04-15 21:48 "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2025-04-15 21:48 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
When a column is stored out of line, there's a rough [adjustment](https://github.com/pgvector/pgvector/blob/v0.8.0/src/hnsw.c#L202-L211) to favor the index, which is likely what's happening in the vector case.
Both plans execute very fast for this query, but for more control, check out the [pg_hint_plan extension](https://github.com/ossc-db/pg_hint_plan). Also, the random page cost being off (like the default is when using an SSD) can contribute to the planner making suboptimal decisions.
^ permalink raw [nested|flat] 7+ messages in thread
end of thread, other threads:[~2025-04-15 21:48 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-04-14 11:39 [pgvector/pgvector] issue #821: avg_width for Vector is 18 and for HalfVec is 1544 "idobenamram (@idobenamram)" <noreply+idobenamram@github.com>
2025-04-14 14:16 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2025-04-14 14:31 ` "idobenamram (@idobenamram)" <noreply+idobenamram@github.com>
2025-04-14 18:46 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-04-14 18:46 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-04-15 08:04 ` "idobenamram (@idobenamram)" <noreply+idobenamram@github.com>
2025-04-15 21:48 ` "ankane (@ankane)" <noreply+ankane@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