'Break array_agg column into batches

I have a query like the following:

SELECT mt.group_id, array_agg(mt.id) as my_array
FROM myTable mt
GROUP BY mt.group_id;

In some contexts the array contains too many elements and a max byte limit error is thrown.

Is there any way that I can break the array into separate smaller batches per row? So for example, if I wanted a maximum batch size of 2 the result set might look like:

group_id, my_array
1, {1,2}
1, {3,4}
2, {5}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source