Re: [BUGS] BUG #8573: int4range memory consumption

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: g(dot)vanluffelen(at)qipc(dot)com
Cc: pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: [BUGS] BUG #8573: int4range memory consumption
Date: 2013-11-01 19:08:28
Message-ID: 18832.1383332908@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

g(dot)vanluffelen(at)qipc(dot)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

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Jim Nasby 2013-11-01 20:21:43 Re: [BUGS] BUG #8573: int4range memory consumption
Previous Message Tom Lane 2013-11-01 18:12:58 Re: BUG #8572: Combination of SET TIME ZONEs and CAST gives wrong results

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2013-11-01 19:21:48 Re: API bug in DetermineTimeZoneOffset()
Previous Message Robert Haas 2013-11-01 19:04:24 Re: Save Hash Indexes