ARRAY data type stores one-dimensional, 1-indexed, homogeneous arrays of any non-array data type.
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 GIN indexes. 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 data type. - Adding the term
ARRAYto any non-array data type.
Size
The size of anARRAY value is variable, but it’s recommended to keep values under 1 MB to ensure performance. Above that threshold, write amplification and other considerations may cause significant performance degradation.
Functions
For the list of supportedARRAY functions, see Functions and Operators.
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 operators<@ (“is contained by”) and @> (“contains”) to run containment queries on ARRAY columns.
Using the overlaps operator
You can use the&& (overlaps) operator 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
Casting 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:BYTESENUMJSONBSERIALBox2D(spatial type)GEOGRAPHY(spatial type)GEOMETRY(spatial type)
INT[]:
INT[]:

