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 1q1kBl-0002Nj-A2 for pgsql-hackers@arkaria.postgresql.org; Wed, 24 May 2023 08:49:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q1kBj-0005JU-Bi for pgsql-hackers@arkaria.postgresql.org; Wed, 24 May 2023 08:49:43 +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 1q1kBj-0005J4-29 for pgsql-hackers@lists.postgresql.org; Wed, 24 May 2023 08:49:43 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q1kBf-001c0c-Hq for pgsql-hackers@lists.postgresql.org; Wed, 24 May 2023 08:49:41 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-96f7bf29550so86454866b.3 for ; Wed, 24 May 2023 01:49:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1684918176; x=1687510176; 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=3DCZM+D6ivGlvHxME1SelvBtVFWspfGwQNa8KfM+WpM=; b=Zbi2D/FO80fRMhZBtAI8b1SnlJPnXBJcG/Gehe8yol8v8bdKnrBgzczKFDHcSO7ppB kVWDb/rpZhy+4rhqw5VZ/kg4F8BhjZt2gxif7FbD48X8DbS0QoYX9h7nHZAJGfHa/YYN eWYuBFzWHG1eybj7ugdulbxqivcEkOnqnZNyegIpEUPYSa0sbYY3MUAMaea/gyMX+IhY Sk26vIamRw48n+hDV204SjS00+RTd6v7Ippz07b17L7dqXDMewxvc8soMZYOIfV8Pqfn SCGp3jPf695Q6hE1+CynBdxNfXzuXcx9mJ+q0/LOQMwXox5yDKN2L45In7OimKJrqkLf mGog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684918176; x=1687510176; 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=3DCZM+D6ivGlvHxME1SelvBtVFWspfGwQNa8KfM+WpM=; b=QNANYuRbM04g/qxX/ukMwSfAJqwGG8XrlDiUs6JGbTL2MliKYi0baNxtRteFZMhvwD bEltcBfFSdC6MuYEDMHdTUaddwiC3wdgn+b8aeoKoTMKb6BG7d7RIDhURJUw/tIxSxAw MLesF7Zw8KgzL5puyqsUuuqLcvBPOtkDiJcfUsvzDJqT6K85NX2PmdyUjUopfTlGUExf bovfgobobFxawQl74dSqsmvdr8PgU+yoN195WtMJUUJyt0DCFAg3/Nhacc9J8ZexEpOV HWRBUMEHsPOcJXrvF/Bx5iXfGiH/RSi43DnAgUjwCWqi44M2HncTzejJudemP9B2/Ij+ ilRQ== X-Gm-Message-State: AC+VfDwiXUqu4xHklVZ5y8kXUqp4ZzRfEa90XnfkemgDqHb58UypqOtH H9nwrBNNIt74Vd91HiRtbBrJNvuTI2O3hDWwohycGPy9Ra7YHgv7IH0S8pYJ/s28AaOh7qi6BxZ kefqFori5wukg+lBFd88Lrt+ltv8cq8i4d0E3cxgN1JGQtxLOwYqH3DcKdjGZY7CNP1gBEnMXB9 4hfaPJQxleQQ7a0FDKU+oLDwJn5WKu7Kckz1e7OQd2+Wkh/GN7tKyPc3++q9+5urusbQ== X-Google-Smtp-Source: ACHHUZ514i+RySEJaMmI+DRQo/gtRYYMT22RtkSlt8bRJIumUgfZ4HxEThZopSHG/vAvI2ZyS+ysdg== X-Received: by 2002:a17:906:7305:b0:966:5c04:2c61 with SMTP id di5-20020a170906730500b009665c042c61mr15578644ejc.8.1684918176054; Wed, 24 May 2023 01:49:36 -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 30-20020a170906011e00b0096fc35ca733sm4295170eje.41.2023.05.24.01.49.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 May 2023 01:49:35 -0700 (PDT) Message-ID: Date: Wed, 24 May 2023 10:49:14 +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: Tom Lane Cc: Andres Freund , PostgreSQL Hackers References: <222a3442-7f7d-246c-ed9b-a76209d19239@enterprisedb.com> <20230523171433.earidmyzock7fnk4@awork3.anarazel.de> <993814.1684877984@sss.pgh.pa.us> Content-Language: en-US From: Tomas Vondra In-Reply-To: <993814.1684877984@sss.pgh.pa.us> 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 23:39, Tom Lane wrote: > Tomas Vondra writes: >> 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, I've had some success localizing palloc memory leaks with valgrind's > leak detection mode. The trick is to ask it for a report before the > context gets destroyed. Beats writing your own infrastructure ... > I haven't tried valgrind, so can't compare. Would it be possible to filter which memory contexts to track? Say we know the leak is in ExecutorState, so we don't need to track allocations in other contexts. That was a huge speedup for me, maybe it'd help valgrind too. Also, how did you ask for the report before context gets destroyed? >> Would require testing with more data, though. I doubt we'd find much >> with our regression tests, which have tiny data sets. > > Yeah, it's not clear whether we could make the still-hypothetical check > sensitive enough to find leaks using small test cases without getting an > unworkable number of false positives. Still, might be worth trying. I'm not against experimenting with that. Were you thinking about something that'd be cheap enough to just be enabled always/everywhere, or something we'd enable during testing? This reminded me a strangeloop talk [1] [2] about the Scalene memory profiler from UMass. That's for Python, but they did some smart tricks to reduce the cost of profiling - maybe we could do something similar, possibly by extending the memory contexts a bit. [1] https://youtu.be/vVUnCXKuNOg?t=1405 [2] https://youtu.be/vVUnCXKuNOg?t=1706 > It might be an acceptable tradeoff to have stricter rules for what can > be allocated in ExecutorState in order to make this sort of problem > more easily detectable. > Would these rules be just customary, or defined/enforced in the code somehow? I can't quite imagine how would that work, TBH. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company