Sub topic 16
Smaller and Sparse Representations: `halfvec`, `sparsevec`, and `bit`
This chapter assumes that you know basic PostgreSQL data types, casts, indexes, and ORDER BY ... LIMIT. It also assumes that you understand embeddings and have enabled the vector extension.
A regular vector stores every component as a 32-bit floating-point number. That works well for dense embeddings, where most components contain useful values. pgvector also provides representations that reduce storage or fit different kinds of data:
| Type | Best fit | Main benefit |
|---|---|---|
| `halfvec` | Dense embeddings | Uses 16-bit values |
| `sparsevec` | Vectors with many zero values | Stores only nonzero values |
| `bit` | Binary fingerprints | Stores zeros and ones compactly |
Using `halfvec` for dense embeddings
halfvec stores values with half-precision floating-point numbers. It uses less storage than vector, which can reduce index size and memory use. The trade-off is lower numeric precision.
The type still has a fixed dimension:
CREATE TABLE product_embeddings (
id bigint PRIMARY KEY,
embedding halfvec(3) NOT NULL
);
INSERT INTO product_embeddings (id, embedding)
VALUES
(1, '[0.10, 0.20, 0.30]'),
(2, '[0.11, 0.19, 0.31]');
SELECT id, embedding <=> '[0.10, 0.20, 0.30]' AS cosine_distance
FROM product_embeddings
ORDER BY embedding <=> '[0.10, 0.20, 0.30]'
LIMIT 5;Use halfvec when tests show that reduced precision does not harm ranking quality. Compare its nearest-neighbor results with a full-precision vector before switching production data.
Using `sparsevec`
A sparsevec stores a fixed total dimension but records only nonzero elements. Its text form lists positions and values, followed by the total dimension:
CREATE TABLE keyword_vectors (
id bigint PRIMARY KEY,
embedding sparsevec(5) NOT NULL
);
INSERT INTO keyword_vectors (id, embedding)
VALUES
(1, '{1:1,3:2}/5'),
(2, '{2:1,5:4}/5');
SELECT id, embedding <-> '{1:1,3:2}/5' AS l2_distance
FROM keyword_vectors
ORDER BY embedding <-> '{1:1,3:2}/5'
LIMIT 5;Positions start at one. The declared dimension is five in this example, even though each row has only two nonzero values. sparsevec is useful for data such as large feature vectors or bag-of-words representations. It is less useful when most components are nonzero.
Using `bit` for binary vectors
A PostgreSQL bit value contains binary components. pgvector supports Hamming distance with <~> and Jaccard distance with <%>:
CREATE TABLE fingerprints (
id bigint PRIMARY KEY,
fingerprint bit(8) NOT NULL
);
INSERT INTO fingerprints (id, fingerprint)
VALUES
(1, B'10110011'),
(2, B'10110001'),
(3, B'01001100');
SELECT id, fingerprint <~> B'10110011' AS hamming_distance
FROM fingerprints
ORDER BY fingerprint <~> B'10110011'
LIMIT 5;Hamming distance counts positions that differ. Jaccard distance compares the overlap between the set bits. Choose the metric that matches how your binary fingerprints were generated.
Choosing a representation
Exercise
Which type is appropriate for a five-dimensional vector with only two nonzero values, and what does {1:1,3:2}/5 mean?
Show answer
Use sparsevec(5). The value has dimension five, with value 1 at position 1, value 2 at position 3, and zeroes elsewhere.