I have a table with items with category id-s and I want to create a function that gets category ids array and returns an array of random items with corresponding categories. Here is what I've tried
CREATE OR REPLACE FUNCTION public.get_random_items(IN args uuid[])
RETURNS SETOF items
LANGUAGE 'plpgsql'
VOLATILE
PARALLEL UNSAFE
COST 100 ROWS 1000
AS $BODY$
DECLARE
cat_id uuid;
output items[];
BEGIN
FOREACH cat_id IN ARRAY args LOOP
EXECUTE (
SELECT *
FROM items
WHERE category_id = cat_id
ORDER BY random()
LIMIT 1
) INTO output;
END LOOP;
RETURN QUERY SELECT * FROM output;
END $BODY$;
And when running -
SELECT * FROM get_random_items('{d59f2779-4868-439c-96eb-b5a735135379}')
.
I get this error -
ERROR: subquery must return only one column
.
Any ideas on how can I achieve this?