How to get rid of recursion in the query:
CREATE OR REPLACE FUNCTION accounts.get_group_by_group(_id uuid) RETURNS SETOF uuid AS $BODY$ BEGIN RETURN query WITH RECURSIVE R AS ( SELECT id FROM accounts.groups WHERE id=(_id) UNION ALL SELECT accounts.groups.id FROM accounts.groups JOIN R ON accounts.groups.parent_id = r.id ) SELECT *FROM R; END; $BODY$ LANGUAGE plpgsql VOLATILE