... ¿Dónde están las bibliotecas estándar (?) Para este tipo de utilidad array_X ??
Intente buscar ... Ver algunos, pero no estándar:
La array_distinct()función snippet-lib más simple y rápida
Aquí la implementación más simple y quizás más rápida para array_unique()o array_distinct():
CREATE FUNCTION array_distinct(anyarray) RETURNS anyarray AS $f$
SELECT array_agg(DISTINCT x) FROM unnest($1) t(x);
$f$ LANGUAGE SQL IMMUTABLE;
NOTA: funciona como se esperaba con cualquier tipo de datos, excepto con una matriz de matrices,
SELECT array_distinct( array[3,3,8,2,6,6,2,3,4,1,1,6,2,2,3,99] ),
array_distinct( array['3','3','hello','hello','bye'] ),
array_distinct( array[array[3,3],array[3,3],array[3,3],array[5,6]] );
el "efecto secundario" es descomponer todas las matrices en un conjunto de elementos.
PD: con matrices JSONB funciona bien,
SELECT array_distinct( array['[3,3]'::JSONB, '[3,3]'::JSONB, '[5,6]'::JSONB] );
Editar: más complejo pero útil, un parámetro "eliminar nulos"
CREATE FUNCTION array_distinct(
anyarray,
boolean DEFAULT false
) RETURNS anyarray AS $f$
SELECT array_agg(DISTINCT x)
FROM unnest($1) t(x)
WHERE CASE WHEN $2 THEN x IS NOT NULL ELSE true END;
$f$ LANGUAGE SQL IMMUTABLE;