> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# VECTOR

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

The `VECTOR` data type stores fixed-length arrays of floating-point numbers, which represent data points in multi-dimensional space. Vector search is often used in AI applications such as Large Language Models (LLMs) that rely on vector representations.

For details on valid `VECTOR` comparison operators, refer to [Syntax](#syntax). For the list of supported `VECTOR` functions, refer to <InternalLink path="functions-and-operators">Functions and Operators</InternalLink>.

<Note>
  `VECTOR` functionality is compatible with the [`pgvector`](https://github.com/pgvector/pgvector) extension for PostgreSQL.
</Note>

## Syntax

A `VECTOR` value is expressed as an <InternalLink path="array">array</InternalLink> of <InternalLink path="float">floating-point numbers</InternalLink>. The array size corresponds to the number of `VECTOR` dimensions. For example, the following `VECTOR` has 3 dimensions:

```
[1.0, 0.0, 0.0]
```

You can specify the dimensions when defining a `VECTOR` column. This will enforce the number of dimensions in the column values. For example:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER TABLE foo ADD COLUMN bar VECTOR(3);
```

The following `VECTOR` comparison operators are valid:

* `=` (equals). Compare vectors for equality in filtering and conditional queries.
* `<>` (not equal to). Compare vectors for inequality in filtering and conditional queries.
* `<->` (L2 distance). Calculate the Euclidean distance between two vectors, as used in [nearest neighbor search](https://en.wikipedia.org/wiki/Nearest_neighbor_search) and clustering algorithms.
* `<#>` (negative inner product). Calculate the [inner product](https://en.wikipedia.org/wiki/Inner_product_space) of two vectors, as used in similarity searches where the inner product can represent the similarity score.
* `<=>` (cosine distance). Calculate the [cosine distance](https://en.wikipedia.org/wiki/Cosine_similarity) between vectors, such as in text and image similarity measures where the orientation of vectors is more important than their magnitude.

## Size

The size of a `VECTOR` value is variable, but it's recommended to keep values under 1 MB to ensure performance. Above that threshold, <InternalLink path="architecture/storage-layer#write-amplification">write amplification</InternalLink> and other considerations may cause significant performance degradation.

## Functions

For the list of supported `VECTOR` functions, refer to <InternalLink path="functions-and-operators">Functions and Operators</InternalLink>.

## Example

Create a table with a `VECTOR` column, specifying `3` dimensions:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
CREATE TABLE items (
    category STRING,
    vector VECTOR(3),
    INDEX (category)
);
```

Insert some sample data into the table:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
INSERT INTO items (category, vector) VALUES
	('electronics', '[1.0, 0.0, 0.0]'),
	('electronics', '[0.9, 0.1, 0.0]'),
	('furniture', '[0.0, 1.0, 0.0]'),
	('furniture', '[0.0, 0.9, 0.1]'),
	('clothing', '[0.0, 0.0, 1.0]');
```

Large batch inserts of <InternalLink path="vector">`VECTOR`</InternalLink> types can cause performance degradation. When inserting vectors, batching should be avoided. For an example, refer to <InternalLink path="vector-indexes#create-and-query-a-vector-index">Create and query a vector index</InternalLink>.

Use the [`<->` operator](#syntax) to sort values with the `electronics` category by their similarity to `[1.0, 0.0, 0.0]`, based on geographic distance.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SELECT category, vector FROM items WHERE category = 'electronics' ORDER BY vector <-> '[1.0, 0.0, 0.0]' LIMIT 5;
```

```
   category   |   vector
--------------+--------------
  electronics | [1,0,0]
  electronics | [0.9,0.1,0]
```

You can use a <InternalLink path="vector-indexes">vector index</InternalLink> to make searches on large numbers of high-dimensional `VECTOR` rows more efficient.

## See also

* <InternalLink path="vector-indexes">Vector Indexes</InternalLink>
* <InternalLink path="functions-and-operators">Functions and Operators</InternalLink>
* <InternalLink path="data-types">Data Types</InternalLink>
