sparse_vector
pg_sparse_vector · C data type and functions for sparse float vectors, normalization, dot product, and cosine similarity.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100490 | sparse_vector | pg_sparse_vector | 0.1 | RAG | MIT | C |
PostgreSQL · 10, 11, 12, 13, 14, 15, 16, 17, 18
Usage
Sources:
The sparse_vector extension, packaged by the upstream project as pg_sparse_vector, stores only the nonzero positions of a float4[] vector and provides dot-product and cosine-similarity calculations. It is a compact calculation type; it does not include nearest-neighbor indexes or vector-distance operators.
Core Workflow
Values must be constructed from arrays because textual input is not implemented:
When every stored vector and query vector is normalized, use the dot product to avoid recomputing norms:
Important Objects
sparse_vector(float4[]): builds a sparse value by retaining nonzero elements and their zero-based indexes.sparse_vector_norm(float4[]): builds a value after dividing elements by the array’s Euclidean norm.dot_product(sparse_vector, sparse_vector): returns afloat4dot product over matching stored indexes.cosine_similarity(sparse_vector, sparse_vector): returns dot product divided by both computed norms.
Caveats
The representation does not store the original dense dimension; trailing zeros are lost, and vectors created from different array lengths are not rejected. Ensure dimensions in application metadata. Use one-dimensional, non-null float4[] inputs; the C implementation reads the array payload directly and does not document multidimensional or null-element handling.
An all-zero vector has zero norm, so cosine similarity can produce a non-finite result. Normalizing such a vector has no meaningful direction. Text casts such as '(0, 1)'::sparse_vector fail by design; use the constructors.
There are no comparison operators, aggregates, casts back to arrays, index operator classes, or approximate-nearest-neighbor search. Similarity queries therefore scan and calculate unless the application adds another retrieval strategy. No preload or restart is required.