Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id DDDF36FED3 for ; Tue, 19 Jun 2012 09:39:34 -0300 (ADT) Received: from exprod7og125.obsmtp.com ([64.18.2.28]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SgxiN-0006UZ-QN for pgsql-bugs@postgresql.org; Tue, 19 Jun 2012 12:39:33 +0000 Received: from mail-lb0-f173.google.com ([209.85.217.173]) (using TLSv1) by exprod7ob125.postini.com ([64.18.6.12]) with SMTP ID DSNKT+By69giavpi+aXbt4F0B268YBs5CJAu@postini.com; Tue, 19 Jun 2012 05:39:23 PDT Received: by lbok6 with SMTP id k6so887188lbo.32 for ; Tue, 19 Jun 2012 05:39:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=2VbNvsti1jXsLv9HNG8eAblHxWcC4UqCMefVZp0U2eI=; b=oV1Bh4Z6QQYGknbPYW+Rb4f/3dOzCKugIjyw9FCm6ojMB/hn3eW1yIQVlRM9sWxX9Y Y+A0whZkIlstbbdX/ioWRHa87Gs0KTRkKg68M3MLQXEyFDbNzEoOzo8sePCRUQPEusVO JmPq6npQ7TE6/Y8LcQXj9ltQ6ZfaZUHonoFTqkOYorbouP1bgaWlg09taoSZyNkXZyUX DqeLesuwAW/0tl8FyQ17cljfoJUhq0HDp2iF4vrNtI7p+9kv8vh291gXzXBtoO10QEyw +9f7Kxjw7dPOmETDUFg8Bl7kNBuDJUnU7G/nE0IDck7upEwoFe6Y3DbjOW/frUvCbfze deAA== Received: by 10.152.144.168 with SMTP id sn8mr18320555lab.1.1340109546027; Tue, 19 Jun 2012 05:39:06 -0700 (PDT) Received: from [192.168.1.100] (dsl-hkibrasgw2-ff6cc300-200.dhcp.inet.fi. [88.195.108.200]) by mx.google.com with ESMTPS id fd1sm13753748lbb.7.2012.06.19.05.39.04 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 19 Jun 2012 05:39:05 -0700 (PDT) Message-ID: <4FE072E7.2010000@enterprisedb.com> Date: Tue, 19 Jun 2012 15:39:03 +0300 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.4) Gecko/20120510 Icedove/10.0.4 MIME-Version: 1.0 To: armando.miraglia@stud-inf.unibz.it CC: pgsql-bugs@postgresql.org Subject: Re: BUG #6698: sub-query with join producing out of memory in where clause References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQldpuE4ZUC6pLxJ0q2+/YyezWL0rmuyFsBVAKtYjOtNT1aiwqf7lo6ZHI0LvfQqsQfFol0J X-Pg-Spam-Score: -4.2 (----) X-Archive-Number: 201206/160 X-Sequence-Number: 33387 On 19.06.2012 04:01, armando.miraglia@stud-inf.unibz.it wrote: > The following bug has been logged on the website: > > Bug reference: 6698 > Logged by: Armando Miraglia > Email address: armando.miraglia@stud-inf.unibz.it > PostgreSQL version: 9.1.2 > Operating system: Arch Linux/Ubuntu > Description: > > Hi everybody! > > Fact: while I was trying to produce a c-function I got an OOM which > RhodiumToad helped me to debug. The OOM is reproducible with also standard > query. > > Environment: I tested the POC using 9.1.2 but also 9.2devel compiled > "by-hand" > > Reproducibility: > - limit the memory usage > ulimit -S -v 500000 > - start postgresql > postgres -D ../data.ascii/ > > - run the following query from psql > SELECT * > > FROM generate_series(1,1000000) i > > WHERE 100<= (SELECT COUNT(*) > > FROM unnest(array(select j from > generate_series(i-100,i+100) j)) u1 > JOIN > > unnest(array(select j from > generate_series(i-100,i+100) j)) u2 > ON (u1.u1=u2.u2)); > > Error: > - psql side: > ERROR: out of memory > DETAIL: Failed on request of size 828. > - server side: > ... > PortalMemory: 8192 total in 1 blocks; 7888 free (0 chunks); 304 used > PortalHeapMemory: 1024 total in 1 blocks; 824 free (0 chunks); 200 used > ExecutorState: 458358928 total in 67 blocks; 794136 free (15965 > chunks); 457564792 used > accumArrayResult: 8192 total in 1 blocks; 5744 free (4 chunks); 2448 > used > HashTableContext: 0 total in 0 blocks; 0 free (0 chunks); 0 used > HashBatchContext: 32768 total in 2 blocks; 8416 free (1 chunks); > 24352 used > ... 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. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com