I recently had need of an "array_except" function but couldn't find
any good/existing examples. Based off the neat "array_intersect"
function at http://www.postgres.cz/index.php/PostgreSQL_SQL_Tricks#Intersection_of_arrays,
I put together an "array_except" version to return the array elements
that are not found in both arrays.
Can anyone think of a faster version of this function? Maybe in C?
The generate_series example takes about 3.5s on the dev db I'm testing
on, which isn't too bad (for my needs at least).
create or replace function array_except(anyarray,anyarray) returns
anyarray as $$
(select unnest($1) except select unnest($2))
(select unnest($2) except select unnest($1))
) as r (elements)
$$ language sql strict immutable;