Set equality

From PostgreSQL wiki

Jump to: navigation, search

Snippets

Set equality

Works with PostgreSQL

all maintained versions

Written in

SQL

Depends on

Nothing


Array equality is sensitive to the order and duplication within the arrays, so it is not suitable for testing arrays as if they were sets.

Set equality can obtained by testing arrays for "contains and is contained by".

Implementation

This is a convenience operator. It is not performance optimized, does not support indexes, and does not consider multidimensional arrays.

CREATE FUNCTION contains_contained_by (anyarray, anyarray) RETURNS BOOLEAN 
LANGUAGE SQL AS $$
    SELECT $1 <@ $2 AND $1 @> $2 
$$
parallel safe --omit this line prior to version 9.6 
immutable; 
 
CREATE operator <@> (
    PROCEDURE = contains_contained_by, 
    leftarg = anyarray, 
    rightarg = anyarray
);

Usage

 SELECT '{c,a,b}'::text[] <@> '{b,a,c,b}'::text[];
Personal tools