Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id EE4847661B8 for ; Thu, 21 Jun 2012 16:33:14 -0300 (ADT) Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Shn7w-000238-Bs for pgsql-bugs@postgresql.org; Thu, 21 Jun 2012 19:33:14 +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 q5LJWu3a000568; Thu, 21 Jun 2012 15:32:56 -0400 (EDT) From: Tom Lane To: Heikki Linnakangas cc: armando.miraglia@stud-inf.unibz.it, pgsql-bugs@postgresql.org Subject: Re: BUG #6698: sub-query with join producing out of memory in where clause In-reply-to: <4FE072E7.2010000@enterprisedb.com> References: <4FE072E7.2010000@enterprisedb.com> Comments: In-reply-to Heikki Linnakangas message dated "Tue, 19 Jun 2012 15:39:03 +0300" Date: Thu, 21 Jun 2012 15:32:56 -0400 Message-ID: <567.1340307176@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201206/193 X-Sequence-Number: 33420 Heikki Linnakangas writes: > This test case can be further reduced into: > explain analyze > SELECT * FROM generate_series(1,100000) i > WHERE (SELECT array(select repeat('a', 10000) || i) u1) is not null; > We're leaking the array constructed on each row, in ExecSetParamPlan(). > At line 1000 in nodeSubplan.c, we create a new array and store it as the > value of the PARAM_EXEC parameter. But it's never free'd. The old value > of the parameter is simply overwritten. > I'm not sure what the correct fix is. I suppose we could pfree() the old > value before overwriting it, but I'm not sure if that's safe, or if > there might still be references to the old value somewhere in the executor. I think it should be safe to pfree the prior value at this point. If you compare the code for scalar subplan results, a few lines above here, pass-by-ref param values are references into node->curTuple which gets summarily freed on the next cycle. So if anybody is keeping a pointer around then it would already be failing for non-ARRAY cases, and we've not seen any such reports. I think we can handle array results the same way as curTuple, ie, keep a link in the node's state. (I thought first about trying to pfree the prior contents of prm->value itself, but I'm less sure that that is safe --- if memory serves, the same ParamExecData slot can sometimes be used for multiple purposes.) Will give it a try ... regards, tom lane