• Tom Lane's avatar
    Fix worst memory leaks in tqueue.c. · af330393
    Tom Lane authored
    TupleQueueReaderNext() leaks like a sieve if it has to do any tuple
    disassembly/reconstruction.  While we could try to clean up its allocations
    piecemeal, it seems like a better idea just to insist that it should be run
    in a short-lived memory context, so that any transient space goes away
    automatically.  I chose to have nodeGather.c switch into its existing
    per-tuple context before the call, rather than inventing a separate
    context inside tqueue.c.
    
    This is sufficient to stop all leakage in the simple case I exhibited
    earlier today (see link below), but it does not deal with leaks induced
    in more complex cases by tqueue.c's insistence on using TopMemoryContext
    for data that it's not actually trying hard to keep track of.  That issue
    is intertwined with another major source of inefficiency, namely failure
    to cache lookup results across calls, so it seems best to deal with it
    separately.
    
    In passing, improve some comments, and modify gather_readnext's method for
    deciding when it's visited all the readers so that it's more obviously
    correct.  (I'm not actually convinced that the previous code *is*
    correct in the case of a reader deletion; it certainly seems fragile.)
    
    Discussion: <32763.1469821037@sss.pgh.pa.us>
    af330393
nodeGather.c 13.3 KB