Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WIIUW-0002Mi-1i for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Feb 2014 13:56:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WIIUV-0005Lm-8B for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Feb 2014 13:56:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WIIUT-0005Kp-8R; Tue, 25 Feb 2014 13:56:09 +0000 Received: from mail-lb0-x22e.google.com ([2a00:1450:4010:c04::22e]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WIIUQ-0001uv-Fu; Tue, 25 Feb 2014 13:56:08 +0000 Received: by mail-lb0-f174.google.com with SMTP id l4so3259972lbv.5 for ; Tue, 25 Feb 2014 05:56:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=uukKbxLgDj7Eq3QLYveYQ8DyKP4aqIFDRXQpiuN7KUg=; b=oJ6MRLP3/DfNtmdnKe6ZJYYmjnqWTyVOEsl4Yz780KfAlj2FeA3VdOv+NgavgB/ks+ CQQqP82lTZMKi1zPGPO/2hYMfNxLJuoWm6Afv9XxIspFk2XVSXmZfKssyf1K3KqkLUYk kAEBUWFtnQ2FLlTu95tboteqaw0Id+jSPzG1Zfm4Lx2ZGIdIYnb+GcHmgK/2NKC4YNGZ ifw7ILHXUnIZ9nBQyiUi2wTbF3tne/temnAdhlVUpOmUqqML2mA3TrwCDnUW6dNhw9rx qNO0RaRZp0ZbhumXUcKMEVHrE5O529yDHeDQA03x92280xiGMTsOBSUITrgMnVAeMLaj 1EIA== X-Received: by 10.112.43.70 with SMTP id u6mr15153835lbl.30.1393336564606; Tue, 25 Feb 2014 05:56:04 -0800 (PST) Received: from seb.koffice.internal (koffice.avito.ru. [195.151.206.25]) by mx.google.com with ESMTPSA id e1sm112316laa.8.2014.02.25.05.56.02 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 25 Feb 2014 05:56:03 -0800 (PST) From: Sergey Burladyan To: pgsql-bugs@postgresql.org Cc: pgsql-hackers@postgresql.org Subject: Re: [BUGS] BUG #9223: plperlu result memory leak References: <20140214163125.24620.66179@wrigleys.postgresql.org> Date: Tue, 25 Feb 2014 17:56:01 +0400 In-Reply-To: <20140214163125.24620.66179@wrigleys.postgresql.org> (eshkinkot@gmail.com's message of "Fri, 14 Feb 2014 16:31:25 +0000") Message-ID: <87ppmbmh3i.fsf@seb.koffice.internal> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --=-=-= Content-Type: text/plain Hello, All! eshkinkot@gmail.com writes: > create function perl_test(IN data text, OUT v1 text, OUT v2 integer, OUT v3 > integer, OUT v4 json, OUT v5 json) > returns record as > $BODY$ > > use strict; > use warnings; > > my $res->{'v1'} = 'content'; > > return $res; > > $BODY$ > language plperlu volatile strict; > test case: > select count(perl_test('')) from generate_series(1, 1000000); It looks like I found the problem, Perl use reference count and something that is called "Mortal" for memory management. As I understand it, mortal is free after FREETMPS. Plperl call FREETMPS in plperl_call_perl_func() but after it, plperl ask perl interpreter again for new mortal SV variables, for example, in hek2cstr from plperl_sv_to_datum, and this new SV is newer freed. I experiment with this patch, and it fix memory leak in my case, but patch is incomplete. It does not free on error and fix only plperl_func_handler, plperl_trigger_handler and may be plperl_inline_handler must be also fixed. Patch for REL9_2_STABLE. without patch: PID VSZ RSS 2503 74112 7740 2503 152928 86860 2503 232208 165836 2503 310732 244508 2503 389264 323032 with patch: PID VSZ RSS 4322 74112 7740 4322 74380 8340 4322 74380 8340 4322 74380 8340 4322 74380 8340 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-WIP-plperl-memory-leak.patch diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c index 49d50c4..9c9874d 100644 --- a/src/pl/plperl/plperl.c +++ b/src/pl/plperl/plperl.c @@ -2173,6 +2173,9 @@ plperl_func_handler(PG_FUNCTION_ARGS) ReturnSetInfo *rsi; ErrorContextCallback pl_error_context; + ENTER; + SAVETMPS; + if (SPI_connect() != SPI_OK_CONNECT) elog(ERROR, "could not connect to SPI manager"); @@ -2271,6 +2274,9 @@ plperl_func_handler(PG_FUNCTION_ARGS) SvREFCNT_dec(perlret); + FREETMPS; + LEAVE; + return retval; } --=-=-= Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --=-=-=--