Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TAp6s-0004FT-TV for pgsql-bugs@postgresql.org; Sun, 09 Sep 2012 21:32:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TAp6q-0005Zo-HT for pgsql-bugs@postgresql.org; Sun, 09 Sep 2012 21:32:06 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id q89LW0Y2016155; Sun, 9 Sep 2012 17:32:01 -0400 (EDT) From: Tom Lane To: Marko Tiikkaja cc: pgsql-bugs@postgresql.org Subject: Re: BUG #7516: PL/Perl crash In-reply-to: <5047BA9C.2040405@joh.to> References: <1194.1346688396@sss.pgh.pa.us> <5047BA9C.2040405@joh.to> Comments: In-reply-to Marko Tiikkaja message dated "Wed, 05 Sep 2012 22:48:28 +0200" Date: Sun, 09 Sep 2012 17:32:00 -0400 Message-ID: <16153.1347226320@sss.pgh.pa.us> X-Pg-Spam-Score: -2.3 (--) X-Archive-Number: 201209/76 X-Sequence-Number: 34071 Marko Tiikkaja writes: > On 03/09/2012 18:06, Tom Lane wrote: >> Hm. Is it possible that the prepared statement recursively called the >> *same* plperl function? And that somebody did a CREATE OR REPLACE on >> that function meanwhile? > No, that doesn't seem possible in this case. The function calls some > prepared statements repeatedly, but no recursion should occur. Hm. Well, I can definitely reproduce a segfault using this example: CREATE OR REPLACE FUNCTION self_modify(INTEGER) RETURNS INTEGER AS $$ spi_exec_query('CREATE OR REPLACE FUNCTION self_modify(INTEGER) RETURNS INTEGER AS \'return $_[0] * 3;\' LANGUAGE plperl;'); spi_exec_query('select self_modify(42) AS a'); return $_[0] * 2; $$ LANGUAGE plperl; select self_modify(42); The crash is 100% reproducible if you tweak plperl like this: diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c index bfa805d..307874c 100644 --- a/src/pl/plperl/plperl.c +++ b/src/pl/plperl/plperl.c @@ -2393,7 +2393,7 @@ validate_plperl_function(plperl_proc_ptr *proc_ptr, HeapTu activate_interpreter(oldinterp); } free(prodesc->proname); - free(prodesc); + memset(prodesc, 0, sizeof(plperl_proc_desc)); } return false; Without that it's probabilistic, since the subsequent malloc in compile_plperl_function is likely to realloc the exact same space. So I think we need to institute a reference counting scheme for the plperl_proc_desc records ... regards, tom lane