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 1jp8HP-0001Tv-Ly for pgsql-hackers@arkaria.postgresql.org; Sat, 27 Jun 2020 10:41:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jp8HO-00030z-7f for pgsql-hackers@arkaria.postgresql.org; Sat, 27 Jun 2020 10:41:50 +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 1jp8HN-00030s-Rl for pgsql-hackers@lists.postgresql.org; Sat, 27 Jun 2020 10:41:50 +0000 Received: from mail-ej1-x642.google.com ([2a00:1450:4864:20::642]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jp8HK-0002Ki-Jg for pgsql-hackers@postgresql.org; Sat, 27 Jun 2020 10:41:49 +0000 Received: by mail-ej1-x642.google.com with SMTP id n24so11706681ejd.0 for ; Sat, 27 Jun 2020 03:41:46 -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=PSbu3J+gbvK6tcIA7/salUm9egsvOBPtrfbpnMxbbnc=; b=ZrG07GLcNwGQbmwOeuPDdbfo1TteU9T5/Jscwjam9haQEVND0lgLIWLudJ9dBZz1D6 +7DhmqseTRqYJBbeANo13DmVqCvfKlBt41WAO+zT3W+duWNmkA8oRn32iTkqWwZaWRW3 R4ynarLwzF0HiM2StQY9E0L0h4IwizYG0NNVDBUpjajLBHbpC+nnBkYGeBjOE+DcFefV ijeiy90K+3FisoTYKRoDq8RE7nXyxuAnq39FOe+IlsNFrvlaqOhhHJ2m/VdJ5IvH21hU A1p2ZIRoYukFG/kwxQkR69AKz0CfSg8ydiJsi8NdWd8VnXtBXXnSdGw4MVwhIGuki86y TM+w== 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=PSbu3J+gbvK6tcIA7/salUm9egsvOBPtrfbpnMxbbnc=; b=E/ObFXyp5c1b6TkMvGpSoRB0InnPvCE/GjhrkTqw2P9/GubdGN80Cv5VnpKeyPvjrQ X1JVrweURAn70VjLA1k+obMfLHOmP/ss5LMWFTm5zcasxtQFSNcpRfPIhtRKB+spG8uB 7cdbzRVk61kGqkBDgx2I2G33rGrFUhmpo1KM7DV1ZK96Y824XVIoPwMLhCLG1R+rZxv5 HvR9hIeoXiC0YsMXX4o1R05qCmd+ziKOV5PHCWpwJ5C03bJYroB7K0G/gIzuJlIDG3Hs nD/7sOcSujZBrU4LLFH/qaHNsnFN0rUoKlNoZgb2hgpa8PnNRs8WWHprcqyE/caH8OCU 1/MA== X-Gm-Message-State: AOAM533YXo2gZv2izwyfEd+2x9mw02wKStd5cVr5qNPpiDgcrCxyURwh O5mM2RZfKmsFT4dLRoOfINv2QQ== X-Google-Smtp-Source: ABdhPJzZ4B+hPDxnLNN9wgJW730BdCE2TNrljYgXhBvxvUoJjFLlduUQyIXP5YzLsVV70tkViQNJDQ== X-Received: by 2002:a17:906:ce3c:: with SMTP id sd28mr4466768ejb.382.1593254505682; Sat, 27 Jun 2020 03:41:45 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id z1sm20714815eji.92.2020.06.27.03.41.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Jun 2020 03:41:44 -0700 (PDT) Date: Sat, 27 Jun 2020 12:41:41 +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: <20200627104141.gq7d3hm2tvoqgjjs@development> References: <20200625171756.GB12486@momjian.us> <20200625235357.udlest4k6i2uekui@development> <20200626040210.GA13963@momjian.us> <20200626144414.rftlidu34rigiulv@development> <20200626163726.GA3337@momjian.us> <20200626174513.tmnkic7q5duugvhh@development> <20200626230020.GD3337@momjian.us> <20200626235850.gvl3lpfyeobu4evi@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 Fri, Jun 26, 2020 at 05:24:36PM -0700, Peter Geoghegan wrote: >On Fri, Jun 26, 2020 at 4:59 PM Tomas Vondra > wrote: >> I agree larger work_mem for hashagg (and thus less spilling) may mean >> lower work_mem for so some other nodes that are less sensitive to this. >> But I think this needs to be formulated as a cost-based decision, >> although I don't know how to do that for the reasons I explained before >> (bottom-up plan construction vs. distributing the memory budget). > >Why do you think that it needs to be formulated as a cost-based >decision? That's probably true of a scheme that allocates memory very >intelligently, but what about an approach that's slightly better than >work_mem? > 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. >What problems do you foresee (if any) with adding a hash_mem GUC that >gets used for both planning and execution for hash aggregate and hash >join nodes, in about the same way as work_mem is now? > 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. 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. 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 :-( >> FWIW some databases already do something like this - SQL Server has >> something called "memory grant" which I think mostly does what you >> described here. > >Same is true of Oracle. But Oracle also has simple work_mem-like >settings for sorting and hashing. People don't really use them anymore, >but apparently it was once common for the DBA to explicitly give over >more memory to hashing -- much like the hash_mem setting I asked about. >IIRC the same is true of DB2. > 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)? >> The difference between sort and hashagg spills is that for sorts >> there is no behavior change. Plans that did (not) spill in v12 will >> behave the same way on v13, modulo some random perturbation. For >> hashagg that's not the case - some queries that did not spill before >> will spill now. >> >> So even if the hashagg spills are roughly equal to sort spills, both >> are significantly more expensive than not spilling. > >Just to make sure we're on the same page: both are significantly more >expensive than a hash aggregate not spilling *specifically*. OTOH, a >group aggregate may not be much slower when it spills compared to an >in-memory sort group aggregate. It may even be noticeably faster, due >to caching effects, as you mentioned at one point upthread. > >This is the property that makes hash aggregate special, and justifies >giving it more memory than other nodes on a system-wide basis (the same >thing applies to hash join). This could even work as a multiplier of >work_mem. > Yes, I agree. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services