Skip to main content

Array functions

array_append

Appends any_compatible to the end of the input array.

array_cat

Concatenates two arrays with the same data type. If the one of the input arrays is a 2-dimensional array, the other array will be appended within the first array as the last element if it is the second argument. The other array will be prepended within the first array as the first element if it is the first argument.

array_dims

Returns the dimensions of an array as a string. The array must be one dimensional.

array_distinct

Returns an array of the same type as the input array with all duplicate values removed.

array_flatten

Added in v2.5.0.
Flattens an array of arrays into a single array. If the input array contains multiple levels of nested arrays, the function will only flatten the first level.

array_length

This function has two variants.

array_length ( array )

Returns the length of array.

array_length ( array int )

Returns the length of the requested array dimension in array. int must be 1.

array_lower

Returns the lower bound of the requested array dimension in array. (This is always 1 or null.)

array_max

Returns the maximum value in an array. Null elements are skipped, but if the array contains only null elements, NULL is returned.

array_min

Returns the minimum value in an array. Null elements are skipped, but if the array contains only null elements, NULL is returned.

array_ndims

Returns the number of dimensions of array.

array_position

Returns the subscript of the first occurrence of any_compatible element in array.

array_positions

Returns an array of the subscripts of all occurrences of any_compatible element in array.

array_prepend

Prepends any_compatible to the beginning of the input array.

array_remove

Returns an array with all occurrences of any_compatible element removed. Multidimensional arrays are also supported.

array_replace

Returns an array with all occurrences of current_element replaced with new_element. Multidimensional arrays are also supported. When the array is multidimensional, the element must be an array of one less dimension. Recursively replacing the base element of a multidimensional array is not supported.

array_sort

Sorts the elements of an array in ascending order.

array_sum

Returns the sum of the values in the array. Null elements are skipped.

array_to_string and array_join

Converts an array to a string. The optional delimiter_string separates the array’s elements in the resulting string, and the optional null_string represents NULL elements in the array. array_join can also be used instead of array_to_string.

array_transform

This function takes an array, transforms the elements, and returns the results in a new array. The output array always has the same length as the input array.
Each element in array_expression is evaluated against the transform_expression. element_alias is an alias that represents an array element.
Example A
Example B
Note that the transform_expression does not support referencing columns. For example, if you have a table:
The following query will fail.

array_upper

Returns the upper bound of the requested array dimension in array. int must be 1. (This will return the same value as array_length.)

cardinality

Returns the total number of elements in array or 0 if the array is empty.

string_to_array

Converts a string to an array. The optional delimiter_string separates the string’s elements to create the resulting array, and the optional null_string represents NULL elements in the array.

trim_array

Trims an array by removing the last n elements. If the array is multidimensional, only the first dimension is trimmed.

unnest

Expands an array, or combination of arrays, into a set of rows. The array’s elements are output in the order they are stored.

Best practices

When using the unnest function, consider the following performance-optimized patterns: For aggregation: If you want to unnest an array and then aggregate the values back to a scalar, consider using functions like array_sum directly against the array. This approach is preferred over using unnest and then sum to aggregate. Pure functions like array_sum have much better performance than relational level aggregation, which introduces additional states. For unnesting: If you want to unnest the array only, use unnest in the SELECT clause directly instead of in a correlated subquery. Correlated subqueries introduce additional states, which impacts performance. When you need to add ordinality to each unnested row, you can use unnest together with generate_series. Given this example table:
For aggregation:
For array elements with ordinality:
Note: If you’re using unnest for row-level processing, followed by an array_agg aggregation, it can incur additional overhead due to subplan execution. Use EXPLAIN to examine your query plan, and see if there are unexpected JOINs in your query. If so, please consider rewriting with array_transform to perform your mapping logic to avoid the subplan execution.

Array operators

array @> array -> boolean

This operator checks if the left array contains all elements of the right array.

array <@ array -> boolean

This operator checks if the left array is contained by the right array.

array || anycompatible → array

Appends any_compatible to the end of array. This operation achieves the same result as using array_append.

array || array → array

Concatenates two arrays with the same data type. This operation achieves the same result as using array_cat.

anycompatible || array → array

Prepends any_compatible to the beginning of array. This operation achieves the same result as using array_prepend.