CREATE OR REPLACE FUNCTION public.jsonb_array_add_distinct(_arr jsonb, _add text) RETURNS jsonb AS $$ DECLARE _ret jsonb; BEGIN SELECT jsonb_agg(DISTINCT x.ae) INTO _ret FROM ( SELECT jsonb_array_elements_text(_arr) ae UNION ALL SELECT _add ae ) x; RETURN _ret; END; $$ language plpgsql