"Jim C. Nasby" <jim@nasby.net> writes:
> Of course I wasn't planning on sucking down a bunch of memory and
> holding on to it. :)
Sure. But when you're done with the big sort, just start a fresh
session. I don't see that this is worth agonizing over.
> If sort_mem is over X size, then use only Y for pre-buffering (How much
> does a large sort_mem help if you have to spill to disk?)
It still helps quite a lot, because the average initial run length is
(if I recall Knuth correctly) twice the working buffer size. I can't
see a reason for cutting back usage once you've been forced to start
spilling.
The bigger problem with your discussion is the assumption that we can
find out "if the OS is running low on free physical memory". That seems
(a) unportable and (b) a moving target.
regards, tom lane