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 1q2Bw5-0002cP-Lx for pgsql-hackers@arkaria.postgresql.org; Thu, 25 May 2023 14:27:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q2Bw4-0002tk-HW for pgsql-hackers@arkaria.postgresql.org; Thu, 25 May 2023 14:27:24 +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 1q2Bw4-0002tb-7d for pgsql-hackers@lists.postgresql.org; Thu, 25 May 2023 14:27:24 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q2Bw0-0025Pi-0B for pgsql-hackers@lists.postgresql.org; Thu, 25 May 2023 14:27:23 +0000 Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-510f525e06cso4585152a12.2 for ; Thu, 25 May 2023 07:27:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1685024837; x=1687616837; 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=ZvE1fyhZvXk6oXc1QwBUxTpGrEFDMaMvqbSXjxellKM=; b=KlEW51k42X8oSRq0pl4mzUth1157sEytsVT/fnOtRAlpTqKKTpZqv8ag/zW2QNUt9v XVJJRdw+05sIBdsyefK6Oc8kM7ccWBdWkNFG5hzt0VsxAvqNIibb/l0Eiqlbq3jcVwN1 etosOoM8CYwxHedt8UGnVhG2qEYu+bcKY534yk5OE8dlaaFcm4wg/mS0rNYKFSgOUaWi N5hwMDdG6aJ2syiJTAXSedzqJckfnZ84fu8J3O2VTVk+9xYPE3HpTXCytxgrp+UeFpLF CpsBAUy5vwnUz3cMecY+znNkanjxhqtA/FivVYDzKGKbjOeWaPkXwzZoMzUv9Y3c8nTF DnLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685024837; x=1687616837; 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=ZvE1fyhZvXk6oXc1QwBUxTpGrEFDMaMvqbSXjxellKM=; b=Dd52RgHKr6bI95Qb+7snkt8Azw821gGHGuvSlN1Ty3EHgVz91M+rNPSpqSXNqq9k7K jhfpABaTYN4EVI5hRN8/b1sj1FTMfjObrjqXxj62tZPe9XZH21jQcIWS1bYO7Jp2Sn27 cZA8NwmzI8D9QPl24SbGKv7v5PwUJJJCRAcdTrT33K2yXZJYyHYKwgyAYMT0kyapQQdz DuGed2UDevaS5eK207ifPaUpx8spqA/MHb8RlCZ/sKQCj9Soe/ggW40Xub/Pr1Q95jkr dNUHimXGBtkR+H02Xm4nqBMB5xafKwcWeHLU2HTia10+1Kkc2BBoqKvTTZUw3lm3IiI0 wtDw== X-Gm-Message-State: AC+VfDycatxza+kD/rzVH0JL3yTEtwqa2eDtUj2+FDx5DNHru1XNomey fL6VJ6Gf766sN7ZT//u2dVNQevOomLx8+W4LjHxUFjXLs7uatpsL5Hk8onrbzpXWmM29OsL8In5 Ew//597Kez5WEp5wkBZlD2Iz4oKogw6LxjowRyzi3orL+gww/Y1jjuTLCvUkw4WuQ0M3Z5lIDvF HaJZPNKIKN0oGixvOwUQfAeFtnc0ev4cLSmdFic5oZy9+ZjkoPzzSnL+sJ7rr4Y/V1kA== X-Google-Smtp-Source: ACHHUZ4hi3wFUxFW7x4PYVWybDRD/YsyEiWG8QVPdS1KSDD7OFHpSlJrlmKCTgSPz0vEQ6z1F0aXCQ== X-Received: by 2002:a05:6402:394:b0:510:e790:4911 with SMTP id o20-20020a056402039400b00510e7904911mr4620427edv.4.1685024837414; Thu, 25 May 2023 07:27:17 -0700 (PDT) Received: from [10.137.0.17] (static-84-42-175-93.bb.vodafone.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id c25-20020aa7df19000000b0050e04125a46sm613885edy.10.2023.05.25.07.27.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 25 May 2023 07:27:16 -0700 (PDT) Message-ID: <5479b188-17b1-ad6b-bf7d-fcefaf9d382d@enterprisedb.com> Date: Thu, 25 May 2023 16:27:16 +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 trigger handling (since PG12) To: Andres Freund Cc: PostgreSQL Hackers References: <222a3442-7f7d-246c-ed9b-a76209d19239@enterprisedb.com> <20230523171433.earidmyzock7fnk4@awork3.anarazel.de> <20230524181408.wlgzhoe7he7h3tjk@awork3.anarazel.de> <20230524202209.xdgmvz7hg23xy76d@awork3.anarazel.de> Content-Language: en-US From: Tomas Vondra In-Reply-To: <20230524202209.xdgmvz7hg23xy76d@awork3.anarazel.de> Content-Type: text/plain; charset=UTF-8 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/24/23 22:22, Andres Freund wrote: > Hi, > > On 2023-05-24 21:56:22 +0200, Tomas Vondra wrote: >>>> The really hard thing was determining what causes the memory leak - the >>>> simple instrumentation doesn't help with that at all. It tells you there >>>> might be a leak, but you don't know where did the allocations came from. >>>> >>>> What I ended up doing is a simple gdb script that sets breakpoints on >>>> all palloc/pfree variants, and prints info (including the backtrace) for >>>> each call on ExecutorState. And then a script that aggregate those to >>>> identify which backtraces allocated most chunks that were not freed. >>> >>> FWIW, for things like this I found "heaptrack" to be extremely helpful. >>> >>> E.g. for a reproducer of the problem here, it gave me the attach "flame graph" >>> of the peak memory usage - after attaching to a running backend and running an >>> UPDATE triggering the leak.. >>> >>> Because the view I screenshotted shows the stacks contributing to peak memory >>> usage, it works nicely to find "temporary leaks", even if memory is actually >>> freed after all etc. >>> >> >> That's a nice visualization, but isn't that useful only once you >> determine there's a memory leak? Which I think is the hard problem. > > So is your gdb approach, unless I am misunderstanding? The view I > screenshotted shows the "peak" allocated memory, if you have a potential leak, > you can see where most of the allocated memory was allocated. Which at least > provides you with a good idea of where to look for a problem in more detail. > Right, it wasn't my ambition to detect memory leaks but to source of the leak if there's one. I got a bit distracted by the discussion detecting leaks etc. > >>>>> Hm. Somehow this doesn't seem quite right. Shouldn't we try to use a shorter >>>>> lived memory context instead? Otherwise we'll just end up with the same >>>>> problem in a few years. >>>>> >>>> >>>> I agree using a shorter lived memory context would be more elegant, and >>>> more in line with how we do things. But it's not clear to me why we'd >>>> end up with the same problem in a few years with what the patch does. >>> >>> Because it sets up the pattern of manual memory management and continues to >>> run the relevant code within a query-lifetime context. >>> >> >> Oh, you mean someone might add new allocations to this code (or into one >> of the functions executed from it), and that'd leak again? Yeah, true. > > Yes. It's certainly not obvious this far down that we are called in a > semi-long-lived memory context. > That's true, but I don't see how adding a ExecGetAllUpdatedCols() variant that allocates stuff in a short-lived context improves this. That'll only cover memory allocated in ExecGetAllUpdatedCols() and nothing else. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company