Skip to content

Instantly share code, notes, and snippets.

@ianthrive
Last active January 18, 2016 01:58
Show Gist options
  • Save ianthrive/6721e6428a0dfd3990bc to your computer and use it in GitHub Desktop.
Save ianthrive/6721e6428a0dfd3990bc to your computer and use it in GitHub Desktop.
create or replace function diff_elements(anyarray, anyarray)
returns anyarray language sql immutable as $$
select array(
select unnest($2)
except
select unnest($1)
);$$;
create operator - (
procedure = diff_elements,
leftarg=anyarray,
rightarg=anyarray
);
=# select array['a', 'n', 'z'] - array['n', 'z', 'd', 'e'];
?column?
----------
{d,e}
(1 row)
=# select array[1, 5, 6] - array[5, 6, 23, 64];
?column?
----------
{23,64}
(1 row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment