Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1arYDv-0001Df-Pg for pgsql-hackers@arkaria.postgresql.org; Sat, 16 Apr 2016 21:57:51 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1arYDu-0002t7-Sd for pgsql-hackers@arkaria.postgresql.org; Sat, 16 Apr 2016 21:57:50 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1arYCZ-0001PR-VP for pgsql-hackers@postgresql.org; Sat, 16 Apr 2016 21:56:28 +0000 Received: from mimolette.dalibo.net ([212.85.157.144] helo=mail.dalibo.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1arYCW-0006rn-Mw for pgsql-hackers@postgresql.org; Sat, 16 Apr 2016 21:56:26 +0000 Received: from [192.168.0.42] (aur75-1-78-192-238-147.fbxo.proxad.net [78.192.238.147]) by mail.dalibo.com (Postfix) with ESMTPSA id 75AD62C002E; Sat, 16 Apr 2016 23:56:22 +0200 (CEST) Subject: Re: Memory leak in GIN index build To: Tom Lane References: <571276DD.5050303@dalibo.com> <17456.1460832307@sss.pgh.pa.us> Cc: pgsql-hackers , Marc Cousin From: Julien Rouhaud Message-ID: <5712B509.7040403@dalibo.com> Date: Sat, 16 Apr 2016 23:56:25 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 MIME-Version: 1.0 In-Reply-To: <17456.1460832307@sss.pgh.pa.us> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On 16/04/2016 20:45, Tom Lane wrote: > Julien Rouhaud writes: > >> Also, in dataPlaceToPageLeaf() and ginVacuumPostingTreeLeaf(), shouldn't >> the START_CRIT_SECTION() calls be placed before the xlog code? > > Yeah, they should. Evidently somebody kluged it to avoid doing a palloc > inside a critical section, while ignoring every other rule about where to > place critical sections. (Maybe we should put an assert about being > within a critical section into XLogBeginInsert.) > After a quick test, it appears that at least log_smgrcreate() calls XLogBeginInsert() without being in a critical section, from the various DDL commands. > This code is pretty fundamentally broken, isn't it. elog() calls > inside a critical section? Really? Even worse, a MemoryContextDelete, > which hardly seems like something that should be assumed error-proof. > > Not to mention the unbelievable ugliness of a function that sometimes > returns with an open critical section (and WAL insertion started) and > sometimes doesn't. > > It kinda looks like this was hacked up without bothering to keep the > comment block in ginPlaceToPage in sync with reality, either. > > I think this needs to be redesigned so that the critical section and WAL > insertion calls all happen within a single straight-line piece of code. > > We could try making that place be ginPlaceToPage(). So far as > registerLeafRecompressWALData is concerned, that does not look that hard; > it could palloc and fill the WAL-data buffer the same as it's doing now, > then pass it back up to be registered (and eventually pfreed) in > ginPlaceToPage. However, that would also mean postponing the call of > dataPlaceToPageLeafRecompress(), which seems much messier. I assume > the data it's working from is mostly in the tmpCtx that > dataPlaceToPageLeaf wants to free before returning. Maybe we could > move creation/destruction of the tmpCtx out to ginPlaceToPage, as well? > > The other line of thought is to move the WAL work that ginPlaceToPage > does down into the subroutines. That would probably result in some > duplication of code, but it might end up being a cleaner solution. > > Anyway, I think memory leakage is just the start of what's broken here, > and fixing it won't be a very small patch. > I'm not really confident, but I'll give a try. Probably with your second solution which looks easier. Any pointer is welcome, unless someone more familiar with this code wants to take it. -- Julien Rouhaud http://dalibo.com - http://dalibo.org -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers