notes/postgres/jsonb_arr_aggcd.sql

28 lines
599 B
PL/PgSQL

CREATE OR REPLACE FUNCTION public.jsonb_concat_distinct_arr(
state jsonb,
concat jsonb)
RETURNS jsonb AS
$BODY$
BEGIN
--RAISE notice 'state is %', state;
--RAISE notice 'concat is %', concat;
RETURN
jsonb_agg(DISTINCT ae)
FROM
(
SELECT jsonb_array_elements(state) ae
UNION ALL
SELECT jsonb_array_elements(concat) ae
) x;
END;
$BODY$
LANGUAGE plpgsql;
DROP AGGREGATE IF EXISTS public.jsonb_arr_aggcd(jsonb);
CREATE AGGREGATE public.jsonb_arr_aggcd(jsonb) (
SFUNC=public.jsonb_concat_distinct_arr,
STYPE=jsonb,
INITCOND='[]'
);