Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZJNv7-0005ME-Rl for pgsql-bugs@arkaria.postgresql.org; Sun, 26 Jul 2015 15:32:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZJNv7-0001EM-EJ for pgsql-bugs@arkaria.postgresql.org; Sun, 26 Jul 2015 15:32:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZJNv7-0001EG-3r for pgsql-bugs@postgresql.org; Sun, 26 Jul 2015 15:32:57 +0000 Received: from mail.anarazel.de ([217.115.131.40]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1ZJNv2-0001QB-Ss for pgsql-bugs@postgresql.org; Sun, 26 Jul 2015 15:32:56 +0000 Received: by mail.anarazel.de (Postfix, from userid 108) id 8DC6B860003; Sun, 26 Jul 2015 17:32:52 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on mail X-Spam-Level: X-Spam-Status: No, score=-6.2 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.5 Received: from anarazel.de (p5DDC4D3B.dip0.t-ipconnect.de [93.220.77.59]) (Authenticated sender: andres@anarazel.de) by mail.anarazel.de (Postfix) with ESMTPSA id 50D06860002; Sun, 26 Jul 2015 17:32:49 +0200 (CEST) Received: by anarazel.de (Postfix, from userid 1000) id CA8F710105B; Sun, 26 Jul 2015 17:32:48 +0200 (CEST) Date: Sun, 26 Jul 2015 17:32:48 +0200 From: Andres Freund To: Michael Paquier Cc: PostgreSQL mailing lists , Heikki Linnakangas Subject: Re: Memory leak in XLOG reader facility when decoding records (caused by WAL refactoring) Message-ID: <20150726153248.GF12755@awork2.anarazel.de> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Pg-Spam-Score: -4.2 (----) 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 On 2015-07-23 22:50:12 +0900, Michael Paquier wrote: > > While running valgring on pg_rewind, I have noticed that each new call > to XLogReadRecord leaks XLogReaderState->main_data and > XLogReaderState->blocks[block_id].data because each one of them is > palloc'd to store respectively the main data of the record and the > data attached to each block. I think that the most correct fix to > prevent the leak is to free those pointers when calling ResetDecoder() > which is used to reset a reader state between two records. I don't think that'd be a good fix. Isn't the idea here that we only allocate new buffers when the previous one is too small? Your fix will greatly increase the memory management overhead. > ==46805== 154 bytes in 1 blocks are definitely lost in loss record 103 of 167 > ==46805== at 0x6B1B: malloc (in > /usr/local/Cellar/valgrind/3.10.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so) > ==46805== by 0x10000DB2D: pg_malloc_internal (fe_memutils.c:30) > ==46805== by 0x10000DE79: palloc (fe_memutils.c:118) > ==46805== by 0x1000052A0: DecodeXLogRecord (xlogreader.c:1203) > ==46805== by 0x100003AA9: XLogReadRecord (xlogreader.c:461) > ==46805== by 0x1000022F0: extractPageMap (parsexlog.c:78) > ==46805== by 0x10000188C: main (pg_rewind.c:280) Uh. This is the leak? if (state->main_data_len > 0) { if (!state->main_data || state->main_data_len > state->main_data_bufsz) { if (state->main_data) pfree(state->main_data); state->main_data_bufsz = state->main_data_len; state->main_data = palloc(state->main_data_bufsz); <--- here } memcpy(state->main_data, ptr, state->main_data_len); ptr += state->main_data_len; } Where/When are we leaking memory here? The previously used memory is freed before we allocate a larger buffer. Greetings, Andres Freund -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs