Thanks again for the precision !
I still don't understand perfectly. We call the aggregate n times, and each time we compute the aggregate, using (potentially) n rows, thus becoming (at most) O(n*n).
With a standard loop, I loop n times, and each times I only need the current row plus the previous row which I put in memory, thus O(n).
I posted a lot about my issue, but only about the fraction of the problem I was blocked by, and I get no conclusive answer.
My problem was to find a good way to have a plpgsql function taking set of rows as input and returning a set of rows.
I worked on range, and I wanted a polymorphic function (working with any range).
Aggregates only returns one row at most, array are dangerous with big data, temp table have to be created/deleted and have to be used in the same session, cursors arn't well supported by accessing library, view can't be written, mat view weren't available.
Anyway I solved it using cursors, not optimal but works !
Cheers,
Rémi-C