Re: Sort memory not being released

From: "Jim C(dot) Nasby" <jim(at)nasby(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Sort memory not being released
Date: 2003-06-18 04:33:25
Message-ID: 20030617233325.W66185@flake.decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jun 17, 2003 at 08:08:29PM -0400, Tom Lane wrote:
> "Jim C. Nasby" <jim(at)nasby(dot)net> writes:
> > Well, there's other ways to do what I'm thinking of that don't rely on
> > getting a free memory number from the OS. For example, there could be a
> > 'total_sort_mem' parameter that specifies the total amount of memory
> > that can be used for all sorts on the entire machine.
>
> How would you find out how many other sorts are going on (and how much
> memory they're actually using)? And probably more to the point, what do
> you do if you want to sort and the parameter's already exhausted?

There'd need to be a list of sorts kept in the backend, presumably in
shared memory. When a sort started, it would add an entry to the list
specifying how much memory it intended to use. Once it was done, it
could update with how much was actually used. Actually, I guess a simple
counter would suffice instead of a list.

As for when memory runs out, there's two things you can do. Obviously,
you can just sleep until more memory becomes available (presumably the
lock on the shared list/counter would prevent more than one backend from
starting a sort at a time). A more elegant solution would be to start
decreasing how much memory a sort will use as the limit is approached. A
possible algorithm would be:

IF total_sort_mem - active_sort_mem < desired_sort_mem THEN
desired_sort_mem = (total_sort_mem - active_sort_mem) / 2

So if you can't get all the memory you'd like, take half of whatever's
available. Obviously there would have to be a limit to this... you can't
sort on 100 bytes. If (total_sort_mem - active_sort_mem) drops below a
certain threshold, you would either ignore it and use some small amount
of memory to do the sort, or you'd sleep until memory became available.

I know this might sound like a lot of added complexity, but if it means
you have a much better chance of being able to perform large sorts
in-memory instead of on-disk, I think it's well worth it.
--
Jim C. Nasby (aka Decibel!) jim(at)nasby(dot)net
Member: Triangle Fraternity, Sports Car Club of America
Give your computer some brain candy! www.distributed.net Team #1828

Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"

In response to

Browse pgsql-general by date

  From Date Subject
Next Message culley harrelson 2003-06-18 04:42:30 Re: plpython? (Was: Re: Damn triggers and NEW)
Previous Message Bruno Wolff III 2003-06-18 04:07:17 Re: ss_family in hba.c