RefLib

Reference library for AI

Back

postgresql_16:9.6.20.1 intarray Functions and Operators

PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > intarray — manipulate arrays of integers > intarray Functions and Operators

The functions provided by the intarray module are shown in (see intarray-func-table), the operators in (see intarray-op-table).

| Function Description Example(s) | | --- | | icount ( integer[] ) integer Returns the number of elements in the array. icount('{1,2,3}'::integer[]) 3 | | sort ( integer[], dir text ) integer[] Sorts the array in either ascending or descending order. dir must be asc or desc. sort('{1,3,2}'::integer[], 'desc') {3,2,1} | | sort ( integer[] ) integer[] sort_asc ( integer[] ) integer[] Sorts in ascending order. sort(array[11,77,44]) {11,44,77} | | sort_desc ( integer[] ) integer[] Sorts in descending order. sort_desc(array[11,77,44]) {77,44,11} | | uniq ( integer[] ) integer[] Removes adjacent duplicates. Often used with sort to remove all duplicates. uniq('{1,2,2,3,1,1}'::integer[]) {1,2,3,1} uniq(sort('{1,2,3,2,1}'::integer[])) {1,2,3} | | idx ( integer[], item integer ) integer Returns index of the first array element matching item, or 0 if no match. idx(array[11,22,33,22,11], 22) 2 | | subarray ( integer[], start integer, len integer ) integer[] Extracts the portion of the array starting at position start, with len elements. subarray('{1,2,3,2,1}'::integer[], 2, 3) {2,3,2} | | subarray ( integer[], start integer ) integer[] Extracts the portion of the array starting at position start. subarray('{1,2,3,2,1}'::integer[], 2) {2,3,2,1} | | intset ( integer ) integer[] Makes a single-element array. intset(42) {42} |

| Operator Description | | --- | | integer[] && integer[] boolean Do arrays overlap (have at least one element in common)? | | integer[] @> integer[] boolean Does left array contain right array? | | integer[] <@ integer[] boolean Is left array contained in right array? | | # integer[] integer Returns the number of elements in the array. | | integer[] # integer integer Returns index of the first array element matching the right argument, or 0 if no match. (Same as idx function.) | | integer[] + integer integer[] Adds element to end of array. | | integer[] + integer[] integer[] Concatenates the arrays. | | integer[] - integer integer[] Removes entries matching the right argument from the array. | | integer[] - integer[] integer[] Removes elements of the right array from the left array. | | integer[] \| integer integer[] Computes the union of the arguments. | | integer[] \| integer[] integer[] Computes the union of the arguments. | | integer[] & integer[] integer[] Computes the intersection of the arguments. | | integer[] @@ query_int boolean Does array satisfy query? (see below) | | query_int ~~ integer[] boolean Does array satisfy query? (commutator of @@) |

The operators &&, @> and <@ are equivalent to PostgreSQL's built-in operators of the same names, except that they work only on integer arrays that do not contain nulls, while the built-in operators work for any array type. This restriction makes them faster than the built-in operators in many cases.

The @@ and ~~ operators test whether an array satisfies a *query*, which is expressed as a value of a specialized data type query_int. A *query* consists of integer values that are checked against the elements of the array, possibly combined using the operators & (AND), | (OR), and ! (NOT). Parentheses can be used as needed. For example, the query 1&(2|3) matches arrays that contain 1 and also contain either 2 or 3.

Back