pgsql: Improve plpgsql's memory management to fix some function-lifespa

Поиск
Список
Период
Сортировка
От Tom Lane
Тема pgsql: Improve plpgsql's memory management to fix some function-lifespa
Дата
Msg-id E1ba5vp-0004ZO-Ry@gemulon.postgresql.org
обсуждение исходный текст
Список pgsql-committers
Improve plpgsql's memory management to fix some function-lifespan leaks.

In some cases, exiting out of a plpgsql statement due to an error, then
catching the error in a surrounding exception block, led to leakage of
temporary data the statement was working with, because we kept all such
data in the function-lifespan SPI Proc context.  Iterating such behavior
many times within one function call thus led to noticeable memory bloat.

To fix, create an additional memory context meant to have statement
lifespan.  Since many plpgsql statements, particularly the simpler/more
common ones, don't need this, create it only on demand.  Reset this context
at the end of any statement that uses it, and arrange for exception cleanup
to reset it too, thereby fixing the memory-leak issue.  Allow a stack of
such contexts to exist to handle cases where a compound statement needs
statement-lifespan data that persists across calls of inner statements.

While at it, clean up code and improve comments referring to the existing
short-term memory context, which by plpgsql convention is the per-tuple
context of the eval_econtext ExprContext.  We now uniformly refer to that
as the eval_mcontext, whereas the new statement-lifespan memory contexts
are called stmt_mcontext.

This change adds some context-creation overhead, but on the other hand
it allows removal of some retail pfree's in favor of context resets.
On balance it seems to be about a wash performance-wise.

In principle this is a bug fix, but it seems too invasive for a back-patch,
and the infrequency of complaints weighs against taking the risk in the
back branches.  So we'll fix it only in HEAD, at least for now.

Tom Lane, reviewed by Pavel Stehule

Discussion: <17863.1469142152@sss.pgh.pa.us>

Branch
------
master

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

Modified Files
--------------
src/pl/plpgsql/src/pl_exec.c | 590 +++++++++++++++++++++++++++++--------------
src/pl/plpgsql/src/plpgsql.h |   6 +-
2 files changed, 403 insertions(+), 193 deletions(-)


В списке pgsql-committers по дате отправления:

Предыдущее
От: Magnus Hagander
Дата:
Сообщение: pgsql: Disable update_process_title by default on Windows
Следующее
От: Tom Lane
Дата:
Сообщение: pgsql: Fix -e option in contrib/intarray/bench/bench.pl.