Skip to main content
clusterize assigns each row of a vector column to a cluster of similar rows and returns a column of BIGINT cluster IDs. clus is an equivalent alias. Two rows are linked when their cosine similarity is at least min_similarity. Rows that are linked, directly or through a chain of other rows, share a cluster.

Signature

Example

HAVING removes single-row clusters, and LIMIT bounds the summary, not the rows being clustered.

Return type

BIGINT, numbered from 0 with no gaps. IDs label clusters within one run and can change between runs.

Behavior

  • clusterize finds each row’s candidate neighbors with approximate nearest-neighbor search over an HNSW index, the standard approach for fast vector search over large relations. Results can occasionally differ from an exact search.
  • The HNSW index is cached in memory. Repeated queries over the same in-memory table in one engine reuse it, even with a different k. See Caching.
  • A row with no links forms its own cluster.
  • Null or zero-magnitude embeddings each form their own cluster.
  • Clusters grow through chains of links, so lowering min_similarity can merge clusters through rows in between.
  • Clusters come from the rows in the query’s input. A WHERE filter or a LIMIT in a subquery changes which rows are clustered. A LIMIT in the same SELECT as clusterize doesn’t.
  • Use neighbors to list each row’s nearest rows with their similarity.
  • Use sim to compare two vectors directly.