pgsql: Fix worst memory leaks in tqueue.c.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Fix worst memory leaks in tqueue.c.
Date: 2016-07-29 23:31:23
Message-ID: E1bTHFT-0003qj-HL@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix worst memory leaks in tqueue.c.

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(dot)1469821037(at)sss(dot)pgh(dot)pa(dot)us>

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/af33039317ddc4a0e38a02e2255c2bf453115fd2

Modified Files
--------------
src/backend/executor/nodeGather.c | 45 ++++++++++++++++++++++-----------------
src/backend/executor/tqueue.c | 22 ++++++++++++-------
src/include/executor/tqueue.h | 8 ++++---
3 files changed, 45 insertions(+), 30 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Bruce Momjian 2016-07-30 16:28:14 pgsql: docs: properly capitalize and space kB, MB, GB, TB
Previous Message Tom Lane 2016-07-29 18:13:26 pgsql: Fix tqueue.c's range-remapping code.