This is what I am currently using:
CREATE FUNCTION array_intersect(a1 anyarray, a2 anyarray) RETURNS anyarray AS $$ SELECT ARRAY( SELECT unnest($1) INTERSECT SELECT unnest($2) ORDER BY 1 ); $$ LANGUAGE sql IMMUTABLE STRICT; --get the length: select array_length ( array_intersect(array[...], array[...]), 1); Is there a faster way?
intarrayextension provides the&operator, but there doesn't seem to be a generalized one for all arrays despite the existence of the&&boolean test for overlapping arrays. It'd be nice to add, but ... well, take a look at the C source code for the PostgreSQL general purpose array functions and operators to see why there aren't as many as you might expect. The array API is horrid.