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 1jzUD8-00062N-Fe for pgsql-hackers@arkaria.postgresql.org; Sun, 26 Jul 2020 00:08:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jzUD7-0001nF-Ei for pgsql-hackers@arkaria.postgresql.org; Sun, 26 Jul 2020 00:08:13 +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 1jzUAH-0006kw-Ju for pgsql-hackers@lists.postgresql.org; Sun, 26 Jul 2020 00:05:17 +0000 Received: from mail-ed1-x544.google.com ([2a00:1450:4864:20::544]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jzUAE-0003gx-Ox for pgsql-hackers@postgresql.org; Sun, 26 Jul 2020 00:05:17 +0000 Received: by mail-ed1-x544.google.com with SMTP id h28so9578485edz.0 for ; Sat, 25 Jul 2020 17:05:14 -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=iSsdfrwKJhCwu8S+Guhb6B7DpC+FDJg0z2IVPAGD2Jg=; b=qy0vTxm/auNjY5d7ddnLXFHTzMb1PJhhZK0ovTbud/CQWrSjPzWLtAhAvtHKk/qdXX R8j3fgCFsEtcek3Av4xaZwn5SQcnaeey1lR51+cSWCrifflTBVn0BYWVcyWnPe/cZipq +0zaV0qo7kp+Br/PX/yqbC4e+NAkyQJsNwbtCOsgheWZ275inPQBgYaKlhB9JKQYAHyO ODDzY4/WyIH9ZKnuxXCzTZ2x5jJawHwkQ+zKsQLol45Mjv83EcCXKx0gVZtmkuTxtxYh Qijc/UUpXhQnOsP2kpTNxSbNodAiZZeakNcKva0yVQEktHlp3ptu51N03zvFbfiTfsIS uFaQ== 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=iSsdfrwKJhCwu8S+Guhb6B7DpC+FDJg0z2IVPAGD2Jg=; b=aJXkeqr27mR74EsK4kK6QomFtscvjt6Z98hfdBiI5Yw0MMU6wF3Y1+WAnGHtgjtH/Y nytcJpklvPCSpET6cTdu0TE73YiqnLvta9Scd8KU5V881uhyOjcywfNJ24xGg5uNhrqx JeiiPM1JQ1W2AIol1HNAVawbEQU91t6D3lJAWx+57Gb2NdEPGMU3citZluFSFYy7KVYr doZYKWK2JGaz9N+Sgs92n86yoRE1tTut2mAgyEUkY/fwcCkQHbfXZl0+D+fitv89f+IL SXzh5NZOGZb4dXUoowJindjEu/chpF79l9F9EfaEMUe+Tyb7M10i3roZs4Rwl0W9bmJy noqA== X-Gm-Message-State: AOAM531DbbhTmU8RbzRP4H6I4lXb7xGHewTTKFW8G7thE8UCCKhOgzlF t3DfdGVxn+K4Jv4wuZdGCbVpwA== X-Google-Smtp-Source: ABdhPJw9QrTO/6TLrf3GruVwpvRjQH6PcQun0WvPgJZzhYV6/EU57UiF9JrBRkxo3evZvM5FUCteZA== X-Received: by 2002:aa7:c4d1:: with SMTP id p17mr14616005edr.268.1595721912610; Sat, 25 Jul 2020 17:05:12 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id qw19sm2541272ejb.46.2020.07.25.17.05.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jul 2020 17:05:11 -0700 (PDT) Date: Sun, 26 Jul 2020 02:05:09 +0200 From: Tomas Vondra To: Peter Geoghegan Cc: Robert Haas , Tom Lane , Jeff Davis , Alvaro Herrera , David Rowley , Stephen Frost , Andres Freund , Bruce Momjian , Justin Pryzby , Melanie Plageman , "pgsql-hackers@postgresql.org" Subject: Re: Default setting for enable_hashagg_disk Message-ID: <20200726000509.vtl4p7vzazdr2dif@development> References: <20200720004827.4c46ikcdrfum6xyu@development> <400617.1595251041@sss.pgh.pa.us> <20200720172539.6jov6ynljly4wj6i@development> <20200724012248.y77rpqc73agrsvb3@development> <20200724191648.zmdaofoqayi4al23@development> 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 Sat, Jul 25, 2020 at 10:07:37AM -0700, Peter Geoghegan wrote: >On Sat, Jul 25, 2020 at 9:39 AM Peter Geoghegan wrote: >> "Peak Memory Usage: 1605334kB" >> >> Hash agg avoids spilling entirely (so the planner gets it right this >> time around). It even uses notably less memory. > >I guess that this is because the reported memory usage doesn't reflect >the space used for transition state, which is presumably most of the >total -- array_agg() is used in the query. > I'm not sure what you mean by "reported memory usage doesn't reflect the space used for transition state"? Surely it does include that, we've built the memory accounting stuff pretty much exactly to do that. I think it's pretty clear what's happening - in the sorted case there's only a single group getting new values at any moment, so when we decide to spill we'll only add rows to that group and everything else will be spilled to disk. In the unsorted case however we manage to initialize all groups in the hash table, but at that point the groups are tiny an fit into work_mem. As we process more and more data the groups grow, but we can't evict them - at the moment we don't have that capability. So we end up processing everything in memory, but significantly exceeding work_mem. FWIW all my tests are done on the same TPC-H data set clustered by l_shipdate (so probably random with respect to other columns). regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services