From: wmwsagara (@wmwsagara) <noreply+wmwsagara@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #461: Increase max vectors dimension limit for index
Date: Sat, 30 Mar 2024 12:00:44 +0000
Message-ID: <gh-pgvector-pgvector-461-c2028027858@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-461@github.com>
References: <gh-pgvector-pgvector-461@github.com>
**TL;DR:**
This is a proposal for pgvector to support lot more than 2000 fp32 vector dimensions without modifying PostgreSQL or lower the accuracy of vector embeddings.
It is not possible to support more than 2000 vector dimensions! See @jkatz first post: "the primary limitation is due to the default size of the PostgreSQL page (8KB), which is not adjustable ...".
8K, 8192/4 = 2048.
floating point 32bit (fp32) or float is 4 bytes.
Max dimensions: 2000
Allocation for Overhead: 48
There are 3 solutions available:
1. Modify PostgreSQL
Ask the PostgreSQL developers to offer an option configure PostgreSQL page size at PostgreSQL server start time. This has the possible negative consequences for PostgreSQL IO pipeline. It's may be too risky and may require extensive testing. Therefore, to the most probability, PostgreSQL developers may turn down this request, or may take quite long to get developed.
2. Go for lower accuracy
fp16:
8000/2 = 4000
Max dimensions: 4000
Still 4096 dimensions cannot support!
Require to go even lower accuracy to support 4096. How about 8K dimensions? It might hit 8K by next year.
There can be some application domains lowering accuracy may be bearable. There can be domains, cannot risk to lower the accuracy.
**3. Proposal - split the document table**
Text part is in the document table, embeddings are in a separate embedding table. There may be one or more records in the embedding table for a one document, each embedding record support up to max 2000 dimensions.
The document table record layout:
document_reference - string
content - text
The embedding table record layout:
document_reference - string
seq_no - number
embedding [] - array
Indexed by: document_reference+seq_no
To support 3072 dimensions, you need two (2) records in the embedding table.
To support 4096 dimensions, you need three (3) records in the embedding table.
When build the index (eg. HNSW), read all embedding records for a document and reconstruct the original dimensions required (eg. 4096) for the document just by append them sequentially. Rest are all in memory operations.
To build the index, pgvector has to read only the embedding table in document_reference+seq_no order.
The document_reference can be a UUID or any other string.
Advantageous:
a) No need to modify PostgreSQL.
b) Fully ACID compliant.
c) No maximum limit for number of fp32 vector dimensions can be supported.
d) Straight away can be implemented without depending on any third party.
disadvantageous:
a) Require to restructure pgvector.
I think, it is possible to offer a hybrid version. That is, no change to user's code up to 2000 dimensions. That is, text and embedding can reside in one table record.
But if the user require more than 2000 dimensions, all embeddings are read from embedding table.
Best regards
Sagara