Skip to main content
The ARRAY data type stores one-dimensional, 1-indexed, homogeneous arrays of any non-array . The ARRAY data type is useful for ensuring compatibility with ORMs and other tools. However, if such compatibility is not a concern, it’s more flexible to design your schema with normalized tables. CockroachDB supports indexing array columns with . This permits accelerating containment queries ( and ) and overlap queries () on array columns by adding an index to them.
CockroachDB does not support nested arrays.

Syntax

A value of data type ARRAY can be expressed in the following ways:
  • Appending square brackets ([]) to any non-array .
  • Adding the term ARRAY to any non-array .

Size

The size of an ARRAY value is variable, but it’s recommended to keep values under 1 MB to ensure performance. Above that threshold, and other considerations may cause significant performance degradation.

Functions

For the list of supported ARRAY functions, see .

Examples

Creating an array column by appending square brackets

Creating an array column by adding the term ARRAY

Accessing an array element using array index

Arrays in CockroachDB are 1-indexed.

Accessing an array column using containment queries

You can use the <@ (“is contained by”) and @> (“contains”) to run containment queries on ARRAY columns.

Using the overlaps operator

You can use the && (overlaps) to select array columns by checking if another array overlaps the column array. Arrays overlap if they have any elements in common.
  1. Create the table:
  2. Insert two new arrays:
  3. Use the && operator in a WHERE clause to a query:

Appending an element to an array

Using the array_append function

Using the append (||) operator

Ordering by an array

Supported casting and conversion

between ARRAY values is supported when the data types of the arrays support casting. For example, it is possible to cast from a BOOL array to an INT array but not from a BOOL array to a TIMESTAMP array:
You can cast an array to a STRING value, for compatibility with PostgreSQL:

Implicit casting

CockroachDB supports implicit casting from string literals to arrays of all data types except the following:
  • Box2D
  • GEOGRAPHY
  • GEOMETRY
For example, if you create a table with a column of type INT[]:
And then insert a string containing a comma-delimited set of integers contained in brackets:
CockroachDB implicitly casts the string literal as an INT[]:

See also