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 1jpvNB-00069C-VK for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jun 2020 15:07:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jpvNA-0006dN-Rr for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jun 2020 15:07:04 +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 1jpvNA-0006dF-DS for pgsql-hackers@lists.postgresql.org; Mon, 29 Jun 2020 15:07:04 +0000 Received: from mail-ej1-x642.google.com ([2a00:1450:4864:20::642]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jpvN7-0002Xq-SL for pgsql-hackers@postgresql.org; Mon, 29 Jun 2020 15:07:03 +0000 Received: by mail-ej1-x642.google.com with SMTP id p20so16880273ejd.13 for ; Mon, 29 Jun 2020 08:07:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=EYNnDPW8/391VmnihwLREjcS4T8gxa7EisOu7776vZk=; b=TvDqobKo3c71KoMvRWVlZEi5IGOZZLMzrA0tRbib2fRUc8Z+6mwwEX7CL8dBbpjFh/ jjEsInhGFhZvOwCuLw/kWqQ7SujgelqaMCBwBuFqmV49KfVY2X+oiR4qqdOIdJQKR/GS jzQ57C3sBpiQMv8g+vlyBCrrmreurWVI1KH+eylydyKpCpRaFH9zYao75M/Et3B0W5tL OjzoLI16G3EKwvmoYEpPtC8bjy0aj/B/bx3BsIrkBG6N8oN0WfsbX5IcyV48zEjS8kw/ p9fPruekkLRLp+914gAKCLJzfKRtr6Ej+Y+b4LDlZAdfuJzJr2dtBs8yO7x81MLhdifm fhsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=EYNnDPW8/391VmnihwLREjcS4T8gxa7EisOu7776vZk=; b=Ecfve3UM+jSxYWSd98H/uNC3ZKx4ehTLhM2KPaYq8etzUlIHd9aApjSH+4fDOknn7X 8Jilhl8fLGkDOft9E5v+/wYtR+qAeEQDMo1UaTSSrsCixo6cTQiYrjulH3KV+fBueOMq /2L+Al3Q//n0iOTsNXzJASCeAeVbpYe/irevBPl4Ji72u0x5AqYHjD0o5ek16IYkDdc1 uWLsqSx/tucBikFaQoVrbEJWPZu/Aea9wLcq47ob3HkvyCgN15dQdEd9z0AL6jqhUsfE XSMVxNt1A1tISvC+EY7OKpMvTYDlKEUoHBqexkS0IGbWSQ1KO2sSWpo/tQLBvxBi+EiT /MbA== X-Gm-Message-State: AOAM532cNiLPM4iE2sEBqa7OmgoTdpzlpBCMbELRBPv68YgFLsX8QyqI tw3NiV3SFVjXWYVXh2KBwRoQ5Q== X-Google-Smtp-Source: ABdhPJxL4HFrqp+bdtSMc6caNGsTfyvKic+hYEx43bQKn4VBxgvflKZVRXUz12whhrGTdrp1acI0zw== X-Received: by 2002:a17:906:1117:: with SMTP id h23mr12921253eja.396.1593443220181; Mon, 29 Jun 2020 08:07:00 -0700 (PDT) Received: from localhost (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id cw14sm71447edb.88.2020.06.29.08.06.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jun 2020 08:06:58 -0700 (PDT) Date: Mon, 29 Jun 2020 17:06:55 +0200 From: Tomas Vondra To: Peter Geoghegan Cc: Bruce Momjian , Robert Haas , Andres Freund , David Rowley , Jeff Davis , Justin Pryzby , Melanie Plageman , "pgsql-hackers@postgresql.org" Subject: Re: Default setting for enable_hashagg_disk Message-ID: <20200629150655.ethstxkf3bkqoown@development> References: <20200625235357.udlest4k6i2uekui@development> <20200626040210.GA13963@momjian.us> <20200626144414.rftlidu34rigiulv@development> <20200626163726.GA3337@momjian.us> <20200626174513.tmnkic7q5duugvhh@development> <20200626230020.GD3337@momjian.us> <20200626235850.gvl3lpfyeobu4evi@development> <20200627104141.gq7d3hm2tvoqgjjs@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, Jun 28, 2020 at 06:39:40PM -0700, Peter Geoghegan wrote: >On Sat, Jun 27, 2020 at 3:41 AM Tomas Vondra > wrote: >> Well, there are multiple ideas discussed in this (sub)thread, one of >> them being a per-query memory limit. That requires decisions how much >> memory should different nodes get, which I think would need to be >> cost-based. > >A design like that probably makes sense. But it's way out of scope for >Postgres 13, and not something that should be discussed further on >this thread IMV. > 100% agree >> Of course, a simpler scheme like this would not require that. And maybe >> introducing hash_mem is a good idea - I'm not particularly opposed to >> that, actually. But I think we should not introduce separate memory >> limits for each node type, which was also mentioned earlier. > >I had imagined that hash_mem would apply to hash join and hash >aggregate only. A GUC that either represents a multiple of work_mem, >or an absolute work_mem-style KB value. > I'm not against having a hash_mem (and I'd vote to be it a simple work_mem-style value, not a multiple). Maybe we should have it, the argument to allow hashagg (and perhaps hashjoin) to use more memory than some other nodes seems convincing to me. I'm just not sure which of the problems mentioned in this thread it actually addresses ... >> The problem of course is that hash_mem does not really solve the issue >> discussed at the beginning of this thread, i.e. regressions due to >> underestimates and unexpected spilling at execution time. > >Like Andres, I suspect that that's a smaller problem in practice. A >hash aggregate that spills often has performance characteristics >somewhat like a group aggregate + sort, anyway. I'm worried about >cases where an *in-memory* hash aggregate is naturally far far faster >than other strategies, and yet we can't use it -- despite the fact >that Postgres 12 could "safely" do so. (It probably doesn't matter >whether the slow plan that you get in Postgres 13 is a hash aggregate >that spills, or something else -- this is not really a costing >problem.) > Not sure I follow. Which cases do you mean when you say that 12 could safely do them, but 13 won't? I see the following two cases: a) Planner in 12 and 13 disagree about whether the hash table will fit into work_mem. I don't quite see why this would be the case (given the same cardinality estimates etc.), though. That is, if 12 says "will fit" I'd expect 13 to end up with the same conclusion. But maybe 13 has higher per-tuple overhead or something? I know we set aside some memory for BufFiles, but not when we expect the whole hash table to fit into memory. b) Planner believes the hash table will fit, due to underestimate. On 12, we'd just let the hash table overflow, which may be a win when there's enough RAM and the estimate is not "too wrong". But it may easily end with a sad OOM. On 13, we'll just start spilling. True - people tend to use conservative work_mem values exactly because of cases like this (which is somewhat futile as the underestimate may be arbitrarily wrong) and also because they don't know how many work_mem instances the query will use. So yeah, I understand why people may not want to increase work_mem too much, and maybe hash_work would be a way to get the "no spill" behavior. >Besides, hash_mem *can* solve that problem to some extent. Other cases >(cases where the estimates are so bad that hash_mem won't help) seem >like less of a concern to me. To some extent, that's the price you pay >to avoid the risk of an OOM. > True, avoiding the risk of OOM has it's cost. >> The thread is getting a rather confusing mix of proposals how to fix >> that for v13 and proposals how to improve our configuration of memory >> limits :-( > >As I said to Amit in my last message, I think that all of the ideas >that are worth pursuing involve giving hash aggregate nodes license to >use more memory than other nodes. One variant involves doing so only >at execution time, while the hash_mem idea involves formalizing and >documenting that hash-based nodes are special -- and taking that into >account during both planning and execution. > Understood. I mostly agree with this. >> Interesting. What is not entirely clear to me how do these databases >> decide how much should each node get during planning. With the separate >> work_mem-like settings it's fairly obvious, but how do they do that with >> the global limit (either per-instance or per-query)? > >I don't know, but that seems like a much more advanced way of >approaching the problem. It isn't in scope here. > +1 >Perhaps I'm not considering some unintended consequence of the planner >giving hash-based nodes extra memory "for free" in the common case >where hash_mem exceeds work_mem (by 2x, say). But my guess is that >that won't be a significant problem that biases the planner in some >obviously undesirable way. > My concern is how much more difficult would these proposals make the reasoning about memory usage get. Maybe not much, not sure. I certainly agree it may be beneficial to give more memory to hashagg at the expense of other nodes. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services