Clouzark

Vector search

Store embeddings with pgvector and find similar rows, for semantic search, RAG, and agent memory.

Every Clouzark database can use pgvector. Your embeddings live in the same Postgres as the rest of your data, so a similarity search can join, filter, and respect row-level security like any other query.

Enable it

On the database's Extensions page, click Enable vector search. Or run:

create extension if not exists vector;

Store embeddings

Create a column whose size matches your embedding model's output, and an HNSW index for fast nearest-neighbor search:

create table documents (
  id bigserial primary key,
  content text not null,
  metadata jsonb default '{}',
  embedding vector(1536)
);

create index on documents using hnsw (embedding vector_cosine_ops);
Model outputColumn
1536 dimensions (e.g. OpenAI text-embedding-3-small)vector(1536)
1024 dimensions (e.g. Voyage voyage-3.5)vector(1024)
More than 2,000 dimensionshalfvec(n), indexable up to 4,000

An HNSW index on vector supports up to 2,000 dimensions.

<=> is cosine distance (lower is closer). Wrap the query in a function so apps can call it over the REST API:

create or replace function match_documents(query_embedding vector, match_count int default 5)
returns table (id bigint, content text, metadata jsonb, similarity float)
language sql stable
as $$
  select id, content, metadata, 1 - (embedding <=> query_embedding) as similarity
  from documents
  order by embedding <=> query_embedding
  limit match_count
$$;
OperatorDistanceIndex ops
<=>Cosinevector_cosine_ops
<->Euclidean (L2)vector_l2_ops
<#>Negative inner productvector_ip_ops

From your app

Embeddings are plain arrays of numbers over the REST API, so supabase-js works as-is:

import { createClient } from '@supabase/supabase-js';

const db = createClient('https://api.clouzark.com/projects/<database ref>', process.env.CLOUZARK_SERVICE_KEY!);

// Store: embed the text with your model's SDK, then insert the array.
await db.from('documents').insert({ content, embedding });

// Search
const { data } = await db.rpc('match_documents', { query_embedding: queryEmbedding, match_count: 5 });

Generate embeddings on the server, never in the browser, so your model provider's API key stays secret. Or connect with any Postgres client and pass the vector as a string: '[0.1, 0.2, ...]'.

Row-level security

Functions run as the caller by default (security invoker), so when an app calls match_documents with the anon key or a signed-in user's token, it only returns rows that user can already read. Enable RLS on documents and add policies as usual; see Auth. The service key bypasses row-level security, so the server-side example above searches every row.

Performance

  • Build the HNSW index after a bulk load, not before; inserting into an existing index is slower than building it once.
  • Filter with where inside the function before order by to search a subset, for example one user's documents.
  • HNSW indexes are held in memory while searching. Keep vectors only for the rows you search, and use halfvec to halve the size when full precision isn't needed.

On this page