Array comparisons (= ANY, @>)
PG 8.2+→ booleanArray containment and overlap operators. @> tests if the left array contains all elements of the right; <@ is the reverse; && tests for any overlap.
Signatures
anyarray @> anyarray → booleananyarray <@ anyarray → booleananyarray && anyarray → booleanParameters
| Parameter | Type | Description |
|---|---|---|
| array1 | anyarray | Left-hand array |
| array2 | anyarray | Right-hand array |
Examples
SELECT ARRAY[1,2,3] @> ARRAY[2,3];true (contains)SELECT ARRAY[1,2] <@ ARRAY[1,2,3,4];true (is contained by)SELECT ARRAY[1,2] && ARRAY[3,4];false (no overlap)SELECT * FROM posts WHERE tags @> ARRAY['postgres','performance'];Posts tagged with both postgres and performance`ARRAY[1,2,3] && ARRAY[2,9]` is true because 2 is shared — even though 9 is not in the left array. Developers intending 'the left array contains all elements of the right' use `&&` and get false positives.
✓ Instead: Use `col_array @> ARRAY[required1, required2]` to test that the column array contains all required elements. Use `&&` only when partial overlap is the correct condition.
Create a GIN index on array columns to accelerate `@>`, `<@`, and `&&` operators: `CREATE INDEX ON posts USING GIN (tags)`. Without this index, these operators require full table scans.
CREATE INDEX ON articles USING GIN (tags);
SELECT * FROM articles WHERE tags @> ARRAY['postgresql', 'index'];Fast containment search on GIN-indexed array column