pgsql: Fix the plpgsql memory leak exhibited in bug #4677.

From: tgl(at)postgresql(dot)org (Tom Lane)
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Fix the plpgsql memory leak exhibited in bug #4677.
Date: 2009-04-09 02:57:53
Message-ID: 20090409025753.BE41E7540F4@cvs.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Log Message:
-----------
Fix the plpgsql memory leak exhibited in bug #4677. That leak was introduced
by my patch of 2007-01-28 to use per-subtransaction ExprContexts/EStates:
since we re-prepared any expression tree when the current subtransaction ID
changed, we'd accumulate more and more leaked expression state trees in the
outermost subtransaction if the same function was executed at multiple levels
of subtransaction nesting. To fix, go back to the previous scheme where
there was only one EState per transaction for simple plpgsql expressions.
We really only need an ExprContext per subtransaction, not a whole EState,
so it's possible to keep prepared expression state trees in the one EState
throughout the transaction. This should be more efficient as well as not
leaking memory for cases involving lots of subtransactions.

The added regression test is the case that inspired the 2007-01-28 patch in
the first place, just to make sure we didn't go backwards. The current
memory leak complaint is unfortunately hard to test for in the regression
test framework, though manual testing shows it's fixed.

Although this is a pre-existing bug, I'm not back-patching because I'd like to
see this method get some field testing first. Consider back-patching if it
gets through 8.4beta unscathed.

Modified Files:
--------------
pgsql/src/pl/plpgsql/src:
pl_exec.c (r1.239 -> r1.240)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/pl/plpgsql/src/pl_exec.c?r1=1.239&r2=1.240)
plpgsql.h (r1.109 -> r1.110)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/pl/plpgsql/src/plpgsql.h?r1=1.109&r2=1.110)
pgsql/src/test/regress/expected:
plpgsql.out (r1.69 -> r1.70)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/test/regress/expected/plpgsql.out?r1=1.69&r2=1.70)
pgsql/src/test/regress/sql:
plpgsql.sql (r1.58 -> r1.59)
(http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/test/regress/sql/plpgsql.sql?r1=1.58&r2=1.59)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2009-04-09 14:21:03 pgsql: Treat EOF like \n for line-counting purposes in ParseConfigFile,
Previous Message Tom Lane 2009-04-08 22:29:30 pgsql: Remove psql's ancient hack that suppressed functions taking or