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 typeARRAY can be expressed in the following ways:
- Appending square brackets (
[]) to any non-array . - Adding the term
ARRAYto any non-array .
Size
The size of anARRAY 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 supportedARRAY 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.
-
Create the table:
-
Insert two new arrays:
-
Use the
&&operator in aWHEREclause 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
betweenARRAY 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:
STRING value, for compatibility with PostgreSQL:
Implicit casting
CockroachDB supports implicit casting from string literals to arrays of all data types except the following:Box2DGEOGRAPHYGEOMETRY
INT[]:
INT[]:

