Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YGtN9-0000ee-RY for pgsql-bugs@arkaria.postgresql.org; Thu, 29 Jan 2015 17:59:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YGtN9-0007Bn-A4 for pgsql-bugs@arkaria.postgresql.org; Thu, 29 Jan 2015 17:59:19 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YGtN7-00078B-Tj for pgsql-bugs@postgresql.org; Thu, 29 Jan 2015 17:59:18 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YGtMz-0000EY-Oo for pgsql-bugs@postgresql.org; Thu, 29 Jan 2015 17:59:15 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id AA73828B17; Thu, 29 Jan 2015 09:59:07 -0800 (PST) Received: from EX13-CAS-010.vmware.com (EX13-CAS-010.vmware.com [10.113.191.62]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id A5328408BF; Thu, 29 Jan 2015 09:59:07 -0800 (PST) Received: from [192.168.42.220] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.913.22; Thu, 29 Jan 2015 09:59:06 -0800 Message-ID: <54CA74D3.3090102@vmware.com> Date: Thu, 29 Jan 2015 19:58:43 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.3.0 MIME-Version: 1.0 To: Tom Lane CC: Michael Paquier , , Subject: Re: BUG #12694: crash if the number of result rows is lower than gin_fuzzy_search_limit References: <20150128181243.2579.80775@wrigleys.postgresql.org> <54CA3EB5.3030804@vmware.com> <4773.1422545177@sss.pgh.pa.us> <54CA57B9.3070602@vmware.com> <6517.1422547480@sss.pgh.pa.us> In-Reply-To: <6517.1422547480@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------020509060906060008010908" X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -6.9 (------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --------------020509060906060008010908 Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit On 01/29/2015 06:04 PM, Tom Lane wrote: > Heikki Linnakangas writes: >> On 01/29/2015 05:26 PM, Tom Lane wrote: >> I'm not sure if >> it would actually be better that way though; it's not clear to me how >> the fuzzy search limit should interact with the fast scan code. > > Yeah, it might be better but it's not very clear what the implications > are. Probably shouldn't touch that as part of an emergency bug fix. Committed and backpatched a minimal fix. For master and 9.4, I'm thinking of applying the attached. It makes it clear that startScan() is not used to re-start a scan with existing scan keys, but is always called on a newly initialized scan keys. It also plugs the obvious leaking of the arrays. I'll look at the other memory leaks separately, but this seems appropriate for 9.4. - Heikki --------------020509060906060008010908 Content-Type: text/x-diff; name="0001-Fix-query-duration-memory-leak-with-GIN-rescans.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-Fix-query-duration-memory-leak-with-GIN-rescans.patch" From bdfee2394844bbbbbff3e7f7884a3ca83f2f0939 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Thu, 29 Jan 2015 15:48:02 +0200 Subject: [PATCH] Fix query-duration memory leak with GIN rescans. The requiredEntries / additionalEntries arrays were not freed in freeScanKeys() like other per-key stuff. It's not obvious, but startScanKey() was only ever called after the keys have been initialized with ginNewScanKey(). That's why it doesn't need to worry about freeing existing arrays. The ginIsNewKey() test in gingetbitmap was never true, because ginrescan free's the existing keys, and it's not OK to call gingetbitmap twice in a row without calling ginrescan in between. To make that clear, remove the unnecessary ginIsNewKey(). And just to be extra sure that nothing funny happens if there is an existing key after all, call freeScanKeys() to free it if it exists. This makes the code more straightforward. (I'm seeing other similar leaks in testing a query that rescans an GIN index scan, but that's a different issue. This just fixes the obvious leak with those two arrays.) diff --git a/src/backend/access/gin/ginget.c b/src/backend/access/gin/ginget.c index 57d378a..d85191f 100644 --- a/src/backend/access/gin/ginget.c +++ b/src/backend/access/gin/ginget.c @@ -560,6 +560,10 @@ startScan(IndexScanDesc scan) } } + /* + * Now that we have the estimates for the entry frequencies, finish + * initializing the scan keys. + */ for (i = 0; i < so->nkeys; i++) startScanKey(ginstate, so, so->keys + i); } @@ -1763,13 +1767,13 @@ scanPendingInsert(IndexScanDesc scan, TIDBitmap *tbm, int64 *ntids) } -#define GinIsNewKey(s) ( ((GinScanOpaque) scan->opaque)->keys == NULL ) #define GinIsVoidRes(s) ( ((GinScanOpaque) scan->opaque)->isVoidRes ) Datum gingetbitmap(PG_FUNCTION_ARGS) { IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0); + GinScanOpaque so = (GinScanOpaque) scan->opaque; TIDBitmap *tbm = (TIDBitmap *) PG_GETARG_POINTER(1); int64 ntids; ItemPointerData iptr; @@ -1778,8 +1782,8 @@ gingetbitmap(PG_FUNCTION_ARGS) /* * Set up the scan keys, and check for unsatisfiable query. */ - if (GinIsNewKey(scan)) - ginNewScanKey(scan); + ginFreeScanKeys(so); /* there should be no keys yet, but just to be sure */ + ginNewScanKey(scan); if (GinIsVoidRes(scan)) PG_RETURN_INT64(0); diff --git a/src/backend/access/gin/ginscan.c b/src/backend/access/gin/ginscan.c index 7ab795c..b3a2de1 100644 --- a/src/backend/access/gin/ginscan.c +++ b/src/backend/access/gin/ginscan.c @@ -163,6 +163,10 @@ ginFillScanKey(GinScanOpaque so, OffsetNumber attnum, key->curItemMatches = false; key->recheckCurItem = false; key->isFinished = false; + key->nrequired = 0; + key->nadditional = 0; + key->requiredEntries = NULL; + key->additionalEntries = NULL; ginInitConsistentFunction(ginstate, key); @@ -223,8 +227,8 @@ ginFillScanKey(GinScanOpaque so, OffsetNumber attnum, } } -static void -freeScanKeys(GinScanOpaque so) +void +ginFreeScanKeys(GinScanOpaque so) { uint32 i; @@ -237,6 +241,10 @@ freeScanKeys(GinScanOpaque so) pfree(key->scanEntry); pfree(key->entryRes); + if (key->requiredEntries) + pfree(key->requiredEntries); + if (key->additionalEntries) + pfree(key->additionalEntries); } pfree(so->keys); @@ -416,7 +424,7 @@ ginrescan(PG_FUNCTION_ARGS) /* remaining arguments are ignored */ GinScanOpaque so = (GinScanOpaque) scan->opaque; - freeScanKeys(so); + ginFreeScanKeys(so); if (scankey && scan->numberOfKeys > 0) { @@ -434,7 +442,7 @@ ginendscan(PG_FUNCTION_ARGS) IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0); GinScanOpaque so = (GinScanOpaque) scan->opaque; - freeScanKeys(so); + ginFreeScanKeys(so); MemoryContextDelete(so->tempCtx); diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h index cf2ef80..c949c97 100644 --- a/src/include/access/gin_private.h +++ b/src/include/access/gin_private.h @@ -899,6 +899,7 @@ extern Datum ginrescan(PG_FUNCTION_ARGS); extern Datum ginmarkpos(PG_FUNCTION_ARGS); extern Datum ginrestrpos(PG_FUNCTION_ARGS); extern void ginNewScanKey(IndexScanDesc scan); +extern void ginFreeScanKeys(GinScanOpaque so); /* ginget.c */ extern Datum gingetbitmap(PG_FUNCTION_ARGS); -- 2.1.4 --------------020509060906060008010908 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------020509060906060008010908--