Andres Freund <and...@anarazel.de> writes: > On 2017-12-12 14:50:37 -0500, Robert Haas wrote: >> It strikes me that a way to optimize these cases even more would be to >> postpone creating the context until it's actually needed. That might >> not always be a reasonable plan -- in particular, it occurs to me to >> think that CurTransactionContext is probably so widely used that >> changing anything about how it works would probably be really painful >> -- but it might be possible in some cases.
> That's not generally easy without slowing things down though - e.g. we > don't want to check for ExprContext's existence before every use, there > can be billions of usages in a single analytics query. The branches (yea > yea ;)) would show up as being noticeable. Yeah. Also, in most of these cases what we're doing with the context is installing it as CurrentMemoryContext while we execute some random code that might or might not need to palloc anything. We can't just set CurrentMemoryContext to null - for one thing, that would leave no trace of how the context should get set up if it's needed. You could imagine installing some sort of placeholder, but really that's the mechanism we've already got, in the case where we just make a context header and no blocks. >> Another idea I have is that perhaps we could arrange to reuse contexts >> instead of destroying them and recreating them. > I'm a bit doubtful that's going to help, maintaining that list isn't > going to be free, and the lifetime and number of those contexts aren't > always going to match up. Actually, this seems like a really promising idea to me. To the extent that an empty context has standard parameters, they're all interchangeable, so you could imagine that AllocSetDelete shoves it onto a freelist after resetting it, instead of just giving it back to libc. I'm slightly worried about creating allocation islands that way, but that problem is probably surmountable, if it's real at all. However, that seems like a different patch from what I'm working on here. regards, tom lane