Vectors and index precisions

更新时间:
复制 MD 格式

PolarDB for PostgreSQL supports multiple vector types and index precision strategies. Use this reference to select the right combination based on your storage budget, dimension count, and recall requirements.

Half-precision vectors

The halfvec type cuts storage by 50% compared to standard single-precision vectors (vector). This is useful for large embedding datasets where storage efficiency matters more than maximum precision.

CREATE TABLE items (id bigserial PRIMARY KEY, embedding halfvec(3));

Binary vectors

The bit type offers the smallest storage footprint and fastest distance computations.

CREATE TABLE items_bit (id bigserial PRIMARY KEY, embedding bit(3));
INSERT INTO items_bit (embedding) VALUES ('000'), ('111');

Find nearest neighbors using the Hamming distance operator (<~>):

SELECT * FROM items_bit ORDER BY embedding <~> '101' LIMIT 5;

Alternatively, use the Jaccard distance operator (<%>) for set-similarity-based retrieval.

Sparse vectors

The sparsevec type stores only non-zero elements. Specify values in the {index1:value1,index2:value2}/dimension format. Indexes start at 1, consistent with SQL arrays.

CREATE TABLE items_spa (id bigserial PRIMARY KEY, embedding sparsevec(5));
INSERT INTO items_spa (embedding) VALUES ('{1:1,3:2,5:3}/5'), ('{1:4,3:5,5:6}/5');

Find nearest neighbors using the L2 distance operator (<->):

SELECT * FROM items_spa ORDER BY embedding <-> '{1:3,3:1,5:2}/5' LIMIT 5;

Half-precision indexing

Index a standard vector column at half precision to produce a smaller HNSW index. Cast to halfvec in both the index definition and the query.

CREATE INDEX ON items USING hnsw ((embedding::halfvec(3)) halfvec_l2_ops);

Query using the same cast:

SELECT * FROM items ORDER BY embedding::halfvec(3) <-> '[1,2,3]' LIMIT 5;

Binary quantization

Build the binary quantization index using expression indexing:

CREATE INDEX ON items USING hnsw ((binary_quantize(embedding)::bit(3)) bit_hamming_ops);

Retrieve the top candidates by Hamming distance:

SELECT * FROM items
ORDER BY binary_quantize(embedding)::bit(3) <~> binary_quantize('[1,-2,3]'::halfvec)
LIMIT 5;

Re-rank using the original vectors to improve recall. The inner query retrieves 20 candidates; the outer query re-scores by cosine distance (<=>) and returns the top 5:

SELECT * FROM (
    SELECT * FROM items
    ORDER BY binary_quantize(embedding)::bit(3) <~> binary_quantize('[1,-2,3]'::halfvec)
    LIMIT 20
) AS foo
ORDER BY embedding <=> '[1,-2,3]'
LIMIT 5;