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.92) (envelope-from ) id 1jHdq9-00074y-Ox for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Mar 2020 01:31:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jHdq8-0001oK-Jm for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Mar 2020 01:31:16 +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 1jHdq8-0001oD-Au for pgsql-hackers@lists.postgresql.org; Fri, 27 Mar 2020 01:31:16 +0000 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jHdq5-0005ZD-6A for pgsql-hackers@postgresql.org; Fri, 27 Mar 2020 01:31:15 +0000 Received: by mail-wm1-x333.google.com with SMTP id c81so9867853wmd.4 for ; Thu, 26 Mar 2020 18:31:12 -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=oEF3S6hE0IUP2opw+QKqNH+tOSGY7Sl0RhX3cr8BGvY=; b=FSskQsIqyQfzU6uI16uo3fgUl80ESK35clerNY7ULCRmSZmnmMfC7JbPhOL4Foidx9 xoL1QFyNSzSukKvrKU3VTYZP9TQMIKFbxgHMFPYJ97iENoqRyvOK4WEuwf52rYWhsFH3 W1Kg6jhDa/d6XCcMthbKg16p7y2uQYAw0ZfjCLXXjLikZiX34MdlQRY483fjmAx3jjjj BDF63sg2LRY9f1cqDkr2wK6E/wEI3rIpXHiCQPlmFjTveVbfYJus2xTLiszM7JQKEA57 iS19TjfpVwexYgdmwSU/Q4WpsgeoB3prkByDetQsicHXrOde/1XTO7kvZVYcm8G+21Ku yblw== 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=oEF3S6hE0IUP2opw+QKqNH+tOSGY7Sl0RhX3cr8BGvY=; b=dha0SmKg1mJD2oNUhlPouDf5BmXVnlg2npiTzeyH3FJSgSkxHCDcBNXJHqUvTMWJJQ CjDKT4x+ylGjJqXfCJBu5jT8/4nWmxNTuVyx2S0ERc8XuNkpbcPHxF6DjpKC+LhEOfr9 WFvWqsTze6fALSPV1MAAxL4RN+oL+2plGFL5eXRvVuN14B0Wa4fnIapzbYsQl4qddX7y TR7e+BEhuQwUxXFcLfnJL8Us/fkQHl05cBajWqsaXC4McemBWcfcr+xChiOl43Gk+qxh aZqd1O9GK3cTe3xR8ZIKrTBzNCgKCmiizEl8TFjcvQIjLwoh3sZEIRRJa1oS/zt69V9i qO+w== X-Gm-Message-State: ANhLgQ0Vjf+5GWZkSIeHJ0TpWyeeMv2XNNTyhnppKvOk+pL2jOd6HkM3 F+cpk6g0rrqjJXgjpqI9VGTnaA== X-Google-Smtp-Source: ADFU+vtfmZUY+YBRs2bqjxijdPttknE1YDgp49JUijqAe0CB9AvEnWiYwXnyrv5uuEG8On8L6PgpyA== X-Received: by 2002:a1c:668a:: with SMTP id a132mr2727688wmc.126.1585272671453; Thu, 26 Mar 2020 18:31:11 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id q8sm6264817wrc.8.2020.03.26.18.31.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2020 18:31:09 -0700 (PDT) Date: Fri, 27 Mar 2020 02:31:08 +0100 From: Tomas Vondra To: Richard Guo Cc: Jeff Davis , Justin Pryzby , Andres Freund , Melanie Plageman , Heikki Linnakangas , Taylor Vesely , Adam Lee , Melanie Plageman , PostgreSQL-development Subject: Re: Memory-Bounded Hash Aggregation Message-ID: <20200327013108.tiskardwkqr5otg6@development> References: <9759ab2d0c2c28a175e11efe1788d37dc7e1b768.camel@j-davis.com> <20200222180020.ej5gvfcaf2semgji@alap3.anarazel.de> <5c371df7cee903e8cd4c685f90c6c72086d3a2dc.camel@j-davis.com> <20200224232928.45bcjmdxgnobijgi@alap3.anarazel.de> <6e7c269b9a84ff75fefcad8ab2d4758f03581e98.camel@j-davis.com> <20200312210146.GG29065@telsasoft.com> <2688fb94f3d0e571768bf851b7fde0eae780e898.camel@j-davis.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Mar 26, 2020 at 05:56:56PM +0800, Richard Guo wrote: >Hello, > >When calculating the disk costs of hash aggregation that spills to disk, >there is something wrong with how we determine depth: > >> depth = ceil( log(nbatches - 1) / log(num_partitions) ); > >If nbatches is some number between 1.0 and 2.0, we would have a negative >depth. As a result, we may have a negative cost for hash aggregation >plan node, as described in [1]. > >I don't think 'log(nbatches - 1)' is what we want here. Should it be >just '(nbatches - 1)'? > I think using log() is correct, but why should we allow fractional nbatches values between 1.0 and 2.0? You either have 1 batch or 2 batches, you can't have 1.5 batches. So I think the issue is here nbatches = Max((numGroups * hashentrysize) / mem_limit, numGroups / ngroups_limit ); and we should probably do nbatches = ceil(nbatches); right after it. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services