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 1jdLCj-000578-Ch for pgsql-hackers@arkaria.postgresql.org; Mon, 25 May 2020 22:04:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jdLCi-0005j8-3r for pgsql-hackers@arkaria.postgresql.org; Mon, 25 May 2020 22:04:16 +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 1jdLCh-0005j0-ON for pgsql-hackers@lists.postgresql.org; Mon, 25 May 2020 22:04:15 +0000 Received: from mail-ed1-x543.google.com ([2a00:1450:4864:20::543]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jdLCd-000693-WF for pgsql-hackers@postgresql.org; Mon, 25 May 2020 22:04:14 +0000 Received: by mail-ed1-x543.google.com with SMTP id bs4so16078524edb.6 for ; Mon, 25 May 2020 15:04:11 -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=bCqs2IRWzHjh3bJUy7l6Fugr8UqICe2uTFTzen1dKeA=; b=Vyd/6HAR74lCdZMOGTaIV+kl+e8TXgQaEHMFqWu4PsVKw9JG1iFh9TtIvyz0qNTBUH HGgnd/useqKnA5Q/yD14EmTEp56MwXyxxDry1nVXq3XWNzEpzyZ64d76492avCU9SXEF COFqrPwDAKKtIYgz4zUNoAxcrOO/r6yAgCL8QZ6pBg2I2PYTnmX8TWS1eA5k/77W9Glg VjlnqbyreIiA0dBtfjrJrqv4xR1+WTvPCN5fnGSI8Cz66GFWtpae+XUc/A95EcO/fQi8 HRst7hMj9ejUbu4/ojX7qbZfNxhgwr/H1Kizj/w8DBUHrhZMVVry/k6CIKGrSNn5PuU4 mHwQ== 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=bCqs2IRWzHjh3bJUy7l6Fugr8UqICe2uTFTzen1dKeA=; b=H+Ysxz5wlPi07FrEVQ+DWwlQEEKFPnV0Har4qPDWrNZGfjBy0WqMZQzIa7UVUb/pov e0K2+YYtWw1LaZ8SAloTE4ylQsFmjC4fZlo/WxGR2H8vxnRWU023U9dKSu/J44zTWWyC JMIIdxycNy1pnhzgrgHt7cJS60hTxo8m3uF/qocxKL+xjYKGkpCVLBoElOADUXDhTkYU 5pWm39TuMYXCPQLTlxdCmw17ynv96TEk61a6bKCSYmHYwNheseX1lAkrUBzrVPO9zE5S ULtugxqkszL6LW5VE1QWogpsE47kHu+JN7mkFtiwI0D3BHj08ZVrrv8/1FqJ5+7tnsSK 8Vtw== X-Gm-Message-State: AOAM531EdTWX+xp9bwITrOhAvoHozSVZbg/dYnS/P96R7uZILXu5r5Of SsCZiDruxX2Hv3eohuIGYI5cMD4yo0M= X-Google-Smtp-Source: ABdhPJwa1yQ8WuXaB1XxoRm/nuSWiM7W0NQSkCu0yatknNl8rdg/XINSXz/6E+vIggICUmRbqwX6aw== X-Received: by 2002:a50:d65c:: with SMTP id c28mr16421344edj.21.1590444250623; Mon, 25 May 2020 15:04:10 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id dj28sm552283edb.69.2020.05.25.15.04.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2020 15:04:09 -0700 (PDT) Date: Tue, 26 May 2020 00:04:07 +0200 From: Tomas Vondra To: Jeff Davis Cc: pgsql-hackers@postgresql.org Subject: Re: Trouble with hashagg spill I/O pattern and costing Message-ID: <20200525220407.c7t5iqg272bb54nc@development> References: <20200519151202.u2p2gpiawoaznsv2@development> <88dd39d782178539d7ee46f98b98579940e2a12e.camel@j-davis.com> <20200519175320.tl4qm6le5dbg2pcf@development> <20200521191318.gw3xjivrxracvzmg@development> <8619437df796903e64c8479e299d13ebacb5fbe0.camel@j-davis.com> <20200521214122.6opovye4mnbqczlc@development> <20200525021045.dilgcsmgiu4l5jpa@development> <978a6fa0bc00071d59ebcddf15ca72ae5aa7a15d.camel@j-davis.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <978a6fa0bc00071d59ebcddf15ca72ae5aa7a15d.camel@j-davis.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, May 25, 2020 at 11:36:42AM -0700, Jeff Davis wrote: >On Mon, 2020-05-25 at 04:10 +0200, Tomas Vondra wrote: >> algorithm master prealloc tlist prealloc-tlist >> -------------------------------------------------- >> hash 1365 437 368 213 >> sort 226 214 224 215 >> >> The sort row simply means "enable_hashagg = off" and AFAIK the >> patches >> should not have a lot of influence here - the prealloc does, but it's >> fairly negligible. > >I also say a small speedup from the prealloc patch for Sort. I wrote if >off initially, but I'm wondering if there's something going on there. >Perhaps drawing K elements from the minheap at once is better for >caching? If so, that's good news, because it means the prealloc list is >a win-win. > True. >> -> Finalize HashAggregate >> Group Key: lineitem_1.l_partkey >> -> Gather >> Workers Planned: 2 >> -> Partial HashAggregate >> Group Key: >> lineitem_1.l_partkey >> -> Parallel Seq Scan on >> lineitem lineitem_1 >> (20 rows) > >Although each worker here only gets half the tuples, it will get >(approximately) all of the *groups*. This may partly explain why the >planner moves away from this plan when there are more workers: the >number of hashagg batches doesn't go down much with more workers. > >It also might be interesting to know the estimate for the number of >groups relative to the size of the table. If those two are close, it >might look to the planner like processing the whole input in each >worker isn't much worse than processing all of the groups in each >worker. > Hmmm, yeah. The number of groups per worker is another moving part. But isn't the number of groups per worker pretty much the same (and equal to the total number of groups) in all plans? I mean all the plans (both hash and sort based) have this: -> Finalize HashAggregate (cost=18313351.98..18654393.43 rows=14949762 width=36) Group Key: lineitem_1.l_partkey Planned Partitions: 64 -> Gather (cost=14993231.96..17967638.74 rows=14949762 width=36) Workers Planned: 1 -> Partial HashAggregate (cost=14992231.96..16471662.54 rows=14949762 width=36) Group Key: lineitem_1.l_partkey Planned Partitions: 64 -> Parallel Seq Scan on lineitem lineitem_1 (cost=0.00..11083534.91 rows=264715991 width=9) I think it's rather that we actually expect this number of rows from each worker, so the total cost is parallel_tuple_cost * num_of_workers * num_of_groups But we just ignore this inherent cost when picking the number of workers, don't we? Because we don't know how many rows will be produced and passed to the Gather in the end. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services