function to add an element to an array and get all unique elements only

This commit is contained in:
Paul Trowbridge 2022-11-04 13:37:00 -04:00
parent b34afce702
commit 27e8ea7025

View File

@ -0,0 +1,23 @@
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