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 1jzOAN-0003Gz-OM for pgsql-hackers@arkaria.postgresql.org; Sat, 25 Jul 2020 17:40:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jzOAL-0004Ru-BJ for pgsql-hackers@arkaria.postgresql.org; Sat, 25 Jul 2020 17:40:57 +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 1jzOAK-0004Rn-VJ for pgsql-hackers@lists.postgresql.org; Sat, 25 Jul 2020 17:40:57 +0000 Received: from mail-pj1-x1041.google.com ([2607:f8b0:4864:20::1041]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jzOAI-0006AE-H6 for pgsql-hackers@postgresql.org; Sat, 25 Jul 2020 17:40:55 +0000 Received: by mail-pj1-x1041.google.com with SMTP id cv18so6969666pjb.1 for ; Sat, 25 Jul 2020 10:40:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:cc:date:in-reply-to:references :mime-version:content-transfer-encoding; bh=DZjJgRwhf+HEYMmQcO9+YIuZ1I8Y+o2vx39W6GLM61w=; b=0R/UJC1Ct1JreVfcoX329VUdfc7FYpGsZ8RfD7C/QDjlwxopj+xBI6VHZxTRv17ldk 7wCpVXd+Y6pDaWFLf60kyqdnnBfMvy5LLYPVJhNr4wdWALrhevcO0G6vwB5gB++HHdul aISVP7BH4D7I/vAP5Tpc5mUAkkM8dSO+NbeTvL9xFBCIBjoZeHdjm05ENxSN44rJM6oV Evh0+SMZFIWihwkt86KzDifLTKReo0qrHBDe+LbRCH3MTdxwZSYPct20gSXq/q2aJRdH 6u0s1GduG1MCe5Oc4TKpum5s9ogeU48wT6CKcPj+Ns5JqjP1sCkD1CmlcSfUFFPp5b5+ 9rjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:mime-version:content-transfer-encoding; bh=DZjJgRwhf+HEYMmQcO9+YIuZ1I8Y+o2vx39W6GLM61w=; b=a+iewQm4k87D4KhLHnrX+aDAHDZyMw3juIJp4PvMUNdgKRQ7zZWqKJOiQeqpJgj6Y1 De7+NDtNo9fBEc+kramjLDDhexn5998skqhmc5wUaH9aAb/3+RMis+sDb2tbmmRnHTRe w02jZAV8P3nsoQ9VChHvrj7Q//aM2A0ZgFWidukf58Nwn9VyCC3YVqLkcr5Hep7aXuKy 5PrBGifrwaBS6oeTDbBcm5Ou7I8Ok5G8WHlS3/U6b7d5+J2a/2xBTReCgBzxEshCLQYv PL07aac+UV5PofdTjZibZceBYVgo786LdO4XAgXnHF9JZWyI0EqmVyyPHwUO9mSY9zhj trYw== X-Gm-Message-State: AOAM533NfZ870/ssDJ1uj88MNGdSv9bNEN2N60npBEH6mNWyxoh7Cvnp azrW7UgvhVtmfbsjMxRoORewMQ== X-Google-Smtp-Source: ABdhPJz08heDnumclfEtimVT8bzPG6MW1smupm8ht1rsQieadUfz2Ngs85M4ntXyKOOmW1fC6yxqQg== X-Received: by 2002:a17:902:8204:: with SMTP id x4mr13531735pln.16.1595698853441; Sat, 25 Jul 2020 10:40:53 -0700 (PDT) Received: from jdavis.lan (c-73-231-146-4.hsd1.ca.comcast.net. [73.231.146.4]) by smtp.gmail.com with ESMTPSA id h3sm9259675pjz.23.2020.07.25.10.40.51 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 25 Jul 2020 10:40:52 -0700 (PDT) Message-ID: <7d250899c8f2fcbf8030db4e778f39033b94c4ee.camel@j-davis.com> Subject: Re: Default setting for enable_hashagg_disk From: Jeff Davis To: Peter Geoghegan , Tomas Vondra Cc: Tom Lane , Alvaro Herrera , David Rowley , Stephen Frost , Andres Freund , Bruce Momjian , Robert Haas , Justin Pryzby , Melanie Plageman , "pgsql-hackers@postgresql.org" Date: Sat, 25 Jul 2020 10:40:50 -0700 In-Reply-To: References: <9e4a4cffe3eee0543e5b94c90821d39e03b81d29.camel@j-davis.com> <6d0aba397625df79bdc3ca85adc11b9e48ecb0da.camel@j-davis.com> <4118680.1595097043@sss.pgh.pa.us> <571473265c5d5309f4012855ebd2131c136073ab.camel@j-davis.com> <32871.1595121333@sss.pgh.pa.us> <88c0af01ad9ac105e424768808a214d855e29a77.camel@j-davis.com> <20200720004827.4c46ikcdrfum6xyu@development> <400617.1595251041@sss.pgh.pa.us> <20200720172539.6jov6ynljly4wj6i@development> <20200724012248.y77rpqc73agrsvb3@development> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.5-0ubuntu0.18.04.2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, 2020-07-23 at 19:33 -0700, Peter Geoghegan wrote: > That does make it sound like the costs of the hash agg aren't being > represented. I suppose it isn't clear if this is a costing issue > because it isn't clear if the execution time performance itself is > pathological or is instead something that must be accepted as the > cost > of spilling the hash agg in a general kind of way. I have a feeling that this is mostly a costing problem. Sort uses its memory in two different phases: 1. when writing the sorted runs, it needs the memory to hold the run before sorting it, and only a single buffer for the output tape; and 2. when merging, it needs a lot of read buffers But in HashAgg, it needs to hold all of the groups in memory *at the same time* as it needs a lot of output buffers (one for each partition). This doesn't matter a lot at high values of work_mem, because the buffers will only be 8MB at most. I did attempt to cost this properly: hash_agg_set_limits() takes into account the memory the partitions will use, and the remaining memory is what's used in cost_agg(). But there's a lot of room for error in there. If someone sees an obvious error in the costing, please let me know. Otherwise, I think it will just take some time to make it better reflect reality in a variety of cases. For v13, and we will either need to live with it, or pessimize the costing for HashAgg until we get it right. Many costing issues can deal with a lot of slop -- e.g. HashJoin vs MergeJoin -- because a small factor often doesn't make the difference between plans. But HashAgg and Sort are more competitive with each other, so costing needs to be more precise. Regards, Jeff Davis