g.vanluffe...@qipc.com writes: > int4range ( and any other range function) consumes much memory when used in > a select statement on a big table.
The problem is that range_out leaks memory, as a consequence of creating a number of intermediate strings that it doesn't bother to free. I don't believe it's the only output function that leaks memory, but it does so with particular vim: now that we've increased the initial size of StringInfo buffers, it's probably wasting upwards of 2K per call. While we could doubtless hack range_out to release those strings again, it seems to me that that's just sticking a finger in the dike. I'm inclined to think that we really ought to solve this class of problems once and for all by fixing printtup.c to run the output functions in a temporary memory context, which we could reset once per row to recover all memory used. That would also let us get rid of the inadequate kluges that printtup currently uses to try to minimize leakage, namely forced detoasting of varlena fields and forced pfree'ing of the strings returned by output functions. (There is no other context in which we imagine that it's safe to pfree a function's result, and there are a number of datatypes for which it'd make sense to return constant strings, were it not for this kluge.) It's possible that this would result in some net slowdown in tuple output; but it's also possible that eliminating the retail pfree's in favor of a single context reset per tuple would make for a net savings. In any case, we're already using a reset-per-row approach to memory management of output function calls in COPY OUT, and I know for a fact that we've squeezed that code path as hard as we could. Thoughts? regards, tom lane -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers