Vector Similarity Search in DuckDB

https://duckdb.org/2024/05/03/vector-similarity-search-vss.html

Author Avatar

TL;DR: This blog post shows a preview of DuckDB's new vss extension, which introduces support for HNSW (Hierarchical Navigable Small Worlds) indexes to accelerate vector similarity search.

In DuckDB v0.10.0, we introduced the ARRAY data type, which stores fixed-sized lists, to complement the existing variable-size LIST data type.

The initial motivation for adding this data type was to provide optimized operations for lists that can utilize the positional semantics of their child elements and avoid branching as all lists have the same length. Think e.g., the sort of array manipulations you'd do in NumPy: stacking, shifting, multiplying – you name it. Additionally, we wanted to improve our interoperability with Apache Arrow, as previously Arrow's fixed-size list types would be converted to regular variable-size lists when ingested into DuckDB, losing some type information.

However, as the hype for vector embeddings and semantic similarity search was growing, we also snuck in a couple of distance metric functions for this new ARRAY type: array_distance, array_negative_inner_product and array_cosine_distance

If you're one of today's lucky 10,000 and haven't heard of word embeddings or vector search, the short version is that it's a technique used to represent documents, images, entities – data as high-dimensional vectors and then search for similar vectors in a vector space, using some sort of mathematical "distance" expression to measure similarity. This is used in a wide range of applications, from natural language processing to recommendation systems and image recognition, and has recently seen a surge in popularity due to the advent of generative AI and availability of pre-trained models.

This got the community really excited! While we (DuckDB Labs) initially went on record saying that we would not be adding a vector similarity search index to DuckDB as we deemed it to be too far out of scope, we were very interested in supporting custom indexes through extensions in general. Shoot, I've been personally nagging on about wanting to plug-in an "R-Tree" index since the inception of DuckDBs spatial extension! So when one of our client projects evolved into creating a proof-of-concept custom "HNSW" index extension, we said that we'd give it a shot. And… well, one thing led to another.

Fast forward to now and we're happy to announce the availability of the vss vector similarity search extension for DuckDB! While some may say we're late to the vector search party, we'd like to think the party is just getting started!

Alright, so what's in vss?

The Vector Similarity Search (VSS) Extension

On the surface, vss seems like a comparatively small DuckDB extension. It does not provide any new data types, scalar functions or copy functions, but rather a single new index type: HNSW (Hierarchical Navigable Small Worlds), which is a graph-based index structure that is particularly well-suited for high-dimensional vector similarity search.

-- Create a table with an array column
CREATE TABLE embeddings (vec FLOAT[3]);
-- Create an HNSW index on the column
CREATE INDEX idx ON embeddings USING HNSW (vec);

This index type can't be used to enforce constraints or uniqueness like the built-in ART index, and can't be used to speed up joins or index regular columns either. Instead, the HNSW index is only applicable to columns of the ARRAY type containing FLOAT elements and will only be used to accelerate queries calculating the "distance" between a constant FLOAT ARRAY and the FLOAT ARRAY's in the indexed column, ordered by the resulting distance and returning the top-n results. That is, queries of the form:

SELECT *
FROM embeddings
ORDER BY array_distance(vec, [1, 2, 3]::FLOAT[3])
LIMIT 3;

will have their logical plan optimized to become a projection over a new HNSW index scan operator, removing the limit and sort altogether. We can verify this by checking the EXPLAIN output:

EXPLAIN
SELECT *
FROM embeddings
ORDER BY array_distance(vec, [1, 2, 3]::FLOAT[3])
LIMIT 3;
┌───────────────────────────┐
│         PROJECTION        │
│   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─   │
│             #0            │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│         PROJECTION        │
│   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─   │
│            vec            │
│array_distance(vec, [1.0, 2│
│         .0, 3.0])         │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│      HNSW_INDEX_SCAN      │
│   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─   │
│   t1 (HNSW INDEX SCAN :   │
│            idx)           │
│   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─   │
│            vec            │
│   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─   │
│           EC: 3           │
└───────────────────────────┘

You can pass the HNSW index creation statement a metric parameter to decide what kind of distance metric to use. The supported metrics are l2sq, cosine and inner_product, matching the three built-in distance functions: array_distance, array_cosine_distance and array_negative_inner_product. The default is l2sq, which uses Euclidean distance (array_distance):

CREATE INDEX l2sq_idx ON embeddings USING HNSW (vec)
WITH (metric = 'l2sq');

To use cosine distance (array_cosine_distance):

CREATE INDEX cos_idx ON embeddings USING HNSW (vec)
WITH (metric = 'cosine');

To use inner product (array_negative_inner_product):

CREATE INDEX ip_idx ON embeddings USING HNSW (vec)
WITH (metric = 'ip');

Implementation

The vss extension is based on the usearch library, which provides a flexible C++ implementation of the HNSW index data structure boasting very impressive performance benchmarks. While we currently only use a subset of all the functionality and tuning options provided by usearch, we're excited to explore how we can leverage more of its features in the future. So far we're mostly happy that it aligns so nicely with DuckDB's development ethos. Much like DuckDB itself, usearch is written in portable C++11 with no external dependencies and released under a permissive license, making it super smooth to integrate into our extension build and distribution pipeline.

Limitations

The big limitation as of now is that the HNSW index can only be created in in-memory databases, unless the SET hnsw_enable_experimental_persistence = ⟨bool⟩ configuration parameter is set to true. If this parameter is not set, any attempt to create an HNSW index in a disk-backed database will result in an error message, but if the parameter is set, the index will not only be created in memory, but also persisted to disk as part of the DuckDB database file during checkpointing. After restarting or loading a database file with a persisted HNSW index, the index will be lazily loaded back into memory whenever the associated table is first accessed, which is significantly faster than having to re-create the index from scratch.

The reasoning for locking this feature behind an experimental flag is that we still have some known issues related to persistence of custom indexes that we want to address before enabling it by default. In particular, WAL recovery is not yet properly implemented for custom indexes, meaning that if a crash occurs or the database is shut down unexpectedly while there are uncommited changes to a HNSW-indexed table, you can end up with data loss or corruption of the index. While it is technically possible to recover from a unexpected shutdown manually by first starting DuckDB separately, loading the vss extension and then ATTACHing the database file, which ensures that the HNSW index functionality is available during WAL-playback, you should not rely on this for production workloads.

We're actively working on addressing this and other issues related to index persistence, which will hopefully make it into DuckDB v0.10.3, but for now we recommend using the HNSW index in in-memory databases only.

At runtime however, much like the ART the HNSW index must be able to fit into RAM in its entirety, and the memory allocated by the HNSW at runtime is allocated "outside" of the DuckDB memory management system, meaning that it wont respect DuckDB's memory_limit configuration parameter.

Another current limitation with the HNSW index so far are that it only supports the FLOAT (a 32-bit, single-precision floating point) type for the array elements and only distance metrics corresponding to the three built in distance functions, array_distance, array_negative_inner_product and array_cosine_distance. But this is also something we're looking to expand upon in the near future as it is much less of a technical limitation and more of a "we haven't gotten around to it yet" limitation.

Conclusion

The vss extension for DuckDB is a new extension that adds support for creating HNSW indexes on fixed-size list columns in DuckDB, accelerating vector similarity search queries. The extension can currently be installed on DuckDB v0.10.2 on all supported platforms (including WASM!) by running INSTALL vss; LOAD vss. The vss extension treads new ground for DuckDB extensions by providing a custom index type and we're excited to refine and expand on this functionality going forward.

While we're still working on addressing some of the limitations above, particularly those related to persistence (and performance), we still really want to share this early version the vss extension as we believe this will open up a lot of cool opportunities for the community. So make sure to check out the vss extension documentation for more information on how to work with this extension!

This work was made possible by the sponsorship of a DuckDB Labs customer! If you are interested in similar work for specific capabilities, please reach out to DuckDB Labs. Alternatively, we're happy to welcome contributors! Please reach out to the DuckDB Labs team over on Discord or on the vss extension GitHub repository to keep up with the latest developments.

{
"by": "vgt",
"descendants": 0,
"id": 40249086,
"score": 21,
"time": 1714751591,
"title": "Vector Similarity Search in DuckDB",
"type": "story",
"url": "https://duckdb.org/2024/05/03/vector-similarity-search-vss.html"
}
{
"author": "Max Gabrielsson",
"date": "2024-11-21T10:03:40.000Z",
"description": "This blog post shows a preview of DuckDB’s new vss extension, which introduces support for HNSW (Hierarchical Navigable Small Worlds) indexes to accelerate vector similarity search.",
"image": "https://duckdb.org/images/blog/thumbs/vss.png",
"logo": "https://logo.clearbit.com/duckdb.org",
"publisher": "DuckDB",
"title": "Vector Similarity Search in DuckDB",
"url": "https://duckdb.org/2024/05/03/vector-similarity-search-vss.html"
}
{
"url": "https://duckdb.org/2024/05/03/vector-similarity-search-vss.html",
"title": "Vector Similarity Search in DuckDB",
"description": "This blog post shows a preview of DuckDB's new vss extension, which introduces support for HNSW (Hierarchical Navigable Small Worlds) indexes to accelerate vector similarity search.",
"links": [
"https://duckdb.org/2024/05/03/vector-similarity-search-vss.html"
],
"image": "https://duckdb.org/images/blog/thumbs/vss.png",
"content": "<div>\n\t\t\t\t\t\t\t\t\t\t<div>\n\t\t\t\t\t\t\t\t\t\t\t<p><img src=\"https://duckdb.org/images/blog/authors/max_gabrielsson.jpg\" alt=\"Author Avatar\" />\n\t\t\t\t\t\t\t\t\t\t\t</p>\n\t\t\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t\t\t<p><em>TL;DR: This blog post shows a preview of DuckDB's new <a target=\"_blank\" href=\"https://duckdb.org/docs/extensions/vss\"><code>vss</code> extension</a>, which introduces support for HNSW (Hierarchical Navigable Small Worlds) indexes to accelerate vector similarity search.</em></p>\n\t\t\t\t\t\t\t\t\t<p>In DuckDB v0.10.0, we introduced the <a target=\"_blank\" href=\"https://duckdb.org/docs/sql/data_types/array.html\"><code>ARRAY</code> data type</a>, which stores fixed-sized lists, to complement the existing variable-size <a target=\"_blank\" href=\"https://duckdb.org/docs/sql/data_types/list.html\"><code>LIST</code> data type</a>.</p>\n<p>The initial motivation for adding this data type was to provide optimized operations for lists that can utilize the positional semantics of their child elements and avoid branching as all lists have the same length. Think e.g., the sort of array manipulations you'd do in NumPy: stacking, shifting, multiplying – you name it. Additionally, we wanted to improve our interoperability with Apache Arrow, as previously Arrow's fixed-size list types would be converted to regular variable-size lists when ingested into DuckDB, losing some type information.</p>\n<p>However, as the hype for <strong>vector embeddings</strong> and <strong>semantic similarity search</strong> was growing, we also snuck in a couple of distance metric functions for this new <code>ARRAY</code> type:\n<a target=\"_blank\" href=\"https://duckdb.org/docs/sql/functions/array.html#array_distancearray1-array2\"><code>array_distance</code></a>,\n<a target=\"_blank\" href=\"https://duckdb.org/docs/sql/functions/array.html#array_negative_inner_productarray1-array2\"><code>array_negative_inner_product</code></a> and\n<a target=\"_blank\" href=\"https://duckdb.org/docs/sql/functions/array.html#array_cosine_distancearray1-array2\"><code>array_cosine_distance</code></a></p>\n<blockquote>\n <p>If you're one of today's <a target=\"_blank\" href=\"https://xkcd.com/1053/\">lucky 10,000</a> and haven't heard of word embeddings or vector search, the short version is that it's a technique used to represent documents, images, entities – <em>data</em> as high-dimensional <em>vectors</em> and then search for <em>similar</em> vectors in a vector space, using some sort of mathematical \"distance\" expression to measure similarity. This is used in a wide range of applications, from natural language processing to recommendation systems and image recognition, and has recently seen a surge in popularity due to the advent of generative AI and availability of pre-trained models.</p>\n</blockquote>\n<p>This got the community really excited! While we (DuckDB Labs) initially went on record saying that we would not be adding a vector similarity search index to DuckDB as we deemed it to be too far out of scope, we were very interested in supporting custom indexes through extensions in general. Shoot, I've been <em>personally</em> nagging on about wanting to plug-in an \"R-Tree\" index since the inception of DuckDBs <a target=\"_blank\" href=\"https://duckdb.org/docs/extensions/spatial/overview.html\">spatial extension</a>! So when one of our client projects evolved into creating a proof-of-concept custom \"HNSW\" index extension, we said that we'd give it a shot. And… well, one thing led to another.</p>\n<p>Fast forward to now and we're happy to announce the availability of the <code>vss</code> vector similarity search extension for DuckDB! While some may say we're late to the vector search party, <a target=\"_blank\" href=\"https://www.gartner.com/en/newsroom/press-releases/2023-10-11-gartner-says-more-than-80-percent-of-enterprises-will-have-used-generative-ai-apis-or-deployed-generative-ai-enabled-applications-by-2026\">we'd like to think the party is just getting started!</a></p>\n<p>Alright, so what's in <code>vss</code>?</p>\n <h2 id=\"the-vector-similarity-search-vss-extension\">\n <a target=\"_blank\" href=\"https://duckdb.org/2024/05/03/vector-similarity-search-vss.html#the-vector-similarity-search-vss-extension\">The Vector Similarity Search (VSS) Extension</a>\n </h2>\n<p>On the surface, <code>vss</code> seems like a comparatively small DuckDB extension. It does not provide any new data types, scalar functions or copy functions, but rather a single new index type: <code>HNSW</code> (<a target=\"_blank\" href=\"https://en.wikipedia.org/wiki/Hierarchical_Navigable_Small_World_graphs\">Hierarchical Navigable Small Worlds</a>), which is a graph-based index structure that is particularly well-suited for high-dimensional vector similarity search.</p>\n<div><pre><code><span>-- Create a table with an array column</span>\n<span>CREATE</span> <span>TABLE</span> <span>embeddings</span> <span>(</span><span>vec</span> <span>FLOAT</span><span>[</span><span>3</span><span>]);</span>\n<span>-- Create an HNSW index on the column</span>\n<span>CREATE</span> <span>INDEX</span> <span>idx</span> <span>ON</span> <span>embeddings</span> <span>USING</span> <span>HNSW</span> <span>(</span><span>vec</span><span>);</span>\n</code></pre></div>\n<p>This index type can't be used to enforce constraints or uniqueness like the built-in <a target=\"_blank\" href=\"https://duckdb.org/docs/sql/indexes.html\"><code>ART</code> index</a>, and can't be used to speed up joins or index regular columns either. Instead, the <code>HNSW</code> index is only applicable to columns of the <code>ARRAY</code> type containing <code>FLOAT</code> elements and will only be used to accelerate queries calculating the \"distance\" between a constant <code>FLOAT</code> <code>ARRAY</code> and the <code>FLOAT</code> <code>ARRAY</code>'s in the indexed column, ordered by the resulting distance and returning the top-n results. That is, queries of the form:</p>\n<div><pre><code><span>SELECT</span> <span>*</span>\n<span>FROM</span> <span>embeddings</span>\n<span>ORDER</span> <span>BY</span> <span>array_distance</span><span>(</span><span>vec</span><span>,</span> <span>[</span><span>1</span><span>,</span> <span>2</span><span>,</span> <span>3</span><span>]::</span><span>FLOAT</span><span>[</span><span>3</span><span>])</span>\n<span>LIMIT</span> <span>3</span><span>;</span>\n</code></pre></div>\n<p>will have their logical plan optimized to become a projection over a new <code>HNSW</code> index scan operator, removing the limit and sort altogether. We can verify this by checking the <code>EXPLAIN</code> output:</p>\n<div><pre><code><span>EXPLAIN</span>\n<span>SELECT</span> <span>*</span>\n<span>FROM</span> <span>embeddings</span>\n<span>ORDER</span> <span>BY</span> <span>array_distance</span><span>(</span><span>vec</span><span>,</span> <span>[</span><span>1</span><span>,</span> <span>2</span><span>,</span> <span>3</span><span>]::</span><span>FLOAT</span><span>[</span><span>3</span><span>])</span>\n<span>LIMIT</span> <span>3</span><span>;</span>\n</code></pre></div>\n<div><pre><code>┌───────────────────────────┐\n│ PROJECTION │\n│ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │\n│ #0 │\n└─────────────┬─────────────┘\n┌─────────────┴─────────────┐\n│ PROJECTION │\n│ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │\n│ vec │\n│array_distance(vec, [1.0, 2│\n│ .0, 3.0]) │\n└─────────────┬─────────────┘\n┌─────────────┴─────────────┐\n│ HNSW_INDEX_SCAN │\n│ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │\n│ t1 (HNSW INDEX SCAN : │\n│ idx) │\n│ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │\n│ vec │\n│ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │\n│ EC: 3 │\n└───────────────────────────┘\n</code></pre></div>\n<p>You can pass the <code>HNSW</code> index creation statement a <code>metric</code> parameter to decide what kind of distance metric to use. The supported metrics are <code>l2sq</code>, <code>cosine</code> and <code>inner_product</code>, matching the three built-in distance functions: <code>array_distance</code>, <code>array_cosine_distance</code> and <code>array_negative_inner_product</code>.\nThe default is <code>l2sq</code>, which uses Euclidean distance (<code>array_distance</code>):</p>\n<div><pre><code><span>CREATE</span> <span>INDEX</span> <span>l2sq_idx</span> <span>ON</span> <span>embeddings</span> <span>USING</span> <span>HNSW</span> <span>(</span><span>vec</span><span>)</span>\n<span>WITH</span> <span>(</span><span>metric</span> <span>=</span> <span>'l2sq'</span><span>);</span>\n</code></pre></div>\n<p>To use cosine distance (<code>array_cosine_distance</code>):</p>\n<div><pre><code><span>CREATE</span> <span>INDEX</span> <span>cos_idx</span> <span>ON</span> <span>embeddings</span> <span>USING</span> <span>HNSW</span> <span>(</span><span>vec</span><span>)</span>\n<span>WITH</span> <span>(</span><span>metric</span> <span>=</span> <span>'cosine'</span><span>);</span>\n</code></pre></div>\n<p>To use inner product (<code>array_negative_inner_product</code>):</p>\n<div><pre><code><span>CREATE</span> <span>INDEX</span> <span>ip_idx</span> <span>ON</span> <span>embeddings</span> <span>USING</span> <span>HNSW</span> <span>(</span><span>vec</span><span>)</span>\n<span>WITH</span> <span>(</span><span>metric</span> <span>=</span> <span>'ip'</span><span>);</span>\n</code></pre></div>\n <h2 id=\"implementation\">\n <a target=\"_blank\" href=\"https://duckdb.org/2024/05/03/vector-similarity-search-vss.html#implementation\">Implementation</a>\n </h2>\n<p>The <code>vss</code> extension is based on the <a target=\"_blank\" href=\"https://github.com/unum-cloud/usearch\"><code>usearch</code></a> library, which provides a flexible C++ implementation of the HNSW index data structure boasting very impressive performance benchmarks. While we currently only use a subset of all the functionality and tuning options provided by <code>usearch</code>, we're excited to explore how we can leverage more of its features in the future. So far we're mostly happy that it aligns so nicely with DuckDB's development ethos. Much like DuckDB itself, <code>usearch</code> is written in portable C++11 with no external dependencies and released under a permissive license, making it super smooth to integrate into our extension build and distribution pipeline.</p>\n <h2 id=\"limitations\">\n <a target=\"_blank\" href=\"https://duckdb.org/2024/05/03/vector-similarity-search-vss.html#limitations\">Limitations</a>\n </h2>\n<p>The big limitation as of now is that the <code>HNSW</code> index can only be created in in-memory databases, unless the <code>SET hnsw_enable_experimental_persistence = ⟨bool⟩</code> configuration parameter is set to <code>true</code>. If this parameter is not set, any attempt to create an <code>HNSW</code> index in a disk-backed database will result in an error message, but if the parameter is set, the index will not only be created in memory, but also persisted to disk as part of the DuckDB database file during checkpointing. After restarting or loading a database file with a persisted <code>HNSW</code> index, the index will be lazily loaded back into memory whenever the associated table is first accessed, which is significantly faster than having to re-create the index from scratch.</p>\n<p>The reasoning for locking this feature behind an experimental flag is that we still have some known issues related to persistence of custom indexes that we want to address before enabling it by default. In particular, WAL recovery is not yet properly implemented for custom indexes, meaning that if a crash occurs or the database is shut down unexpectedly while there are uncommited changes to a <code>HNSW</code>-indexed table, you can end up with data loss or corruption of the index. While it is technically possible to recover from a unexpected shutdown manually by first starting DuckDB separately, loading the <code>vss</code> extension and then <code>ATTACH</code>ing the database file, which ensures that the <code>HNSW</code> index functionality is available during WAL-playback, you should not rely on this for production workloads.</p>\n<p>We're actively working on addressing this and other issues related to index persistence, which will hopefully make it into <a target=\"_blank\" href=\"https://duckdb.org/docs/dev/release_calendar.html#upcoming-releases\">DuckDB v0.10.3</a>, but for now we recommend using the <code>HNSW</code> index in in-memory databases only.</p>\n<p>At runtime however, much like the <code>ART</code> the <code>HNSW</code> index must be able to fit into RAM in its entirety, and the memory allocated by the <code>HNSW</code> at runtime is allocated \"outside\" of the DuckDB memory management system, meaning that it wont respect DuckDB's <code>memory_limit</code> configuration parameter.</p>\n<p>Another current limitation with the <code>HNSW</code> index so far are that it only supports the <code>FLOAT</code> (a 32-bit, single-precision floating point) type for the array elements and only distance metrics corresponding to the three built in distance functions, <code>array_distance</code>, <code>array_negative_inner_product</code> and <code>array_cosine_distance</code>. But this is also something we're looking to expand upon in the near future as it is much less of a technical limitation and more of a \"we haven't gotten around to it yet\" limitation.</p>\n <h2 id=\"conclusion\">\n <a target=\"_blank\" href=\"https://duckdb.org/2024/05/03/vector-similarity-search-vss.html#conclusion\">Conclusion</a>\n </h2>\n<p>The <code>vss</code> extension for DuckDB is a new extension that adds support for creating HNSW indexes on fixed-size list columns in DuckDB, accelerating vector similarity search queries. The extension can currently be installed on DuckDB v0.10.2 on all supported platforms (including WASM!) by running <code>INSTALL vss; LOAD vss</code>. The <code>vss</code> extension treads new ground for DuckDB extensions by providing a custom index type and we're excited to refine and expand on this functionality going forward.</p>\n<p>While we're still working on addressing some of the limitations above, particularly those related to persistence (and performance), we still really want to share this early version the <code>vss</code> extension as we believe this will open up a lot of cool opportunities for the community. So make sure to check out the <a target=\"_blank\" href=\"https://duckdb.org/docs/extensions/vss.html\"><code>vss</code> extension documentation</a> for more information on how to work with this extension!</p>\n<p>This work was made possible by the sponsorship of a DuckDB Labs customer! If you are interested in similar work for specific capabilities, please reach out to <a target=\"_blank\" href=\"https://duckdblabs.com/\">DuckDB Labs</a>. Alternatively, we're happy to welcome contributors! Please reach out to the DuckDB Labs team over on Discord or on the <a target=\"_blank\" href=\"https://github.com/duckdb/duckdb_vss\"><code>vss</code> extension GitHub repository</a> to keep up with the latest developments.</p>\n\t\t\t\t\t\t\t\t</div>",
"author": "@Maxxen_",
"favicon": "https://duckdb.org/images/favicon/favicon.ico",
"source": "duckdb.org",
"published": "2024-05-03T00:00:00+00:00",
"ttr": 311,
"type": "article"
}