Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZJmHN-0006oE-IJ for pgsql-bugs@arkaria.postgresql.org; Mon, 27 Jul 2015 17:33:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZJmHN-0001tM-2P for pgsql-bugs@arkaria.postgresql.org; Mon, 27 Jul 2015 17:33:33 +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 1ZJmGk-0001BU-Cq for pgsql-bugs@postgresql.org; Mon, 27 Jul 2015 17:32:54 +0000 Received: from mail-wi0-x231.google.com ([2a00:1450:400c:c05::231]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZJmGc-0006QY-NM for pgsql-bugs@postgresql.org; Mon, 27 Jul 2015 17:32:53 +0000 Received: by wicgb10 with SMTP id gb10so122360103wic.1 for ; Mon, 27 Jul 2015 10:32:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=4Ts5V6PFVds/Cdl5buGKhcsgFIrX+CuHfB6paT2ruu8=; b=JNQViPA5YgZNsmB97ej8MTcLKmnYWOOqGlwO2oG7dbnv5jBgFm74yCvF2SBXDRPIoI 8cmi3h010Om4rMsQQXEWrbsMbinI/pz3zaubleUlXZ5J3AnPBbs7VVVB0+GxqY3aP3So f0WiriR0WSlO/uwo2/1/txqyCAMBu/P+FNF29tMUKIskJrPTpJ8kkhoSmm3G8DiZQzGD /RhjJxQeyTf8vfZdQ/HwLlEnhpZLhramKG1GrjiD/VkEMRzzjM03Gh2127J7kJIfLlIW uOm6ef15M0onMlIklppgAFsrmk5/d0ZVKneDoZurYePW+H/oo+FzHlujUkPCgXtLgyLG 5NIA== X-Received: by 10.194.88.69 with SMTP id be5mr56441476wjb.149.1438018365139; Mon, 27 Jul 2015 10:32:45 -0700 (PDT) Received: from [192.168.1.105] (dsl-hkibrasgw1-58c38f-82.dhcp.inet.fi. [88.195.143.82]) by smtp.gmail.com with ESMTPSA id bg6sm28971151wjc.13.2015.07.27.10.32.43 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Jul 2015 10:32:44 -0700 (PDT) Message-ID: <55B66B3A.3070906@iki.fi> Date: Mon, 27 Jul 2015 20:32:42 +0300 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.7.0 MIME-Version: 1.0 To: Michael Paquier , Andres Freund CC: PostgreSQL mailing lists Subject: Re: Memory leak in XLOG reader facility when decoding records (caused by WAL refactoring) References: <20150726153248.GF12755@awork2.anarazel.de> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.4 (--) 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 07/27/2015 04:40 AM, Michael Paquier wrote: > On Mon, Jul 27, 2015 at 12:32 AM, Andres Freund wrote: >> 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. > > OK. > >>> ==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)g >> 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. > > Yep, it is here, and it is not related to the calls of XLogReadRecord > actually, that's a problem in XLogReaderFree that relies on max_block_id, > which gets reinitialized to -1 between two records in ResetDecoder, instead > of XLR_MAX_BLOCK_ID to clean up the data blocks when freeing the XLOG > reader (note: looking at memory leaks at night can lead to incorrect > conclusions). So for example if a first record uses some data blocks and > allocates buffers for some data, and then a second record uses no data > blocks, we'll leak. So that's less critical than I though as the couple > XLofReaderAllocate/XLogReaderFree is called only once in StartupXLOG, still > that's a leak. > > Attached is a self-contained script able to show the problem if anybody > wants to check by himself (the other leaks related to pg_rewind are > addressed in another thread), and a patch that takes care of it. Thanks, committed. The leak was not actually in the "main_data" handling, as quoted in the above snippet, but in the per-block buffers. - Heikki -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs