9.18. Array Functions and Operators
Table 9.47 shows the operators available for array types.
Table 9.47. Array Operators
|less than or equal|
|greater than or equal|
|is contained by|
|overlap (have elements in common)|
The array ordering operators (
>=, etc) compare the array contents element-by-element, using the default B-tree comparison function for the element data type, and sort based on the first difference. In multidimensional arrays the elements are visited in row-major order (last subscript varies most rapidly). If the contents of two arrays are equal but the dimensionality is different, the first difference in the dimensionality information determines the sort order. (This is a change from versions of PostgreSQL prior to 8.2: older versions would claim that two arrays with the same contents were equal, even if the number of dimensions or subscript ranges were different.)
The array containment operators (
@>) consider one array to be contained in another one if each of its elements appears in the other one. Duplicates are not treated specially, thus
ARRAY[1,1] are each considered to contain the other.
See Section 8.15 for more details about array operator behavior. See Section 11.2 for more details about which operators support indexed operations.
Table 9.48 shows the functions available for use with array types. See Section 8.15 for more information and examples of the use of these functions.
Table 9.48. Array Functions
|append an element to the end of an array|
|concatenate two arrays|
|returns the number of dimensions of the array|
|returns a text representation of array's dimensions|
|returns an array initialized with supplied value and dimensions, optionally with lower bounds other than 1|
|returns the length of the requested array dimension|
|returns lower bound of the requested array dimension|
|returns the subscript of the first occurrence of the second argument in the array, starting at the element indicated by the third argument or at the first element (array must be one-dimensional)|
|returns an array of subscripts of all occurrences of the second argument in the array given as first argument (array must be one-dimensional)|
|append an element to the beginning of an array|
|remove all elements equal to the given value from the array (array must be one-dimensional)|
|replace each array element equal to the given value with a new value|
|concatenates array elements using supplied delimiter and optional null string|
|returns upper bound of the requested array dimension|
|returns the total number of elements in the array, or 0 if the array is empty|
|splits string into array elements using supplied delimiter and optional null string|
|expand an array to a set of rows|
1 2(2 rows)
|expand multiple arrays (possibly of different types) to a set of rows. This is only allowed in the FROM clause; see Section 18.104.22.168|
1 foo 2 bar NULL baz(3 rows)
array_positions, each array element is compared to the searched value using
IS NOT DISTINCT FROM semantics.
NULL is returned if the value is not found.
NULL is returned only if the array is
NULL; if the value is not found in the array, an empty array is returned instead.
string_to_array, if the delimiter parameter is NULL, each character in the input string will become a separate element in the resulting array. If the delimiter is an empty string, then the entire input string is returned as a one-element array. Otherwise the input string is split at each occurrence of the delimiter string.
string_to_array, if the null-string parameter is omitted or NULL, none of the substrings of the input will be replaced by NULL. In
array_to_string, if the null-string parameter is omitted or NULL, any null elements in the array are simply skipped and not represented in the output string.
There are two differences in the behavior of
string_to_array from pre-9.1 versions of PostgreSQL. First, it will return an empty (zero-element) array rather than NULL when the input string is of zero length. Second, if the delimiter string is NULL, the function splits the input into individual characters, rather than returning NULL as before.
See also Section 9.20 about the aggregate function
array_agg for use with arrays.