Turso and libSQL enable vector search capability without an extension.
How it works
- Create a table with one or more vector columns (e.g.
FLOAT32) - Provide vector values in binary format or convert text representation to binary using the appropriate conversion function (e.g.
vector32(...)) - Calculate vector similarity between vectors in the table or from the query itself using dedicated vector functions (e.g.
vector_distance_cos) - Create a special vector index to speed up nearest neighbors queries (use the
libsql_vector_idx(column)expression in theCREATE INDEXstatement to create vector index) - Query the index with the special
vector_top_k(idx_name, q_vector, k)table-valued function
Vectors
Types
LibSQL uses the native SQLite BLOB storage class for vector columns. To align with SQLite affinity rules, all type names have two alternatives: one that is easy to type and another with a _BLOB suffix that is consistent with affinity rules.
The table below lists six vector types currently supported by LibSQL. Types are listed from more precise and storage-heavy to more compact but less precise alternatives (the number of dimensions in vector is used to estimate storage requirements for a single vector).
| Type name | Storage (bytes) | Description |
|---|---|---|
FLOAT64 | F64_BLOB | Implementation of IEEE 754 double precision format for 64-bit floating point numbers | |
FLOAT32 | F32_BLOB | Implementation of IEEE 754 single precision format for 32-bit floating point numbers | |
FLOAT16 | F16_BLOB | Implementation of IEEE 754-2008 half precision format for 16-bit floating point numbers | |
FLOATB16 | FB16_BLOB | Implementation of bfloat16 format for 16-bit floating point numbers | |
FLOAT8 | F8_BLOB | LibSQL specific implementation which compresses each vector component to single u8 byte b and reconstruct value from it using simple transformation: | |
FLOAT1BIT | F1BIT_BLOB | LibSQL-specific implementation which compresses each vector component down to 1-bit and packs multiple components into a single machine word, achieving a very compact representation |
Functions
To work with vectors, LibSQL provides several functions that operate in the vector domain. Each function understands vectors in binary format aligned with the six types described above or in text format as a single JSON array of numbers. Currently, LibSQL supports the following functions:
| Function name | Description |
|---|---|
vector64 | vector32 | vector16 | vectorb16 | vector8 | vector1bit | Conversion function which accepts a valid vector and converts it to the corresponding target type |
vector | Alias for vector32 conversion function |
vector_extract | Extraction function which accepts valid vector and return its text representation |
vector_distance_cos | Cosine distance (1 - cosine similarity) function which operates over vector of same type with same dimensionality |
vector_distance_l2 | Euclidean distance function which operates over vector of same type with same dimensionality |
Vectors usage
1
2
3
Understanding Distance Results
The vector_distance_cos function calculates the cosine distance, which is defined as:
- Cosine Distance = 1 — Cosine Similarity
The cosine distance ranges from 0 to 2, where:
- A distance close to 0 indicates that the vectors are nearly identical or exactly matching.
- A distance close to 1 indicates that the vectors are orthogonal (perpendicular).
- A distance close to 2 indicates that the vectors are pointing in opposite directions.
Vector Limitations
- Euclidean distance is not supported for 1-bit
FLOAT1BITvectors - LibSQL can only operate on vectors with no more than 65536 dimensions
Indexing
Nearest neighbors (NN) queries are popular for various AI-powered applications (RAG uses NN queries to extract relevant information, and recommendation engines can suggest items based on embedding similarity). LibSQL implements DiskANN algorithm in order to speed up approximate nearest neighbors queries for tables with vector columns.
Vector Index
LibSQL introduces a custom index type that helps speed up nearest neighbors queries against a fixed distance function (cosine similarity by default).
From a syntax perspective, the vector index differs from ordinary application-defined B-Tree indices in that it must wrap the vector column into a libsql_vector_idx marker function like this
The vector index is fully integrated into the LibSQL core, so it inherits all operations and most features from ordinary indices:
- An index created for a table with existing data will be automatically populated with this data
- All updates to the base table will be automatically reflected in the index
- You can rebuild index from scratch using
REINDEX movies_idxcommand - You can drop index with
DROP INDEX movies_idxcommand - You can create partial vector index with a custom filtering rule:
Query
At the moment vector index must be queried explicitly with special vector_top_k(idx_name, q_vector, k) table-valued function. The function accepts index name, query vector and amount of neighbors to return. This function searches for k approximate nearest neighbors and returns ROWID of these rows or PRIMARY KEY if base index does not have ROWID.
In order for table-valued function to work query vector must have the same vector type and dimensionality.
Settings
LibSQL vector index optionally can accept settings which must be specified as variadic parameters of the libsql_vector_idx function as strings in the format key=value:
At the moment LibSQL supports the following settings:
| Setting key | Value type | Description |
|---|---|---|
metric | cosine | l2 | Which distance function to use for building the index. Default: cosine |
max_neighbors | positive integer | How many neighbors to store for every node in the DiskANN graph. The lower the setting — the less storage index will use in exchange to search precision. Default: where — dimensionality of vector column |
compress_neighbors | float1bit|float8|float16|floatb16|float32 | Which vector type must be used to store neighbors for every node in the DiskANN graph. The more compact vector type is used for neighbors — the less storage index will use in exchange to search precision. Default: no compression (neighbors has same type as base table) |
alpha | positive float | “Density” parameter of general sparse neighborhood graph build during DiskANN algorithm. The lower parameter — the more sparse is DiskANN graph which can speed up query speed in exchange to lower search precision. Default: 1.2 |
search_l | positive integer | Setting which limits the amount of neighbors visited during vector search. The lower the setting — the faster will be search query in exchange to search precision. Default: 200 |
insert_l | positive integer | Setting which limits the amount of neighbors visited during vector insert. The lower the setting — the faster will be insert query in exchange to DiskANN graph navigability properties. Default: 70 |
Index usage
1
2
3
4
Index limitations
- Vector index works only for tables with
ROWIDor with singularPRIMARY KEY. CompositePRIMARY KEYwithoutROWIDis not supported