Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q9tcb-0006Lh-P6 for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Jun 2023 20:31:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q9tca-0008W9-Hw for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Jun 2023 20:31:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q9tca-0008T0-7u for pgsql-hackers@lists.postgresql.org; Thu, 15 Jun 2023 20:31:08 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q9tcU-002bGl-AT for pgsql-hackers@lists.postgresql.org; Thu, 15 Jun 2023 20:31:07 +0000 Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-977ed383b8aso335108966b.3 for ; Thu, 15 Jun 2023 13:31:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1686861059; x=1689453059; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=y6sn6D8khGXlhsgmSbHWbqg6BcyZjGRVy4TLAKm9ZrE=; b=eMRkMs6inYYygDHc+tyEbzZlIlGt3UJUV+EwdKxVva3kII1cv8abTG39B/NbbN3+Lc OP9zg8qqCKzenrkm/YBsNv1uhzSv7HyBo/t3FSE00aaUqcmKJ0DeBvPbUlR9Emm42YSB +j4n53fkX+1/vC9R+MfDpmCWWwQCF/17HrK3J2T8dDCSMCnLXj7aPqVNapeSG4F7scHc Yc3DVk3dsqhoLsiAEYxf8iOvq75pJQ4KITwh+rcOOCMe09r5TOKj1GBBW0Fd26pA+XtM CXtfrToH3eppYcn/3AkB45cBBMC0oIhLu2q4+t+fVULekT61+MrHP+wjJc9TzNtNYcff QnUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686861059; x=1689453059; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=y6sn6D8khGXlhsgmSbHWbqg6BcyZjGRVy4TLAKm9ZrE=; b=CJAmBlYB6dGKF8DmnT2uMhR9F4GF8+3Z3kT1No3gDdaWDW3JFvedYKA3GCqUGEHOAW +oPNaL6TKofe8d404D6i7jTx9ORaughEH0P5G8bPm4BNKKS49Ff/mcN18aivQizitFmh T06JPCZGgK09BdL7ceWLWiAS1/Ixp3F7la6uPadNPPXNQ1ZezYi0ZpADDV5KmTpBAXGc +bL5tB90GnSzm+5oiIe/ZnZsLtZ7J01MssvKzX7D6hGXjpfBuSaR4ejPY1RkJ9ajYtUr ylwjNnpLkVrM6KiwhL5YTOxK6A17H5PCE+xdDFktxF41UeYBRHDTYbekm0lNi//UilC/ W3lg== X-Gm-Message-State: AC+VfDyM9y0AEEFtWolq6jgtrnvexQv8RVXBHWmufNTZDYFb/5jbV1Os 68gmk+X3GAtgRim00QKok8M0yQ== X-Google-Smtp-Source: ACHHUZ7OyQN9KhNgMHqqmRa3i3bujzv9ncWCb36jqCZnk4LovxhP9B6WlC3c879SG17vczdNj4g4Aw== X-Received: by 2002:a17:907:62a0:b0:96f:d8a9:d045 with SMTP id nd32-20020a17090762a000b0096fd8a9d045mr116267ejc.59.1686861059295; Thu, 15 Jun 2023 13:30:59 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-228-220.bb.vodafone.cz. [86.49.228.220]) by smtp.gmail.com with ESMTPSA id rv14-20020a17090710ce00b00977c88d70c6sm9705938ejb.192.2023.06.15.13.30.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 15 Jun 2023 13:30:58 -0700 (PDT) Message-ID: <0643dbc2-53a6-37ac-b567-243a489cef0f@enterprisedb.com> Date: Thu, 15 Jun 2023 22:30:58 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: Memory leak in incremental sort re-scan To: Tom Lane Cc: Laurenz Albe , pgsql-hackers@lists.postgresql.org References: <99e584a1-2f55-a56d-efb1-d38c57eb2beb@enterprisedb.com> <2267955.1686859883@sss.pgh.pa.us> Content-Language: en-US From: Tomas Vondra In-Reply-To: <2267955.1686859883@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 6/15/23 22:11, Tom Lane wrote: > Tomas Vondra writes: >> On 6/15/23 13:48, Laurenz Albe wrote: >>> ExecIncrementalSort() calls tuplesort_begin_common(), which creates the "TupleSort main" >>> and "TupleSort sort" memory contexts, and ExecEndIncrementalSort() calls tuplesort_end(), >>> which destroys them. >>> But ExecReScanIncrementalSort() only resets the memory contexts. > >> I think it's correct, but I need to look at the code more closely - it's >> been a while. The code is a bit silly, as it resets the tuplesort and >> then throws away all the pointers - so what could the _end() break? > > The report at [1] seems to be the same issue of ExecReScanIncrementalSort > leaking memory. Funny how these reports often come in pairs ... > I applied Laurenz's fix, and that greatly reduces the > speed of leak but doesn't remove the problem entirely. It looks like > the remaining issue is that the data computed by preparePresortedCols() is > recomputed each time we rescan the node. This seems entirely gratuitous, > because there's nothing in that that could change across rescans. Yeah, I was wondering about that too when I skimmed over that code earlier today. > I see zero leakage in that example after applying the attached quick > hack. (It might be better to make the check in the caller, or to just > move the call to ExecInitIncrementalSort.) > Thanks for looking. Are you planning to work on this and push the fix, or do you want me to finish this up? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company