mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 00:19:08 +00:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? Docs update — small structural and formatting fixes to `apps/docs/content/guides/database/extensions/pgvector.mdx`. ## What is the current behavior? **Duplicate `## Usage` heading.** The guide has `## Usage` at two places — once above "Enable the extension", and again above "Create a table to store vectors". Anchor IDs are generated from heading text, so both render as `#usage`. The table of contents shows two identical "Usage" entries, and clicking the second one scrolls to the first section. **Untagged code fence.** The iterative-search example uses a bare ``` fence, so it gets no syntax highlighting, unlike every other block on the page. Its SQL is also uppercase, while the rest of the guide (and the docs style guide) uses lowercase keywords. **Stray leading space** before `-- Example: enable the "vector" extension.` inside the enable-extension SQL block. **Typo:** the prose says "Transformer.js"; the library is Transformers.js (the code comment two lines below already spells it correctly). ## What is the new behavior? - Second `## Usage` renamed to `## Storing vectors`, which describes the two subsections it introduces ("Create a table to store vectors" and "Storing a vector / embedding"). The TOC now has unique entries and unique anchors. - Iterative-search fence tagged ` ```sql ` and its keywords lowercased. - Stray leading space removed. - "Transformer.js" → "Transformers.js". Content-only; no components, navigation, or generated output touched. ## Additional context Before renaming the heading I checked for anything linking to the old anchor: ``` grep -rn "#usage" apps/docs/content ``` No in-page or cross-file link targets `pgvector#usage` — the hits are unrelated pages and external URLs — so the rename doesn't break any links. Verified locally against the repo's own tooling: - `supa-mdx-lint` with `supa-mdx-lint.config.toml` → no errors or warnings (the new heading is sentence case, per `Rule001HeadingCase`). - Prettier 3.8.1 with `prettier.config.mjs` and `prettier-plugin-sql-cst` → already formatted, including the newly tagged `sql` block. One thing I deliberately left alone: the JS sample still imports `@xenova/transformers`, which has since been superseded by `@huggingface/transformers`. That's a behavioral change rather than a formatting one, so it felt out of scope here — happy to open a separate PR if you'd like it updated. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Nik Richers <nrichers@gmail.com>
126 lines
4.1 KiB
Plaintext
126 lines
4.1 KiB
Plaintext
---
|
|
id: 'pgvector'
|
|
title: 'pgvector: Embeddings and vector similarity'
|
|
description: 'pgvector: a Postgres extension for storing embeddings and performing vector similarity search.'
|
|
---
|
|
|
|
[pgvector](https://github.com/pgvector/pgvector/) is a Postgres extension for vector similarity search. It can also be used for storing [embeddings](/blog/openai-embeddings-postgres-vector).
|
|
|
|
<Admonition type="note">
|
|
|
|
The name of pgvector's Postgres extension is [vector](https://github.com/pgvector/pgvector/blob/258eaf58fdaff1843617ff59ea855e0768243fe9/README.md?plain=1#L64).
|
|
|
|
</Admonition>
|
|
|
|
Learn more about Supabase's [AI & Vector](/docs/guides/ai) offering.
|
|
|
|
## Concepts
|
|
|
|
### Vector similarity
|
|
|
|
Vector similarity refers to a measure of the similarity between two related items. For example, if you have a list of products, you can use vector similarity to find similar products. To do this, you need to convert each product into a "vector" of numbers, using a mathematical model. You can use a similar model for text, images, and other types of data. Once all of these vectors are stored in the database, you can use vector similarity to find similar items.
|
|
|
|
### Embeddings
|
|
|
|
This is particularly useful if you're building AI applications with large language models. You can create and store [embeddings](/docs/guides/ai/quickstarts/generate-text-embeddings) for retrieval augmented generation (RAG).
|
|
|
|
## Usage
|
|
|
|
### Enable the extension
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="dashboard"
|
|
queryGroup="database-method"
|
|
>
|
|
<TabPanel id="dashboard" label="Dashboard">
|
|
|
|
1. Go to the [Database](/dashboard/project/_/database/tables) page in the Dashboard.
|
|
2. Click on **Extensions** in the sidebar.
|
|
3. Search for "vector" and enable the extension.
|
|
|
|
</TabPanel>
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
-- Example: enable the "vector" extension.
|
|
create extension vector
|
|
with
|
|
schema extensions;
|
|
|
|
-- Example: disable the "vector" extension
|
|
drop
|
|
extension if exists vector;
|
|
```
|
|
|
|
Even though the SQL code is `create extension`, this is the equivalent of "enabling the extension".
|
|
To disable an extension, call `drop extension`.
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
## Storing vectors
|
|
|
|
### Create a table to store vectors
|
|
|
|
```sql
|
|
create table posts (
|
|
id serial primary key,
|
|
title text not null,
|
|
body text not null,
|
|
embedding extensions.vector(384)
|
|
);
|
|
```
|
|
|
|
### Storing a vector / embedding
|
|
|
|
In this example we'll generate a vector using Transformers.js, then store it in the database using the Supabase client.
|
|
|
|
```js
|
|
import { pipeline } from '@xenova/transformers'
|
|
|
|
const generateEmbedding = await pipeline('feature-extraction', 'Supabase/gte-small')
|
|
|
|
const title = 'First post!'
|
|
const body = 'Hello world!'
|
|
|
|
// Generate a vector using Transformers.js
|
|
const output = await generateEmbedding(body, {
|
|
pooling: 'mean',
|
|
normalize: true,
|
|
})
|
|
|
|
// Extract the embedding output
|
|
const embedding = Array.from(output.data)
|
|
|
|
// Store the vector in Postgres
|
|
const { data, error } = await supabase.from('posts').insert({
|
|
title,
|
|
body,
|
|
embedding,
|
|
})
|
|
```
|
|
|
|
## Specific usage cases
|
|
|
|
### Queries with filtering
|
|
|
|
If you use an IVFFlat or HNSW index and naively filter the results based on the value of another column, you may get fewer rows returned than requested.
|
|
|
|
For example, the following query may return fewer than 5 rows, even if 5 corresponding rows exist in the database. This is because the embedding index may not return 5 rows matching the filter.
|
|
|
|
```sql
|
|
select * from items where category_id = 123 order by embedding <-> '[3,1,2]' limit 5;
|
|
```
|
|
|
|
To get the exact number of requested rows, use [iterative search](https://github.com/pgvector/pgvector/?tab=readme-ov-file#iterative-index-scans) to continue scanning the index until enough results are found.
|
|
|
|
## More pgvector and Supabase resources
|
|
|
|
- [Supabase Clippy: ChatGPT for Supabase Docs](/blog/chatgpt-supabase-docs)
|
|
- [Storing OpenAI embeddings in Postgres with pgvector](/blog/openai-embeddings-postgres-vector)
|
|
- [A ChatGPT Plugins Template built with Supabase Edge Runtime](/blog/building-chatgpt-plugins-template)
|
|
- [Template for building your own custom ChatGPT style doc search](https://github.com/supabase-community/nextjs-openai-doc-search)
|