<i>result := array_append(result, ('num=>' || i)::HSTORE);</i><p>Yeah, I've never read a line of PLPGSQL before this, but I can tell by the assignment operator := that this is obviously creating a copy of the array every time you add an element to the array. This would also cause a quadratic blowup and/or a memory blowup in any other language I've heard of, not just PLPGSQL. Anyone who has taken an introductory algorithms & data structures class really should know better than to write code like this.