From: Tom Lane Date: Tue, 21 Nov 2006 22:35:29 +0000 (+0000) Subject: Prevent intratransaction memory leak when a subtransaction is aborted X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=d5320abb19a8140d763cc0fa3f8d1bb10d2bc171;p=users%2Fbernd%2Fpostgres.git Prevent intratransaction memory leak when a subtransaction is aborted in the middle of executing a SPI query. This doesn't entirely fix the problem of memory leakage in plpgsql exception handling, but it should get rid of the lion's share of leakage. --- diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c index 7d4485838e..ab7e793744 100644 --- a/src/backend/executor/spi.c +++ b/src/backend/executor/spi.c @@ -254,6 +254,19 @@ AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid) (errcode(ERRCODE_WARNING), errmsg("subtransaction left non-empty SPI stack"), errhint("Check for missing \"SPI_finish\" calls."))); + + /* + * If we are aborting a subtransaction and there is an open SPI context + * surrounding the subxact, clean up to prevent memory leakage. + */ + if (_SPI_current && !isCommit) + { + /* free Executor memory the same as _SPI_end_call would do */ + MemoryContextResetAndDeleteChildren(_SPI_current->execCxt); + /* throw away any partially created tuple-table */ + SPI_freetuptable(_SPI_current->tuptable); + _SPI_current->tuptable = NULL; + } }