Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs(ai): vector indexes first draft

Greg Richardson9401c03ef462
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
index 1176430f692..929454252e5 100644
--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
@@ -870,7 +870,10 @@ export const ai: NavMenuConstant = {
{
name: 'Guides',
url: undefined,
- items: [{ name: 'Creating collections', url: '/guides/ai/creating-collections' }],
+ items: [
+ { name: 'Creating collections', url: '/guides/ai/creating-collections' },
+ { name: 'Managing indexes', url: '/guides/ai/managing-indexes' },
+ ],
},
{
name: 'Examples',
diff --git a/apps/docs/pages/guides/ai/managing-indexes.mdx b/apps/docs/pages/guides/ai/managing-indexes.mdx
new file mode 100644
index 00000000000..5bc08ccce93
--- /dev/null
+++ b/apps/docs/pages/guides/ai/managing-indexes.mdx
@@ -0,0 +1,68 @@
+import Layout from '~/layouts/DefaultGuideLayout'
+
+export const meta = {
+ id: 'ai-managing-indexes',
+ title: 'Managing indexes',
+ description: 'Understanding vector indexes',
+ sidebar_label: 'Managing indexes',
+}
+
+Once your vector table starts to grow, you will likely want to add an index to speed up queries. Without indexes, you'll be performing a sequential scan which can be a resource-intensive operation when you have many records.
+
+## IVFFlat indexes
+
+Today `pgvector` indexes use an algorithm called IVFFlat. IVF stands for 'inverted file indexes'. It works by clustering your vectors in order to reduce the similarity search scope. Rather than comparing a vector to every other vector, the vector is only compared against vectors within the same cell cluster (or nearby clusters, depending on your configuration).
+
+### Inverted lists (cell clusters)
+
+When you create the index, you choose the number of inverted lists (cell clusters). Increase this number to speed up queries, but at the expense of recall.
+
+For every query, you can set the number of probes (1 by default). The number of probes corresponds to the number of nearby cells to probe for a match. Increase this for better recall at the expense of speed.
+
+### Approximate nearest neighbor
+
+One important note with IVF indexes is that nearest neighbor search is approximate, since exact search on high dimensional data can't be indexed efficiently. This means that similarity results will change (slightly) after you add an index (trading recall for speed).
+
+## Distance operators
+
+The type of index required depends on the distance operator you are using. `pgvector` includes 3 distance operators:
+
+| Operator | Description | [**Operator class**](https://www.postgresql.org/docs/current/sql-createopclass.html) |
+| -------- | ---------------------- | ------------------------------------------------------------------------------------ |
+| `<->` | Euclidean distance | `vector_l2_ops` |
+| `<#>` | negative inner product | `vector_ip_ops` |
+| `<=>` | cosine distance | `vector_cosine_ops` |
+
+Use the following SQL commands can create an index for the operator(s) used in your queries.
+
+### Euclidean L2 distance (`vector_l2_ops`)
+
+```sql
+create index on items using ivfflat (column_name vector_l2_ops) with (lists = 100);
+```
+
+### Inner product (`vector_ip_ops`)
+
+```sql
+create index on items using ivfflat (column_name vector_ip_ops) with (lists = 100);
+```
+
+### Cosine distance (`vector_cosine_ops`)
+
+```sql
+create index on items using ivfflat (column_name vector_cosine_ops) with (lists = 100);
+```
+
+Currently vectors with up to 2,000 dimensions can be indexed.
+
+## When should you add indexes?
+
+`pgvector` recommends adding indexes only after the table has sufficient data, so that the internal IVFFlat cell clusters are based on your data's distribution. Anytime the distribution changes significantly, consider recreating indexes.
+
+## Resources
+
+You can read more about indexing on `pgvector`'s GitHub page [here](https://github.com/pgvector/pgvector#indexing).
+
+export const Page = ({ children }) => <Layout meta={meta} children={children} />
+
+export default Page