Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1htZDM-0007hB-EF for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Aug 2019 15:11:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1htZDL-0004zH-43 for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Aug 2019 15:11:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1htZDK-0004zA-Mt for pgsql-hackers@lists.postgresql.org; Fri, 02 Aug 2019 15:11:26 +0000 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1htZDI-0007sm-FW for pgsql-hackers@postgresql.org; Fri, 02 Aug 2019 15:11:26 +0000 Received: by mail-pf1-x42c.google.com with SMTP id f17so32202820pfn.6 for ; Fri, 02 Aug 2019 08:11:23 -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=cM094G+HAo2UwT3pbfoUlntokkOqbefA02R6aEb+ejQ=; b=lwzjur4z/KbRlSBeXovIdjhbBmROHl1oJkeRKiG1MUsKZgA09SeHbXfJT2lo7WnTwb 39LxzTtEPTwr2nNlNeS1yRKz0Xks2B7vgrRmdS/rTIOdDdzDkISRHlslN9CRFBDjQQey /dN7tNwQgZDHhiP1lS3nkkN5U9ByP3gzQQBkVgT5R6fa+J9ZHvZnX9r4SGSO7JyKxPAz dSMm26b6FmcHMTkhW6fvnyHRVuSUjvJ87uMX8HVOcv2IztOYKExK+t15iFfxS+qy/26o d0E29S6xrcyx5pwa+ZQI88dJ4etuxAQytZZP5tZMNMRQW6kaCx5sus1hlS3DfwrkIFqQ fCAA== 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=cM094G+HAo2UwT3pbfoUlntokkOqbefA02R6aEb+ejQ=; b=G0zV1+oW/I0OIWg6tJIzyCO+DGgPa+7rz+M7Q9/FZljyemt1EMMkLrNzNHhDp851CU gzZm7WP1RaVOVJdOzvm7qpUmvOc4Bgt6Pe0XTuZKSVUW6X+WNdF/9e+JsSSceb8SnvVe S4R7nUa5YwuPdjp2Nu3lqivTpDnWIjETgfYrUcn0ybLaI3oLDj+zsr84nC2PIQ05PSQf L6AIu8tz+MA4/M5SyZPmndqMEBy9WlR9vEybdWGJ8GS9q2vH+msjHkWFILV+0gezB43g dm+cV8YEya6vMCebBccLIFccAa+3ts4ze2FEkoXurg5tsjtz42CckV/EpOQsWbU5T/IF szyg== X-Gm-Message-State: APjAAAXw00P49JY+7Uer40VB0tki/I1hhYpj1Wf/Fk+4XrszkKaspgpV QB0eQs4KOnHRKG/pK04lS0M= X-Google-Smtp-Source: APXvYqxF1Kk3Rt9cCMNvJA+4jKy/iZZy+1hRZkh+U+0viM4fP7WfBgF39m8wEFeLH3fMBaaEQdAeXA== X-Received: by 2002:a62:8246:: with SMTP id w67mr61647796pfd.226.1564758681766; Fri, 02 Aug 2019 08:11:21 -0700 (PDT) Received: from jdavis.lan (c-71-204-186-7.hsd1.ca.comcast.net. [71.204.186.7]) by smtp.gmail.com with ESMTPSA id a6sm6920051pjs.31.2019.08.02.08.11.20 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 02 Aug 2019 08:11:20 -0700 (PDT) Message-ID: <617fea8258ba3df7bc8b21cd7f3afc219f2383d9.camel@j-davis.com> Subject: Re: Memory-Bounded Hash Aggregation From: Jeff Davis To: Adam Lee Cc: Melanie Plageman , pgsql-hackers@postgresql.org Date: Fri, 02 Aug 2019 08:11:19 -0700 In-Reply-To: <20190802064405.GA94036@mars.local> References: <507ac540ec7c20136364b5272acbcd4574aa76ef.camel@j-davis.com> <20190703001753.s5e5cme3ucjgeu6c@development> <20190711155520.zqipi23o3yybx7ym@development> <9be86fc1adc315f69b8af4b379087ab451008d8a.camel@j-davis.com> <20190802064405.GA94036@mars.local> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.5-0ubuntu0.18.04.1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 2019-08-02 at 14:44 +0800, Adam Lee wrote: > I'm late to the party. You are welcome to join any time! > These two approaches both spill the input tuples, what if the skewed > groups are not encountered before the hash table fills up? The spill > files' size and disk I/O could be downsides. Let's say the worst case is that we encounter 10 million groups of size one first; just enough to fill up memory. Then, we encounter a single additional group of size 20 million, and need to write out all of those 20 million raw tuples. That's still not worse than Sort+GroupAgg which would need to write out all 30 million raw tuples (in practice Sort is pretty fast so may still win in some cases, but not by any huge amount). > Greenplum spills all the groups by writing the partial aggregate > states, > reset the memory context, process incoming tuples and build in-memory > hash table, then reload and combine the spilled partial states at > last, > how does this sound? That can be done as an add-on to approach #1 by evicting the entire hash table (writing out the partial states), then resetting the memory context. It does add to the complexity though, and would only work for the aggregates that support serializing and combining partial states. It also might be a net loss to do the extra work of initializing and evicting a partial state if we don't have large enough groups to benefit. Given that the worst case isn't worse than Sort+GroupAgg, I think it should be left as a future optimization. That would give us time to tune the process to work well in a variety of cases. Regards, Jeff Davis