pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #9223: plperlu result memory leak
9+ messages / 4 participants
[nested] [flat]
* BUG #9223: plperlu result memory leak
@ 2014-02-14 16:31 eshkinkot@gmail.com
0 siblings, 1 reply; 9+ messages in thread
From: eshkinkot@gmail.com @ 2014-02-14 16:31 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 9223
Logged by: Sergey Burladyan
Email address: eshkinkot@gmail.com
PostgreSQL version: 9.2.6
Operating system: Debian testing
Description:
PostgreSQL 9.2.6 on x86_64-unknown-linux-gnu, compiled by gcc (Debian
4.8.2-10) 4.8.2, 64-bit
This is perl 5, version 18, subversion 2 (v5.18.2) built for
x86_64-linux-gnu-thread-multi
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);
before:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
sergey 14771 0.0 0.0 1127204 6916 ? Ss 20:16 0:00 postgres:
sergey sergey 127.0.0.1(60492) idle
after first run:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
sergey 14771 20.5 1.0 1216824 88308 ? Ss 20:16 0:08 postgres:
sergey sergey 127.0.0.1(60492) idle
after second run:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
sergey 14771 9.1 2.8 1360876 229488 ? Ss 20:16 0:16 postgres:
sergey sergey 127.0.0.1(60492) idle
similar plpgsql function does not create a memory leak:
create function plpgsql_test(IN data text, OUT v1 text, OUT v2 integer, OUT
v3 integer, OUT v4 json, OUT v5 json)
returns record as
$BODY$
begin
v1 := 'content';
end
$BODY$
language plpgsql volatile strict;
select count(plpgsql_test('')) from generate_series(1, 1000000);
first run, before:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
sergey 14836 0.0 0.0 1126448 4064 ? Ss 20:21 0:00 postgres:
sergey sergey 127.0.0.1(60577) idle
after first run:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
sergey 14836 5.4 0.0 1128924 5636 ? Ss 20:21 0:05 postgres:
sergey sergey 127.0.0.1(60577) idle
after second run:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
sergey 14836 7.8 0.1 1131292 8108 ? Ss 20:21 0:10 postgres:
sergey sergey 127.0.0.1(60577) idle
after third run:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
sergey 14836 9.1 0.1 1131292 8108 ? Ss 20:21 0:15 postgres:
sergey sergey 127.0.0.1(60577) idle
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [BUGS] BUG #9223: plperlu result memory leak
@ 2014-02-25 13:56 Sergey Burladyan <eshkinkot@gmail.com>
parent: eshkinkot@gmail.com
0 siblings, 1 reply; 9+ messages in thread
From: Sergey Burladyan @ 2014-02-25 13:56 UTC (permalink / raw)
To: pgsql-bugs; +Cc: pgsql-hackers@postgresql.org
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
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Attachments:
0001-WIP-plperl-memory-leak.patch (text/x-diff, 523B) download
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;
}
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #9223: plperlu result memory leak
@ 2014-02-25 20:46 Alex Hunsaker <badalex@gmail.com>
parent: Sergey Burladyan <eshkinkot@gmail.com>
0 siblings, 2 replies; 9+ messages in thread
From: Alex Hunsaker @ 2014-02-25 20:46 UTC (permalink / raw)
To: Sergey Burladyan <eshkinkot@gmail.com>; +Cc: pgsql-bugs; PostgreSQL-development <pgsql-hackers@postgresql.org>
On Tue, Feb 25, 2014 at 6:56 AM, Sergey Burladyan <eshkinkot@gmail.com> wrote:
> 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.
So I think hek2cstr is the only place we leak (its the only place I
can see that allocates a mortal sv without being wrapped in
ENTER/SAVETMPS/FREETMPS/LEAVE).
Does the attached fix it for you?
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
plperl_hek2cstr_leak.patch (text/x-patch, 923B) download
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c
index 930b9f0..3bc4034 100644
--- a/src/pl/plperl/plperl.c
+++ b/src/pl/plperl/plperl.c
@@ -304,6 +304,16 @@ static char *setlocale_perl(int category, char *locale);
static char *
hek2cstr(HE *he)
{
+ char *ret;
+ SV *sv;
+
+ /*
+ * HeSVKEY_force will return a temporary mortal SV*, so we need to make
+ * sure to free it with ENTER/SAVE/FREE/LEAVE
+ */
+ ENTER;
+ SAVETMPS;
+
/*-------------------------
* Unfortunately, while HeUTF8 is true for most things > 256, for values
* 128..255 it's not, but perl will treat them as unicode code points if
@@ -328,11 +338,17 @@ hek2cstr(HE *he)
* right thing
*-------------------------
*/
- SV *sv = HeSVKEY_force(he);
+ sv = HeSVKEY_force(he);
if (HeUTF8(he))
SvUTF8_on(sv);
- return sv2cstr(sv);
+ ret = sv2cstr(sv);
+
+ /* free sv */
+ FREETMPS;
+ LEAVE;
+
+ return ret;
}
/*
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #9223: plperlu result memory leak
@ 2014-02-26 08:58 Sergey Burladyan <eshkinkot@gmail.com>
parent: Alex Hunsaker <badalex@gmail.com>
1 sibling, 0 replies; 9+ messages in thread
From: Sergey Burladyan @ 2014-02-26 08:58 UTC (permalink / raw)
To: Alex Hunsaker <badalex@gmail.com>; +Cc: pgsql-bugs; PostgreSQL-development <pgsql-hackers@postgresql.org>
Alex Hunsaker <badalex@gmail.com> writes:
> On Tue, Feb 25, 2014 at 6:56 AM, Sergey Burladyan <eshkinkot@gmail.com> wrote:
>
> > 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.
>
> So I think hek2cstr is the only place we leak (its the only place I
> can see that allocates a mortal sv without being wrapped in
> ENTER/SAVETMPS/FREETMPS/LEAVE).
Yeah, I also try to fix only hek2cstr, but failed.
> Does the attached fix it for you?
Yes, your patch is fix it for me, thank you, Alex!
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #9223: plperlu result memory leak
@ 2014-03-05 19:22 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Alex Hunsaker <badalex@gmail.com>
1 sibling, 1 reply; 9+ messages in thread
From: Alvaro Herrera @ 2014-03-05 19:22 UTC (permalink / raw)
To: Alex Hunsaker <badalex@gmail.com>; +Cc: Sergey Burladyan <eshkinkot@gmail.com>; pgsql-bugs; PostgreSQL-development <pgsql-hackers@postgresql.org>
Alex Hunsaker escribió:
> On Tue, Feb 25, 2014 at 6:56 AM, Sergey Burladyan <eshkinkot@gmail.com> wrote:
>
> > 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.
>
> So I think hek2cstr is the only place we leak (its the only place I
> can see that allocates a mortal sv without being wrapped in
> ENTER/SAVETMPS/FREETMPS/LEAVE).
>
> Does the attached fix it for you?
Can I bug you into verifying what supported releases need this patch,
and to which does it backpatch cleanly? And if there's any to which it
doesn't, can I further bug you into providing one that does?
Thanks,
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [BUGS] BUG #9223: plperlu result memory leak
@ 2014-03-05 19:55 Alex Hunsaker <badalex@gmail.com>
parent: Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 1 reply; 9+ messages in thread
From: Alex Hunsaker @ 2014-03-05 19:55 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Sergey Burladyan <eshkinkot@gmail.com>; pgsql-bugs; PostgreSQL-development <pgsql-hackers@postgresql.org>
On Wed, Mar 5, 2014 at 12:22 PM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:
> Can I bug you into verifying what supported releases need this patch,
> and to which does it backpatch cleanly? And if there's any to which it
> doesn't, can I further bug you into providing one that does?
Sure! Not bugging at all. I'll dig into this in a few hours.
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [BUGS] BUG #9223: plperlu result memory leak
@ 2014-03-06 02:59 Alex Hunsaker <badalex@gmail.com>
parent: Alex Hunsaker <badalex@gmail.com>
0 siblings, 2 replies; 9+ messages in thread
From: Alex Hunsaker @ 2014-03-06 02:59 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Sergey Burladyan <eshkinkot@gmail.com>; pgsql-bugs; PostgreSQL-development <pgsql-hackers@postgresql.org>
On Wed, Mar 5, 2014 at 12:55 PM, Alex Hunsaker <badalex@gmail.com> wrote:
> On Wed, Mar 5, 2014 at 12:22 PM, Alvaro Herrera
> <alvherre@2ndquadrant.com> wrote:
>
>> Can I bug you into verifying what supported releases need this patch,
>> and to which does it backpatch cleanly? And if there's any to which it
>> doesn't, can I further bug you into providing one that does?
>
> Sure! Not bugging at all. I'll dig into this in a few hours.
This will apply cleanly all the way to REL9_2_STABLE. It applies (with
fuzz, but cleanly to REL9_1). REL9_0 does this completely differently
and so does not have this leak.
Thanks!
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #9223: plperlu result memory leak
@ 2014-03-15 20:57 Sergey Burladyan <eshkinkot@gmail.com>
parent: Alex Hunsaker <badalex@gmail.com>
1 sibling, 0 replies; 9+ messages in thread
From: Sergey Burladyan @ 2014-03-15 20:57 UTC (permalink / raw)
To: Alex Hunsaker <badalex@gmail.com>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; pgsql-bugs; PostgreSQL-development <pgsql-hackers@postgresql.org>
Hi!
On Thu, Mar 6, 2014 at 6:59 AM, Alex Hunsaker <badalex@gmail.com> wrote:
. . .
> This will apply cleanly all the way to REL9_2_STABLE. It applies (with
> fuzz, but cleanly to REL9_1). REL9_0 does this completely differently
> and so does not have this leak.
>
Looks like patch still not pushed to repo.
--
Sergey Burladyan
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [BUGS] BUG #9223: plperlu result memory leak
@ 2014-03-17 02:30 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Alex Hunsaker <badalex@gmail.com>
1 sibling, 0 replies; 9+ messages in thread
From: Alvaro Herrera @ 2014-03-17 02:30 UTC (permalink / raw)
To: Alex Hunsaker <badalex@gmail.com>; +Cc: Sergey Burladyan <eshkinkot@gmail.com>; pgsql-bugs; PostgreSQL-development <pgsql-hackers@postgresql.org>
Alex Hunsaker escribió:
> On Wed, Mar 5, 2014 at 12:55 PM, Alex Hunsaker <badalex@gmail.com> wrote:
> > On Wed, Mar 5, 2014 at 12:22 PM, Alvaro Herrera
> > <alvherre@2ndquadrant.com> wrote:
> >
> >> Can I bug you into verifying what supported releases need this patch,
> >> and to which does it backpatch cleanly? And if there's any to which it
> >> doesn't, can I further bug you into providing one that does?
> >
> > Sure! Not bugging at all. I'll dig into this in a few hours.
>
> This will apply cleanly all the way to REL9_2_STABLE. It applies (with
> fuzz, but cleanly to REL9_1). REL9_0 does this completely differently
> and so does not have this leak.
Excellent, thanks. I verified all these assertions and then pushed.
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 9+ messages in thread
end of thread, other threads:[~2014-03-17 02:30 UTC | newest]
Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-02-14 16:31 BUG #9223: plperlu result memory leak eshkinkot@gmail.com
2014-02-25 13:56 ` Sergey Burladyan <eshkinkot@gmail.com>
2014-02-25 20:46 ` Alex Hunsaker <badalex@gmail.com>
2014-02-26 08:58 ` Sergey Burladyan <eshkinkot@gmail.com>
2014-03-05 19:22 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2014-03-05 19:55 ` Alex Hunsaker <badalex@gmail.com>
2014-03-06 02:59 ` Alex Hunsaker <badalex@gmail.com>
2014-03-15 20:57 ` Sergey Burladyan <eshkinkot@gmail.com>
2014-03-17 02:30 ` Alvaro Herrera <alvherre@2ndquadrant.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox