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 1pX4f7-0004Vi-D4 for pgsql-hackers@arkaria.postgresql.org; Tue, 28 Feb 2023 18:25:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pX4f6-0002oV-83 for pgsql-hackers@arkaria.postgresql.org; Tue, 28 Feb 2023 18:25:16 +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 1pX4f5-0002oL-SY for pgsql-hackers@lists.postgresql.org; Tue, 28 Feb 2023 18:25:15 +0000 Received: from mail-il1-x12f.google.com ([2607:f8b0:4864:20::12f]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pX4f3-0000nG-1N for pgsql-hackers@lists.postgresql.org; Tue, 28 Feb 2023 18:25:15 +0000 Received: by mail-il1-x12f.google.com with SMTP id i12so6858175ila.5 for ; Tue, 28 Feb 2023 10:25:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; t=1677608711; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=MsFz4d/PH1jYLNZm+h/RKaVxzC8Uak2Hj+Mvo3RU5j8=; b=UUlbdSL4ExpYBG8H2I8uMh53kcDWhAxE82drBlY+8uxRaQAii+mMJm+cv71RkktmfD mnhcxTSKiHfY6AUtZvbZom3X/HkU8e2GsybCjLvOmW0l3EoV6kipGkICLX/E95Lf8Pwo I9YE1FqTNzOfDGDJypuMhWtVo0qMWmm8/WzcuvKoTj2TVwZBabItdL1ArAkYQhSbEEYr CU9H7hBkXGlsEdtpj5VNcv++6ddGx4lxMsQM38yuQZ2TQ21z1cB8Qhdh4ab211DCpSnP C0n7uKekqTzRfiiw7yorrkmLzHboL3U9b78b/DDBqWSSKTQRABzq7FSmXY8wrx8z51p3 rkoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677608711; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MsFz4d/PH1jYLNZm+h/RKaVxzC8Uak2Hj+Mvo3RU5j8=; b=eT+afTi/OaFtowXfiSHNBaqs6wlc3pRg/L5vEHCYcQghEhASACFPfwLko0QgbkASRe 2fEFDNJdmEzv+YlWzLyDaKKtfonqi+fhsgzdqihTuUPa8PJcLKoszRS0U5xBxJU2EG1Q sYEy+7bgzyCh2nGh1bgDFaBhOLNiIRj/04Fc7Buxj9+A1Mn5s0jwdXAsuBlP/uASIiVI 1UZFSfBwi8vrINbxwlByP/IW0zg2x4g+WdHondtB2EtKkI8aKxIIcX3Y3UxLyY3qutQO 2wgad/CUwof5h9w/NBp9x2TpederVigiv9pxjqpJahnm+67ZbsKeuAPICBZgeb7Q/WKW eSeA== X-Gm-Message-State: AO0yUKX0QcA1cR+0RYwf8ViRgBISvpB8sbYT2O3mIOgHf6FqxEiVOBD2 /R0fC0+sO7jAaCMVt8UaZNW9xAigAGfa97TR X-Google-Smtp-Source: AK7set/Tvs0Sy68T33H+ycOXbnJr7+dZW/B4+tpOaeWrrb3UyjsdcI0Y43krlX8zin9S55B7LEQgpw== X-Received: by 2002:a05:6e02:1c48:b0:317:5956:f7fd with SMTP id d8-20020a056e021c4800b003175956f7fdmr3814539ilg.30.1677608710967; Tue, 28 Feb 2023 10:25:10 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id l3-20020a02a883000000b003c4f97d41d2sm2949007jam.116.2023.02.28.10.25.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Feb 2023 10:25:10 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 1D927800BF2; Tue, 28 Feb 2023 12:25:09 -0600 (CST) Date: Tue, 28 Feb 2023 12:25:08 -0600 From: Justin Pryzby To: Jehan-Guillaume de Rorthais Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Memory leak from ExecutorState context? Message-ID: <20230228182508.GA30529@telsasoft.com> References: <20230228190643.1e368315@karst> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230228190643.1e368315@karst> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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: |Allow hash aggregation to use disk storage for large aggregation result |sets (Jeff Davis) | |Previously, hash aggregation was avoided if it was expected to use more |than work_mem memory. Now, a hash aggregation plan can be chosen despite |that. The hash table will be spilled to disk if it exceeds work_mem |times hash_mem_multiplier. | |This behavior is normally preferable to the old behavior, in which once |hash aggregation had been chosen, the hash table would be kept in memory |no matter how large it got — which could be very large if the planner |had misestimated. If necessary, behavior similar to that can be obtained |by increasing hash_mem_multiplier. > https://explain.depesz.com/s/sGOH This shows multiple plan nodes underestimating the row counts by factors of ~50,000, which could lead to the issue fixed in v13. I think you should try to improve the estimates, which might improve other queries in addition to this one, in addition to maybe avoiding the issue with joins. > The customer is aware he should rewrite this query to optimize it, but it's a > long time process he can not start immediately. To make it run in the meantime, > he actually removed the top CTE to a dedicated table. Is the table analyzed ? > Is it usual a backend is requesting such large memory size (13 GB) and > actually use less of 60% of it (7.7GB of RSS)? It's possible it's "using less" simply because it's not available. Is the process swapping ? -- Justin