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 1lmbeR-0001RA-Hb for pgsql-hackers@arkaria.postgresql.org; Fri, 28 May 2021 12:31:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lmbeQ-0005xp-8S for pgsql-hackers@arkaria.postgresql.org; Fri, 28 May 2021 12:31:42 +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 1lmbeQ-0005xh-0b for pgsql-hackers@lists.postgresql.org; Fri, 28 May 2021 12:31:42 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lmbeL-0006cw-Hr for pgsql-hackers@lists.postgresql.org; Fri, 28 May 2021 12:31:41 +0000 Received: by mail-ed1-x52a.google.com with SMTP id r11so4620516edt.13 for ; Fri, 28 May 2021 05:31:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=BTAXiN+lyjpMol+PiBVda488F+1qb7AJ24+3LMPdvwY=; b=gjzbP5JFUMzlOIs8OVBa3stnHKOiRdxREIGeJ9W9xeiZdBcm1rLevIaHi8Rp4ihcAC fVMAcuZQ5Iqyv2gaO1N7YBmnIfBhs0sbiERW1ocaT87HotnXodiRNm0ctRqvfn6oohWG vO5r+V9/WlMEXtxHomfq2TEB7xys+7Jb3XUrL5MDxJD4uaj7rvDPR0shoV83bqFLp3Fu UoXK00R4xZKwQ1wLJbRRTU//pAreQte5XnX1/jrJ2aI9pQKa0ZcBBvGMmXu0OdDbmLdg o7zqmjfrG9c00TlAEAps+7F+LJE8puL+WqzlCLz++g5fPt/FpD1eVFRjbBefAwOPLl2t vP4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=BTAXiN+lyjpMol+PiBVda488F+1qb7AJ24+3LMPdvwY=; b=fgY2XJmqE1sP/ePLGpp6rmXfAJik6V2w/9KswxxdAvbBbxrqMCwCJ1AM0SlcS3EZKb YJJx/Zj4OJaFoI4E5CbvVUFIwfgPk+AjutQiSztpuDpPYJHckTBcEp5Tmxm7My1Isinz rxw50KU7azoskc5fFiUhh0O1Do5l9rj64RbNj1zTav9cRwLmlN02Y9vMIXkN2qD+WbWq /oiUaF7mSpSbswZVR0ttGqtL6VfSo4sU7Plm4vhGh96u+AMI358X/AD1XtxvR3w7R8Gn Ku/THCetg9ra60DMFTtBt2wAdsw1wwKmAwLS55tolfu2hgZ10ThQL26imQSj1dHi3w9h epiA== X-Gm-Message-State: AOAM530tyIXpW5EuGTipiTJl921Ygp3QG8V1vG9qdxdHsFamsfv4kuOX ONJPw3GsBpF7jKGjbvRFurytLxxBSuWRavjV9hL7R++MQcTAvl4JD1LCim7wJJISdagRdd5lCrJ w9sY/B2PucUbFxTNE77XphmMhjd9I8I1NdL1734j32Ut9gIV9rEewCtkr/9PlnHrQjt6MgZ6Mr9 ePpVfqRDrb2uXtsFa8QhbrYDIFdHZo9EfhVoPflVdCo7Nea6/RvZvi X-Google-Smtp-Source: ABdhPJwYUXQtgtnbSi27BlhAu5QkIa1H0o8bNPV5Hf5OuqIo3qw6io13y5fvC36dnEn9Wkwr6etuFg== X-Received: by 2002:a05:6402:111a:: with SMTP id u26mr9352300edv.260.1622205096539; Fri, 28 May 2021 05:31:36 -0700 (PDT) Received: from [10.137.0.19] (ip-86-49-254-183.net.upcbroadband.cz. [86.49.254.183]) by smtp.gmail.com with ESMTPSA id m12sm2681225edc.40.2021.05.28.05.31.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 May 2021 05:31:35 -0700 (PDT) Subject: Re: Decoding speculative insert with toast leaks memory To: Dilip Kumar Cc: Amit Kapila , Ashutosh Bapat , PostgreSQL Hackers , Andres Freund , Petr Jelinek , Peter Eisentraut References: From: Tomas Vondra Message-ID: Date: Fri, 28 May 2021 14:31:33 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 5/28/21 2:17 PM, Dilip Kumar wrote: > On Fri, May 28, 2021 at 5:16 PM Tomas Vondra > wrote: >> On 5/27/21 6:36 AM, Dilip Kumar wrote: >>> On Thu, May 27, 2021 at 9:47 AM Amit Kapila wrote: >>>> >>>> On Thu, May 27, 2021 at 9:40 AM Dilip Kumar wrote: >>>> >>>> True, but if you do this clean-up in ReorderBufferCleanupTXN then you >>>> don't need to take care at separate places. Also, toast_hash is stored >>>> in txn so it appears natural to clean it up in while releasing TXN. >>> >>> Make sense, basically, IMHO we will have to do in TruncateTXN and >>> ReturnTXN as attached? >>> >> >> Yeah, I've been working on a fix over the last couple days (because of a >> customer issue), and I ended up with the reset in ReorderBufferReturnTXN >> too - it does solve the issue in the case I've been investigating. >> >> I'm not sure the reset in ReorderBufferTruncateTXN is correct, though. >> Isn't it possible that we'll need the TOAST data after streaming part of >> the transaction? After all, we're not resetting invalidations, tuplecids >> and snapshot either > > Actually, as per the current design, we don't need the toast data > after the streaming. Because currently, we don't allow to stream the > transaction if we need to keep the toast across stream e.g. if we only > have toast insert without the main insert we assure this as partial > changes, another case is if we have multi-insert with toast then we > keep the transaction as mark partial until we get the last insert of > the multi-insert. So with the current design we don't have any case > where we need to keep toast data across streams. > > ... And we'll eventually clean it after the streamed >> transaction gets commited (ReorderBufferStreamCommit ends up calling >> ReorderBufferReturnTXN too). > > Right, but generally after streaming we assert that txn->size should > be 0. That could be changed if we change the above design but this is > what it is today. > Can we add some assert to enforce this? >> I wonder if there's a way to free the TOASTed data earlier, instead of >> waiting until the end of the transaction (as this patch does). But I >> suspect it'd be way more complex, harder to backpatch, and destroying >> the hash table is a good idea anyway. > > Right. > >> Also, I think the "if (txn->toast_hash != NULL)" checks are not needed, >> because it's the first thing ReorderBufferToastReset does. > > I see, I will change this. If we all agree with this idea. > +1 from me. I think it's good enough to do the cleanup at the end, and it's an improvement compared to current state. There might be cases of transactions doing many such speculative inserts and accumulating a lot of data in the TOAST hash, but I find it very unlikely. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company