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 1pXo3O-0007lZ-6M for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Mar 2023 18:53:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pXo3M-00029v-68 for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Mar 2023 18:53:20 +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 1pXo3L-00029m-Qj for pgsql-hackers@lists.postgresql.org; Thu, 02 Mar 2023 18:53:19 +0000 Received: from mail-ed1-x52d.google.com ([2a00:1450:4864:20::52d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pXo3I-0001Dv-OA for pgsql-hackers@lists.postgresql.org; Thu, 02 Mar 2023 18:53:19 +0000 Received: by mail-ed1-x52d.google.com with SMTP id u9so1058295edd.2 for ; Thu, 02 Mar 2023 10:53:16 -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:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=mguErkk59bWvHz+VEJR89ru8kPRpyEOhVODqjHRHC+Y=; b=ES9U4X0c4izLCmgI/+4SBGdbVjTqXKdrcuGq3eY/yt88EqGDHS3agpjd3/l14ddDTk aeDQLbLRLgkzDUKW7IgMxpypaaVgI8gb8+yfNVUAXlcOIeWdtBqkmzF3D41+jIR/7C81 aLQO1B1e7biDtA22fdZRFkkizi7Labr4B38ISdpxbADgRG0WCrxeH6hswT9ySUNjaR2S DIOCS48Q8cbdx1vLeiwye6SJ+17teBiBgWAPvX4RqSYjHHCOKuP8iGo1vwVuqdIqcpDd ZkqpOspSvFhHyonbfAYJ5Pu/ESC3GdvzX6WsCzaVsFlTdVYfi0myK6j08Z3AgWt9554S 7+sQ== 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: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=mguErkk59bWvHz+VEJR89ru8kPRpyEOhVODqjHRHC+Y=; b=fBPS48GMCkIijfM+lPSVCGaVzxxi8UcCwYylt1pafH/rwZXlgy5KdNzDcVHW7tqQIE pYG60PoyJ4hJbLdyi+zqF079WbmOKrI4xPGorkGuimQ2sAcYu3wKsDOrFTIiZwHvvVP3 kEVJi4PL+9BV7hVcNB5MGAaPjzjLWhvAXoh+k8nzP5xHgzMMTae1ZZ3s6hH1H/vk6ruA E4HIahJj1yJTIM+bLHS1W3NrvPdyJfApWjegspP8tR0ah4g+IjVQyQLQzhPY0/CsfhHG Ltxz16ATfCj9mszlVvn+hzU8gbGOeA4ONQAbfbfdnlj3fuVEP1J1sAP+bPniXtez+4d0 UeRQ== X-Gm-Message-State: AO0yUKXI6P4fcijxcS35lKxgio9khECPo8KwwwsraqtGrVS8bomyR/Xy R3rXFUMHunfl6dYKlTCW9b6a955P1lYn/p48yEwilfgPBU4pf91hRBIOnd8MEdtjO3wnbadLoR4 yUOURRJEm45ZBKS0kBLLqgPiHI+8EqfttAN+Qi326+cjaSh1bTZKzmRLKCKCjTJ3nSA+h+CWD0J mwsd0rSu+tD44WWthoCS/AmLUoPixFR5kIiVtmyTpvs53Ksu8JJuDL X-Google-Smtp-Source: AK7set/xbNoDz+GR00Skn1eW080J5LXV5ekPZ6VuTVKoiFWVAwFUpn304UTRYZsSj4bYP/oKi6yEuw== X-Received: by 2002:a17:907:6d9f:b0:907:9bda:93b9 with SMTP id sb31-20020a1709076d9f00b009079bda93b9mr4863201ejc.17.1677783195559; Thu, 02 Mar 2023 10:53:15 -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 h7-20020a50cdc7000000b004bc15a440f1sm198499edj.78.2023.03.02.10.53.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Mar 2023 10:53:15 -0800 (PST) Message-ID: Date: Thu, 2 Mar 2023 19:53:14 +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? Content-Language: en-US To: Jehan-Guillaume de Rorthais Cc: pgsql-hackers@lists.postgresql.org References: <20230228190643.1e368315@karst> <45d453c8-b2d3-b477-36eb-32fdf4455f3c@enterprisedb.com> <20230301184840.0a897a80@karst> <3013398b-316c-638f-2a73-3783e8e2ef02@enterprisedb.com> <20230302001827.66e95dc3@karst> <41c5766d-ed71-b70c-bbbc-d3396c462d62@enterprisedb.com> <20230302130838.717e888d@karst> <77a96d42-00cb-2448-465a-aa1e92d00cac@enterprisedb.com> <20230302191530.781909fe@karst> From: Tomas Vondra In-Reply-To: <20230302191530.781909fe@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/2/23 19:15, Jehan-Guillaume de Rorthais wrote: > Hi! > > On Thu, 2 Mar 2023 13:44:52 +0100 > Tomas Vondra wrote: >> Well, yeah and no. >> >> In principle we could/should have allocated the BufFiles in a different >> context (possibly hashCxt). But in practice it probably won't make any >> difference, because the query will probably run all the hashjoins at the >> same time. Imagine a sequence of joins - we build all the hashes, and >> then tuples from the outer side bubble up through the plans. And then >> you process the last tuple and release all the hashes. >> >> This would not fix the issue. It'd be helpful for accounting purposes >> (we'd know it's the buffiles and perhaps for which hashjoin node). But >> we'd still have to allocate the memory etc. (so still OOM). > > Well, accounting things in the correct context would already worth a patch I > suppose. At least, it help to investigate faster. Plus, you already wrote a > patch about that[1]: > > https://www.postgresql.org/message-id/20190421114618.z3mpgmimc3rmubi4%40development > > Note that I did reference the "Out of Memory errors are frustrating as heck!" > thread in my first email, pointing on a Tom Lane's email explaining that > ExecutorState was not supposed to be so large[2]. > > [2] https://www.postgresql.org/message-id/flat/12064.1555298699%40sss.pgh.pa.us#eb519865575bbc549007878a5fb7219b > Ah, right, I didn't realize it's the same thread. There are far too many threads about this sort of things, and I probably submitted half-baked patches to most of them :-/ >> There's only one thing I think could help - increase the work_mem enough >> not to trigger the explosive growth in number of batches. Imagine >> there's one very common value, accounting for ~65MB of tuples. With >> work_mem=64MB this leads to exactly the explosive growth you're >> observing here. With 128MB it'd probably run just fine. >> >> The problem is we don't know how large the work_mem would need to be :-( >> So you'll have to try and experiment a bit. >> >> I remembered there was a thread [1] about *exactly* this issue in 2019. >> >> [1] >> https://www.postgresql.org/message-id/flat/bc138e9f-c89e-9147-5395-61d51a757b3b%40gusw.net >> >> I even posted a couple patches that try to address this by accounting >> for the BufFile memory, and increasing work_mem a bit instead of just >> blindly increasing the number of batches (ignoring the fact that means >> more memory will be used for the BufFile stuff). >> >> I don't recall why it went nowhere, TBH. But I recall there were >> discussions about maybe doing something like "block nestloop" at the >> time, or something. Or maybe the thread just went cold. > > So I read the full thread now. I'm still not sure why we try to avoid hash > collision so hard, and why a similar data subset barely larger than work_mem > makes the number of batchs explode, but I think I have a better understanding of > the discussion and the proposed solutions. > I don't think this is about hash collisions (i.e. the same hash value being computed for different values). You can construct cases like this, of course, particularly if you only look at a subset of the bits (for 1M batches we only look at the first 20 bits), but I'd say it's fairly unlikely to happen unless you do that intentionally. (I'm assuming regular data types with reasonable hash functions. If the query joins on custom data types with some silly hash function, it may be more likely to have conflicts.) IMHO a much more likely explanation is there actually is a very common value in the data. For example there might be a value repeated 1M times, and that'd be enough to break this. We do build a special "skew" buckets for values from an MCV, but maybe the stats are not updated yet, or maybe there are too many such values to fit into MCV? I now realize there's probably another way to get into this - oversized rows. Could there be a huge row (e.g. with a large text/bytea value)? Imagine a row that's 65MB - that'd be game over with work_mem=64MB. Or there might be smaller rows, but a couple hash collisions would suffice. > There was some thoughts about how to make a better usage of the memory. As > memory is exploding way beyond work_mem, at least, avoid to waste it with too > many buffers of BufFile. So you expand either the work_mem or the number of > batch, depending on what move is smarter. TJis is explained and tested here: > > https://www.postgresql.org/message-id/20190421161434.4hedytsadpbnglgk%40development > https://www.postgresql.org/message-id/20190422030927.3huxq7gghms4kmf4%40development > > And then, another patch to overflow each batch to a dedicated temp file and > stay inside work_mem (v4-per-slice-overflow-file.patch): > > https://www.postgresql.org/message-id/20190428141901.5dsbge2ka3rxmpk6%40development > > Then, nothing more on the discussion about this last patch. So I guess it just > went cold. > I think a contributing factor was that the OP did not respond for a couple months, so the thread went cold. > For what it worth, these two patches seems really interesting to me. Do you need > any help to revive it? > I think another reason why that thread went nowhere were some that we've been exploring a different (and likely better) approach to fix this by falling back to a nested loop for the "problematic" batches. As proposed in this thread: https://www.postgresql.org/message-id/20190421161434.4hedytsadpbnglgk%40development So I guess the best thing would be to go through these threads, see what the status is, restart the discussion and propose what to do. If you do that, I'm happy to rebase the patches, and maybe see if I could improve them in some way. I was hoping we'd solve this by the BNL, but if we didn't get that in 4 years, maybe we shouldn't stall and get at least an imperfect stop-gap solution ... regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company