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 1q1ZWu-00021s-Mp for pgsql-hackers@arkaria.postgresql.org; Tue, 23 May 2023 21:26:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q1ZWt-0007tk-LM for pgsql-hackers@arkaria.postgresql.org; Tue, 23 May 2023 21:26:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q1ZWt-0007ta-Bc for pgsql-hackers@lists.postgresql.org; Tue, 23 May 2023 21:26:51 +0000 Received: from mail-ej1-x633.google.com ([2a00:1450:4864:20::633]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q1ZWp-001WKr-Rb for pgsql-hackers@lists.postgresql.org; Tue, 23 May 2023 21:26:49 +0000 Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-96652cb7673so17428966b.0 for ; Tue, 23 May 2023 14:26:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1684877206; x=1687469206; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=tCZVXObaiaBDzuHxJhDkHd0rGoNv1Rifk/aPhViT9Pk=; b=bKFkytZaXVhfn4RPEpg2fNQMGoOspvutGX70d/JXQ8f9I+Hti0iIlqCLxv0u8BMfhg 8N8DGUiO1DfnUvICUSiS+pxSX9K+eMWVcUXLM263ivsFzabF0zJyrDubeFgelKLdFaRM f6kY53Fli/N5t6bGDUq1ieECzsdbJRhJ+x35RcrguUiRNLep1aDvPY4BlVVEAE5zXkiX a2Gh1I3eZKQZ76VqTPXdhMAXtOc1PUe+ec69DUGBrP+K8yTZzphaeXuOGQbXdYrJR8tU PkGaj48iYl24ln6uei+S5g2VWgocqVfDHnmhQui5jmIvpOhWhw4fvzn5jiL5dl0wphSO cgdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684877206; x=1687469206; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tCZVXObaiaBDzuHxJhDkHd0rGoNv1Rifk/aPhViT9Pk=; b=O1ZuTbLyouvqUtnrGTMAb8euJ/XuFdhPGFQAd8XYq0vKEr0eUXGZBihaG2LZVvbhvS RVj4M14/I2aAoe5pn5koHze9FkVFE678WjIbMpbXEJcTVgbojuNSsFBXMcy4E8fIdgn0 kHHteWHlu9T/LlZJZkVwXli6QJoUIvCUG/TZUktTY6ToCSrobYRtQK/afkj2k4RJ5hEc wDV9gSsbs4hUyzEAoEYNOHST/CWfeDzpgOic/wVZpPqzlGIwZyl+TCZCYP7u1fss8a1m JR/CAj+avrLAwPrnubyFLt6tdH8S9GeXnTcC3qMnDmxJTVvtzhwMVhST/JzvBTfu1A/h SSDQ== X-Gm-Message-State: AC+VfDxZ1JpaudIFmyhlsuRKowfAYWtWDKhu7lXqknadxjB3UjeoPqYd gqz8DVwzef3DfJsYeOgk9MNc/Z/3iIPbU6UJqucg7yzS5D+9h082IKFaiP6dbFkpJpGvcvvC8IU VDb+DrYpb1ZsHXQXK8oeT2ircRJwaKsnpr/4hXwcPswrznZd0mjraNDtMwKAa181EbGlpk+yKtd X0sXkO28ebFsGxCxwACf3XIvbF429/ZOkoJcxHtlWFpX3LUkwOvfo6fbSjW+Cm6b+xDg== X-Google-Smtp-Source: ACHHUZ4Cbk4qFzOOyzlQlXhQHozo6FiHuTDhQKLI48LUrdHUQIu2JrpEanSQwDUZRR1SB0dKl9E6mA== X-Received: by 2002:a17:906:5d03:b0:96f:893:7f5 with SMTP id g3-20020a1709065d0300b0096f089307f5mr16012540ejt.26.1684877205379; Tue, 23 May 2023 14:26:45 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-225-239.bb.vodafone.cz. [86.49.225.239]) by smtp.gmail.com with ESMTPSA id z30-20020a17090674de00b00965e68b8df5sm4921849ejl.76.2023.05.23.14.26.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 23 May 2023 14:26:44 -0700 (PDT) Message-ID: Date: Tue, 23 May 2023 23:26:42 +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) Content-Language: en-US To: Andres Freund Cc: PostgreSQL Hackers References: <222a3442-7f7d-246c-ed9b-a76209d19239@enterprisedb.com> <20230523171433.earidmyzock7fnk4@awork3.anarazel.de> From: Tomas Vondra In-Reply-To: <20230523171433.earidmyzock7fnk4@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/23/23 19:14, Andres Freund wrote: > Hi, > > On 2023-05-23 18:23:00 +0200, Tomas Vondra wrote: >> This means that for an UPDATE with triggers, we may end up calling this >> for each row, possibly multiple bitmaps. And those bitmaps are allocated >> in ExecutorState, so won't be freed until the end of the query :-( > > Ugh. > > > I've wondered about some form of instrumentation to detect such issues > before. It's obviously a problem that we can have fairly large leaks, like the > one you just discovered, without detecting it for a couple years. It's kinda > made worse by the memory context infrastructure, because it hides such issues. > > Could it help to have a mode where the executor shutdown hook checks how much > memory is allocated in ExecutorState and warns if its too much? There's IIRC a > few places that allocate large things directly in it, but most of those > probably should be dedicated contexts anyway. Something similar could be > useful for some other long-lived contexts. > Not sure such simple instrumentation would help much, unfortunately :-( We only discovered this because the user reported OOM crashes, which means the executor didn't get to the shutdown hook at all. Yeah, maybe if we had such instrumentation it'd get triggered for milder cases, but I'd bet the amount of noise is going to be significant. For example, there's a nearby thread about hashjoin allocating buffiles etc. in ExecutorState - we ended up moving that to a separate context, but surely there are more such cases (not just for ExecutorState). 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. This was super slow (definitely useless outside development environment) but it made it super obvious where the root cause is. I experimented with instrumenting the code a bit (as alternative to gdb breakpoints) - still slow, but much faster than gdb. But perhaps useful for special (valgrind-like) testing mode ... Would require testing with more data, though. I doubt we'd find much with our regression tests, which have tiny data sets. > ... > >> Attached is a patch, restoring the pre-12 behavior for me. > > 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. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company