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 1jeNj7-0003jf-G6 for pgsql-hackers@arkaria.postgresql.org; Thu, 28 May 2020 18:58:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jeNj6-0000eU-7z for pgsql-hackers@arkaria.postgresql.org; Thu, 28 May 2020 18:58:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jeNj5-0000eN-O3 for pgsql-hackers@lists.postgresql.org; Thu, 28 May 2020 18:58:00 +0000 Received: from mail-ed1-x543.google.com ([2a00:1450:4864:20::543]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jeNj2-0007TH-Dh for pgsql-hackers@postgresql.org; Thu, 28 May 2020 18:57:58 +0000 Received: by mail-ed1-x543.google.com with SMTP id e10so969819edq.0 for ; Thu, 28 May 2020 11:57:56 -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=0IxSd5+OOuYiczkzewni0Ae+dKvsAY5m9BhZ+jsfg3M=; b=XNf085f9T+IEOqFVaqGJ1HjuGyu+yAioTKEr/03x9PA/1gcGMCLzkMqlZhLwW4/fr5 s34Xb+g47AyqgRGxs81pMdxJVO04t4mG2iWP4e06zMYmRO0BN343wHSSK7/87SG6djiD xQwkLM+NSMmlW+Ca7Glmm6iEQWI/7UMtNJ6482+w3hdr3A/lVux1Gr9h7jZVwwd4rb4O Qq9VXo/gkVAwwN0gu+ri31NmZsLH7JncmsYOwzif4SIdkSu2BsJwL48s/azr5uoYBx49 BiJi9MUseF2CjC7iZuzl49kXUbRAYcqLk6NHaOXby4TXxPa4jOytbFLjAZ8CgiZUFqxe fopQ== 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=0IxSd5+OOuYiczkzewni0Ae+dKvsAY5m9BhZ+jsfg3M=; b=BLn2tqnGxJeOEpyDeOwGz88wS4rmlXubCslUmn7y2xo6R7YIJXuAqI+sfbgYkeswlx yAemw7C+rwXG/DdrsbDBIPzHjFY3yIA6A8gOchGjXGJdhdzjLvuEcDh4RQOzaFOrUpb4 /iW23/A7EI4sNEBT7zW1B85igknSirRNeil1U+WCjtkxrxQrv6Sw21c/5FtaDt6/gT// SBA7MuJPGLstJduBN1u479lwrOQ7lXrXv1DVbT3FFsmOMJuc8drHd0gq5TgaDyiO3o4S EJyPXsweVZ4SzQ6h1fhEJm/z3y3io4jwo/Vw4oa1/3FnYhgkISmUGAprZrjTzySsaPs0 yjwA== X-Gm-Message-State: AOAM530M8+OSzJo5Le20C/v5mumZrloTvgF2VnLdIbCixrdJkA9PDG1G sxo+mHYRIZ343Y3a/94PcLqF1Q== X-Google-Smtp-Source: ABdhPJxKHPv0K070A+AQ3uKlXm74OuxMOkctXoNno4zJnk09zPJdvm8VltegKZYpDV4+274NrlQA8g== X-Received: by 2002:a50:e607:: with SMTP id y7mr4758934edm.71.1590692272932; Thu, 28 May 2020 11:57:52 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id h14sm5193635edv.34.2020.05.28.11.57.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 May 2020 11:57:51 -0700 (PDT) Date: Thu, 28 May 2020 20:57:50 +0200 From: Tomas Vondra To: Melanie Plageman Cc: Jeff Davis , Thomas Munro , pgsql-hackers , HeikkiLinnakangas Subject: Re: Trouble with hashagg spill I/O pattern and costing Message-ID: <20200528185750.aamptuqdzrggqi5c@development> References: <20200525121722.6wzcelweefyuwg54@development> <2df2e0728d48f498b9d6954b5f9080a34535c385.camel@j-davis.com> <20200525225909.aifxjm53hw4xyzxd@development> <20200526141524.ohtvrp23vxosgivl@development> <20200526191511.d7pwz4awrvdm4p6j@development> <0589f869969de2f3e9713689159c7c4fbe1fcfa5.camel@j-davis.com> <20200527090704.onkjogxeg34ib5cn@development> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="77ydloivxtrnxwzh" Content-Disposition: inline In-Reply-To: <20200527090704.onkjogxeg34ib5cn@development> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --77ydloivxtrnxwzh Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Wed, May 27, 2020 at 11:07:04AM +0200, Tomas Vondra wrote: >On Tue, May 26, 2020 at 06:42:50PM -0700, Melanie Plageman wrote: >>On Tue, May 26, 2020 at 5:40 PM Jeff Davis wrote: >> >>>On Tue, 2020-05-26 at 21:15 +0200, Tomas Vondra wrote: >>>> >>>> As for the tlist fix, I think that's mostly ready too - the one thing >>>> we >>>> should do is probably only doing it for AGG_HASHED. For AGG_SORTED >>>> it's >>>> not really necessary. >>> >>>Melanie previously posted a patch to avoid spilling unneeded columns, >>>but it introduced more code: >>> >>> >>> >>>https://www.postgresql.org/message-id/CAAKRu_aefEsv+UkQWqu+ioEnoiL2LJu9Diuh9BR8MbyXuZ0j4A@mail.gmail.com >>> >>>and it seems that Heikki also looked at it. Perhaps we should get an >>>acknowledgement from one of them that your one-line change is the right >>>approach? >>> >>> >>I spent some time looking at it today, and, it turns out I was wrong. >> >>I thought that there was a case I had found where CP_SMALL_TLIST did not >>eliminate as many columns as could be eliminated for the purposes of >>spilling, but, that turned out not to be the case. >> >>I changed CP_LABEL_TLIST to CP_SMALL_TLIST in >>create_groupingsets_plan(), create_agg_plan(), etc and tried a bunch of >>different queries and this 2-3 line change worked for all the cases I >>tried. Is that where you made the change? > >I've only made the change in create_agg_plan, because that's what was in >the query plan I was investigating. You may be right that the same fix >is needed in additional places, though. > Attached is a patch adding CP_SMALL_TLIST to create_agg_plan and create_groupingsets_plan. I've looked at the other places that I think seem like they might benefit from it (create_upper_unique_plan, create_group_plan) but I think we don't need to modify those - there'll either be a Sort of HashAgg, which will take care about the projection. Or do you think some other places need CP_SMALL_TLIST? >>And then are you proposing to set it based on the aggstrategy to either >>CP_LABEL_TLIST or CP_SMALL_TLIST here? >> > >Yes, something like that. The patch I shared on on 5/21 just changed >that, but I'm wondering if that could add overhead for sorted >aggregation, which already does the projection thanks to the sort. > I ended up tweaking the tlist only for AGG_MIXED and AGG_HASHED. We clearly don't need it for AGG_PLAIN or AGG_SORTED. This way we don't break regression tests by adding unnecessary "Subquery Scan" nodes. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --77ydloivxtrnxwzh Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="hashagg-tlist-fix.patch" diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index 9941dfe65e..ddb277cdf0 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -2113,12 +2113,22 @@ create_agg_plan(PlannerInfo *root, AggPath *best_path) Plan *subplan; List *tlist; List *quals; + int flags; /* * Agg can project, so no need to be terribly picky about child tlist, but - * we do need grouping columns to be available + * we do need grouping columns to be available. We are a bit more careful + * with hash aggregate, where we explicitly request small tlist to minimize + * I/O needed for spilling (possibly not known already). */ - subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST); + flags = CP_LABEL_TLIST; + + /* ensure small tlist for hash aggregate */ + if (best_path->aggstrategy == AGG_HASHED || + best_path->aggstrategy == AGG_MIXED) + flags |= CP_SMALL_TLIST; + + subplan = create_plan_recurse(root, best_path->subpath, flags); tlist = build_path_tlist(root, &best_path->path); @@ -2209,7 +2219,8 @@ create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path) * Agg can project, so no need to be terribly picky about child tlist, but * we do need grouping columns to be available */ - subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST); + subplan = create_plan_recurse(root, best_path->subpath, + CP_LABEL_TLIST | CP_SMALL_TLIST); /* * Compute the mapping from tleSortGroupRef to column index in the child's --77ydloivxtrnxwzh--