CREATE OR REPLACE FUNCTION array_append_distinct(anyarray, anyelement) RETURNS anyarray AS $$ SELECT ARRAY(SELECT unnest($1) union SELECT $2) $$ LANGUAGE sql;postgres=# select array_append_distinct(array[1,2,3,4],1); array_append_distinct ----------------------- { 1,2,3,4}(1 row)postgres=# select array_append_distinct(array[1,2,3,4],7); array_append_distinct ----------------------- { 1,2,3,4,7}(1 row)CREATE OR REPLACE FUNCTION array_erase(anyarray, anyelement) RETURNS anyarray AS $$ SELECT ARRAY(SELECT v FROM unnest($1) g(v) WHERE v <> $2)$$ LANGUAGE sql;postgres=# select array_erase(array[1,2,3,2,5], 2); array_erase ------------- { 1,3,5}(1 row)postgres=# select array_erase(array[1,2,3,2,5], 1); array_erase ------------- { 2,3,2,5}(1 row)