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 1htZN9-0008A3-4T for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Aug 2019 15:21:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1htZN6-0006I3-N3 for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Aug 2019 15:21:32 +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 1htZN6-0006BS-CE for pgsql-hackers@lists.postgresql.org; Fri, 02 Aug 2019 15:21:32 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1htZN2-0007yA-Tv for pgsql-hackers@postgresql.org; Fri, 02 Aug 2019 15:21:31 +0000 Received: by mail-wm1-x341.google.com with SMTP id f17so66682713wme.2 for ; Fri, 02 Aug 2019 08:21:28 -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:user-agent; bh=UAo/+Zs/1d8l179jd7UQNix9w9/7EcL3Uibwx3R1fTU=; b=qP/u8LKFvDIdUaantaJvAsQUPOtxr8SPSUpGZ7u9FRCRFW10VGtqh6+lA8vQZrJPJN Lef6fAek7fkkimRzTlMeIDlRv6tinJRPa/k0gkM6EfGTfrEXFpGEvPd9ToS/4w/SIaKW SnDpm15W/2lviODVpz1ffzWElqpvjFlKrNQK8Z8gnTUhTfXR6VDnpTXtx9g87Gci1T6f deOQ4EZ0XV+0bn8+BdhyYSuE+TlQ4vNsMaFY1NK6PA03Hqk6He29vZdQoeKQC8vNht3/ NXyLOgGd4fQ3Dl9KDXvww4JoyOaU8YgglXAmvIt/VoBH35TL05mJTd9LkMltR/c4pNaJ 2l4A== 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:user-agent; bh=UAo/+Zs/1d8l179jd7UQNix9w9/7EcL3Uibwx3R1fTU=; b=lxlhBHaQXitaA9z8JHLCFc+3peP68TetpdoBGn9v1NP0IMGLcTF2U+el4Is//XpspW DfDLdxNN8MT5u9kZoysY6GyXV7zpT18EJxJPKnjUlAElvAXn3GJ08/kA/vkSlke3tPNP IhPQFQY66t087VM4ihZGJtMtQCV5qdfjZPgbL9v+h7kUp1BDWjiL3ncOmnGvH/aGiP2U aWMXoZQc3b56YdG/ET5qV37pc6jRxWue6VcjCTqnRgvl5ydlV63TjJMl3RUoy2dc6HgL 4LtBp2O1RMbtFw//4kXElRTwz7VJyxyxq1Hh41RwyRIS7aCH5BzZ+Lz00/H175eTF44Q lNNw== X-Gm-Message-State: APjAAAVP3x/Gla4Zgd1wVPwNQUfOhI6UEGVXeM4YREBo2pL2MULaqBdA 0SZs0b1ALZ4LdmWGkY2hDM3fHQ== X-Google-Smtp-Source: APXvYqy2jefeuzO5AJkn44oKh/RZVQGR8lTUPl8W9//apMGFuUtft6Nmzrmkg6xSzJqa5/kpaCn5BQ== X-Received: by 2002:a05:600c:212:: with SMTP id 18mr4815365wmi.88.1564759285227; Fri, 02 Aug 2019 08:21:25 -0700 (PDT) Received: from localhost (ip-86-49-253-160.net.upcbroadband.cz. [86.49.253.160]) by smtp.gmail.com with ESMTPSA id w23sm82063862wmi.45.2019.08.02.08.21.24 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 02 Aug 2019 08:21:24 -0700 (PDT) Date: Fri, 2 Aug 2019 17:21:23 +0200 From: Tomas Vondra To: Jeff Davis Cc: Adam Lee , Melanie Plageman , pgsql-hackers@postgresql.org Subject: Re: Memory-Bounded Hash Aggregation Message-ID: <20190802152123.ichmacgx3y3x7dc3@development> References: <507ac540ec7c20136364b5272acbcd4574aa76ef.camel@j-davis.com> <20190703001753.s5e5cme3ucjgeu6c@development> <20190711155520.zqipi23o3yybx7ym@development> <9be86fc1adc315f69b8af4b379087ab451008d8a.camel@j-davis.com> <20190802064405.GA94036@mars.local> <617fea8258ba3df7bc8b21cd7f3afc219f2383d9.camel@j-davis.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <617fea8258ba3df7bc8b21cd7f3afc219f2383d9.camel@j-davis.com> User-Agent: NeoMutt/20180716-1444-295967 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Aug 02, 2019 at 08:11:19AM -0700, Jeff Davis wrote: >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. > +1 to leaving that as a future optimization I think it's clear there's no perfect eviction strategy - for every algorithm we came up with we can construct a data set on which it performs terribly (I'm sure we could do that for the approach used by Greenplum, for example). So I think it makes sense to do what Jeff proposed, and then maybe try improving that in the future with a switch to different eviction strategy based on some heuristics. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services