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 1iww92-0007iv-Mp for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Jan 2020 22:49:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iww90-0001Bw-MZ for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Jan 2020 22:49:10 +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 1iww90-0001Bo-8c for pgsql-hackers@lists.postgresql.org; Wed, 29 Jan 2020 22:49:10 +0000 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iww8t-0005Qp-8x for pgsql-hackers@postgresql.org; Wed, 29 Jan 2020 22:49:09 +0000 Received: by mail-pf1-x442.google.com with SMTP id q8so409681pfh.7 for ; Wed, 29 Jan 2020 14:49:02 -0800 (PST) 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=coTOUxuHUU9XwUZ85OP91U/gBd3rszpIj/Ew9s8XFC4=; b=Y5XY5y9LY1JkpK9D2wfy4wWfEk4Ef9rNnoMCq6b1qGZI3OqbyYpXhMuR/X9GjajVV7 QoKEw3rB5wgedrOw4mmIcV8kgnIY2kxiSJmmIs8PtID9Z03QPH46TmgdeSjiDGiMkhJ+ 0xmMkEcA1InY4wQLjyfT7klBKUW6Mr/QbhqiwqCfZ1RHLZrn/JH0D1TnHQfUwoGv0wU3 cEJeHs8W9ed6KXNKN/EmPSdeEGIv1TiBEtDBnp1yE8N3/WXi+GckKJosI2jOY8rD7bF2 U1v6SaakT94z0pDZHk+80pA/37xzjXOJ5PlNHQz50iyV3ZgsNlp34QGipWsXqO5WvMV2 MDcA== 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=coTOUxuHUU9XwUZ85OP91U/gBd3rszpIj/Ew9s8XFC4=; b=ojMIC0j1ByKHIC0goM0HLjkec94Mlimi2hJ7+l484GUL6Geg7ZF52d74nzpR9fPb3k W3kccVp9TGEA36p9OS/ZExi1Rs4hx0G8ig7G8WXIPihoPhy/Hygo1pnBWFeO/nE2345f MW/8C+UowE/GKKCxMOhofds+M21YkJWnb7B3ksSXRF1mjUGV4OnHz2H0Z2OSKrjxuTgc ZhzeWVQPQgSwv11XAMatziDq7vyKZMgIXKwD1YyftpPCneEogWgiXRXcP5JOlgmdCsRf ra7azXZcjFCc/pY90ZpR+troIfhlOZUSVuNRqHazOZ0YaM0sqAiROb8GiKEzjFwFdtVJ eTsw== X-Gm-Message-State: APjAAAUD4HaVlvpj2XHj8NOsDhtFzprDmebvJxL1J25faWa3fbNxnrL5 hRmEl+ouA7iRn7wkEQQg082y9w== X-Google-Smtp-Source: APXvYqx77gZ/myYVpTcCs1M1glEdLKrt9L94pVnrZXUV1ZVgSJ+2DXkUZDvVhYro1OM+o/M81AnSHA== X-Received: by 2002:a62:6401:: with SMTP id y1mr1888737pfb.18.1580338140975; Wed, 29 Jan 2020 14:49:00 -0800 (PST) Received: from jdavis ([2600:1010:b00f:565:6458:80b:898f:69bc]) by smtp.gmail.com with ESMTPSA id q28sm3961940pfl.153.2020.01.29.14.48.58 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 29 Jan 2020 14:48:59 -0800 (PST) Message-ID: <6e2b4a4475a690fa515829950aeed935696b0df3.camel@j-davis.com> Subject: Re: Memory-Bounded Hash Aggregation From: Jeff Davis To: Peter Geoghegan Cc: Heikki Linnakangas , Tomas Vondra , Taylor Vesely , Adam Lee , Melanie Plageman , PostgreSQL Hackers Date: Wed, 29 Jan 2020 14:48:56 -0800 In-Reply-To: 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> <20190802152123.ichmacgx3y3x7dc3@development> <20191214173225.3kzsbncr6brpxdll@development> <775d754c-9a61-5363-6998-f87d15f1b592@iki.fi> 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, 2020-01-24 at 17:16 -0800, Peter Geoghegan wrote: > That sounds weird. Might be pathological in some sense. > > I have a wild guess for you. Maybe this has something to do with the > "test for presorted input" added by commit a3f0b3d68f9. That can > perform very badly when the input is almost sorted, but has a few > tuples that are out of order towards the end. (I have called these > "banana skin tuples" in the past.) My simple test case is: 'explain analyze select i from big group by i;', where "big" has 20M tuples. I tried without that change and it helped (brought the time from 55s to 45s). But if I completely remove the sorting of the freelist, it goes down to 12s. So it's something about the access pattern. After digging a bit more, I see that, for Sort, the LogicalTapeSet's freelist hovers around 300 entries and doesn't grow larger than that. For HashAgg, it gets up to almost 60K. The pattern in HashAgg is that the space required is at a maximum after the first spill, and after that point the used space declines with each batch (because the groups that fit in the hash table were finalized and emitted, and only the ones that didn't fit were written out). As the amount of required space declines, the size of the freelist grows. That leaves a few options: 1) Cap the size of the LogicalTapeSet's freelist. If the freelist is growing large, that's probably because it will never actually be used. I'm not quite sure how to pick the cap though, and it seems a bit hacky to just leak the freed space. 2) Use a different structure more capable of handling a large fraction of free space. A compressed bitmap might make sense, but that seems like overkill to waste effort tracking a lot of space that is unlikely to ever be used. 3) Don't bother tracking free space for HashAgg at all. There's already an API for that so I don't need to further hack logtape.c. 4) Try to be clever and shrink the file (or at least the tracked portion of the file) if the freed blocks are at the end. This wouldn't be very useful in the first recursive level, but the problem is worst for the later levels anyway. Unfortunately, I think this requires a breadth-first strategy to make sure that blocks at the end get freed. If I do change it to breadth-first also, this does amount to a significant speedup. I am leaning toward #1 or #3. As an aside, I'm curious why the freelist is managed the way it is. Newly-released blocks are likely to be higher in number (or at least not the lowest in number), but they are added to the end of an array. The array is therefore likely to require repeated re-sorting to get back to descending order. Wouldn't a minheap or something make more sense? Regards, Jeff Davis