intarray

intarray

intarray : functions, operators, and index support for 1-D arrays of integers

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
4960
intarray
intarray
1.5
FUNC
PostgreSQL
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
----dt-
No
No
No
Yes
no
yes
Relationships
See Also
aggs_for_arrays
aggs_for_vecs
arraymath
floatvec
vector
vchord
vectorscale
vectorize

Packages

PG18PG17PG16PG15PG14
1.5
1.5
1.5
1.5
1.5

This is a built-in contrib extension ship with the PostgreSQL kernel

Install

Create this extension with:

CREATE EXTENSION intarray;

Usage

intarray: integer array functions and operators with index support

Provides functions and operators for manipulating null-free integer arrays, with GiST and GIN index support for fast array searches.

CREATE EXTENSION intarray;

Functions

FunctionDescriptionExample
icount(int[])Number of elementsicount('{1,2,3}') – 3
sort(int[], dir)Sort array ('asc' or 'desc')sort('{3,1,2}','asc'){1,2,3}
sort_asc(int[])Sort ascendingsort_asc('{3,1,2}'){1,2,3}
sort_desc(int[])Sort descendingsort_desc('{3,1,2}'){3,2,1}
uniq(int[])Remove adjacent duplicatesuniq(sort('{1,2,3,2,1}')){1,2,3}
idx(int[], item)Index of first matchidx('{11,22,33}', 22) – 2
subarray(int[], start, len)Extract sub-arraysubarray('{1,2,3,4}', 2, 2){2,3}
intset(int)Make single-element arrayintset(42){42}

Operators

OperatorDescription
&&Arrays overlap (have common elements)
@>Left array contains right
<@Left array is contained in right
#Number of elements
+Array concatenation / append element
-Remove elements
|Union of arrays
&Intersection of arrays
@@Array matches a query expression
~~Query expression matches array

Index Support

-- GiST index for array containment/overlap queries
CREATE INDEX idx ON messages USING GIST (tags gist__intbig_ops);

-- GIN index (alternative)
CREATE INDEX idx ON messages USING GIN (tags gin__int_ops);

-- Query with index support
SELECT * FROM messages WHERE tags && '{1,2}';     -- overlap
SELECT * FROM messages WHERE tags @> '{1,2}';     -- contains
SELECT * FROM messages WHERE tags @@ '1&(2|3)';  -- query expression
Last updated on