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 1pXJtN-0006YC-Vr for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Mar 2023 10:41:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pXJtM-00011X-Dx for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Mar 2023 10:41:00 +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 1pXJtM-00011N-3H for pgsql-hackers@lists.postgresql.org; Wed, 01 Mar 2023 10:41:00 +0000 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pXJtE-0006gr-Tc for pgsql-hackers@lists.postgresql.org; Wed, 01 Mar 2023 10:40:59 +0000 Received: by mail-wr1-x432.google.com with SMTP id t15so12713374wrz.7 for ; Wed, 01 Mar 2023 02:40:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; 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=GRee2YdaOhB3Ex5fbezLFHmhTnNvN8a35jrT7ZDNYKg=; b=TuAts3Z8coS0GJOg3ptIgYbtC+3TXpjA/n7gyfrJ9PiTbF9PVbYemHOxbdi+5JrEjx 3lnVyXOmRiNPTuB9ZiO+PqEDUtgslylenmEaMP6IftVL2xysH4KvM6w1gV+f7ljXroEJ 4UrlS9zwnzg97WngTmtm2VcVfzlwqWuRD91pzbhV5otCHJl6DK2kkxMKjWwGMGCe7o5Z ht91xLOv/SljbmOlwOd84Cxd/kD6G4BpZBg+zjhpr8yZFPJ+7hHIEun//DImqrmHmHYs rbnfMJ9EY4YVF9OZDyrtIvQ8JI7V5EWwdh5ChkpDSDQ9rFdoW9XQTOsoS31vDZN+spuT i3gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=GRee2YdaOhB3Ex5fbezLFHmhTnNvN8a35jrT7ZDNYKg=; b=s9w0Gqjt2j4wHj5UnvxtEzgY0DCiQRAzr3Vk6/Gx9cm6iis/rmi9yhYSSE/LH67Lr0 Fo9W7ifN+lL8CTEc5ZfrcoetHpH2silPJzSKXh3ZR8+QpdeBhdO6nY68iqASNgFX0wv1 s7vHEbkXsMuRJCdoACI4mVK5es5eK1tBSCbPVegbADBuxdkAnBO0kD2KAg0cNFUYHI3J +VeHj+199RG8mH59U0cMa7mxihN4+lpoAK1pNVVc8Xilt2Jx6Z8QViFqodXWJN1mKvyb jZSbZjgMC+D6GiJdaQAM5Ea2POPZqjV7zTk7fS5/55AHLNw3NI9WLPJMPlaU52zg/Uv/ TQTw== X-Gm-Message-State: AO0yUKVuP9vrQjI1v6MDVw84bt3kkzQ7JM2H6CxTMLlo9fQDyBwtJ6Tb S6bBeatSnq8yb6jaHRQ1XPqhW03U1csOsd4NgkZRUeeTZCf+AgpuovzIQy15k2r276hXX12dNms pKXLRVHg9uBApYViySRqmV5MwMlD4eM+O10/RJcWxcErlIgZs4cX6s0fQFYZWjp0XRs/7Rqansi wIh82Jr1GHOieezvF0e9o4AOx8KRccNtEorqXmmAV6zalkRqq+xjdS X-Google-Smtp-Source: AK7set+53wihsgatVlPYYXjO9/tALZde04e2XXLHH+sAm1nxapdpBHsb8EZwz7uigo5QApS3hgsw8w== X-Received: by 2002:a5d:5308:0:b0:2c7:a39:6e2e with SMTP id e8-20020a5d5308000000b002c70a396e2emr4283290wrv.15.1677667251548; Wed, 01 Mar 2023 02:40:51 -0800 (PST) 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 k28-20020a5d525c000000b002c556a4f1casm12148091wrc.42.2023.03.01.02.40.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Mar 2023 02:40:51 -0800 (PST) Message-ID: <0c8fdad2-e14b-8415-95b2-9e82dc28b2bd@enterprisedb.com> Date: Wed, 1 Mar 2023 11:40:51 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: Memory leak from ExecutorState context? To: Jehan-Guillaume de Rorthais , Justin Pryzby Cc: pgsql-hackers@lists.postgresql.org References: <20230228190643.1e368315@karst> <20230228182508.GA30529@telsasoft.com> <20230301104612.7799b105@karst> Content-Language: en-US From: Tomas Vondra In-Reply-To: <20230301104612.7799b105@karst> 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 3/1/23 10:46, Jehan-Guillaume de Rorthais wrote: > Hi Justin, > > On Tue, 28 Feb 2023 12:25:08 -0600 > Justin Pryzby wrote: > >> On Tue, Feb 28, 2023 at 07:06:43PM +0100, Jehan-Guillaume de Rorthais wrote: >>> Hello all, >>> >>> A customer is facing out of memory query which looks similar to this >>> situation: >>> >>> https://www.postgresql.org/message-id/flat/12064.1555298699%40sss.pgh.pa.us#eb519865575bbc549007878a5fb7219b >>> >>> This PostgreSQL version is 11.18. Some settings: >> >> hash joins could exceed work_mem until v13: > > Yes, I am aware of this. But as far as I understand Tom Lane explanations from > the discussion mentioned up thread, it should not be ExecutorState. > ExecutorState (13GB) is at least ten times bigger than any other context, > including HashBatchContext (1.4GB) or HashTableContext (16MB). So maybe some > aggregate is walking toward the wall because of bad estimation, but something > else is racing way faster to the wall. And presently it might be something > related to some JOIN node. > I still don't understand why would this be due to a hash aggregate. That should not allocate memory in ExecutorState at all. And HashBatchContext (which is the one bloated) is used by hashjoin, so the issue is likely somewhere in that area. > About your other points, you are right, there's numerous things we could do to > improve this query, and our customer is considering it as well. It's just a > matter of time now. > > But in the meantime, we are facing a query with a memory behavior that seemed > suspect. Following the 4 years old thread I mentioned, my goal is to inspect > and provide all possible information to make sure it's a "normal" behavior or > something that might/should be fixed. > It'd be interesting to see if the gdb stuff I suggested yesterday yields some interesting info. Furthermore, I realized the plan you posted yesterday may not be the case used for the failing query. It'd be interesting to see what plan is used for the case that actually fails. Can you do at least explain on it? Or alternatively, if the query is already running and eating a lot of memory, attach gdb and print the plan in ExecutorStart set print elements 0 p nodeToString(queryDesc->plannedstmt->planTree) Thinking about this, I have one suspicion. Hashjoins try to fit into work_mem by increasing the number of batches - when a batch gets too large, we double the number of batches (and split the batch into two, to reduce the size). But if there's a lot of tuples for a particular key (or at least the hash value), we quickly run into work_mem and keep adding more and more batches. The problem with this theory is that the batches are allocated in HashTableContext, and that doesn't grow very much. And the 1.4GB HashBatchContext is used for buckets - but we should not allocate that many, because we cap that to nbuckets_optimal (see 30d7ae3c76). And it does not explain the ExecutorState bloat either. Nevertheless, it'd be interesting to see the hashtable parameters: p *hashtable regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company