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 1jeeh6-0001Bu-Df for pgsql-hackers@arkaria.postgresql.org; Fri, 29 May 2020 13:05:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jeeh5-000512-65 for pgsql-hackers@arkaria.postgresql.org; Fri, 29 May 2020 13:05:03 +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 1jeeh4-00050t-RW for pgsql-hackers@lists.postgresql.org; Fri, 29 May 2020 13:05:02 +0000 Received: from mail-ej1-x642.google.com ([2a00:1450:4864:20::642]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jeeh2-0000Ik-9h for pgsql-hackers@postgresql.org; Fri, 29 May 2020 13:05:01 +0000 Received: by mail-ej1-x642.google.com with SMTP id gl26so1974338ejb.11 for ; Fri, 29 May 2020 06:05:00 -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=GYqrORgJHbFCbxxcYFlgDp61b5Ll1lkdmBVNErBwKH8=; b=m50dLIm/58tNPxVoRB8m+C4tiN+D8pTK4t+htQQ/S+6x/EhB/ICV7173Dh0QTX7TUG ip/4KQcXBKTgcuK032TO1tktdQVq4npJfIl/qFtFpBUunrAjS7NeTl6WZHzPX+Npa7bT QDJ/ji7mJaakATiAVI9XhkveJgTBFaBqtroiI7PkDJEDX2hKAfDT5D5WicR2ujIcUtxC 9eMs9UJRiHAUOpTRbuC1n0Bi93EwGaPBqK3LAEc57exW3vH17gZ0U4n/1wOb3E8YfjPN 2ie9PWz3YOzRh2AW6dd62bROSrbfazKIjWy5gD2Yj0kiuaQjOJqVKz+l6w4Hxhv+0pmn iscw== 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=GYqrORgJHbFCbxxcYFlgDp61b5Ll1lkdmBVNErBwKH8=; b=sLb3Vra+1Ut31oszInk4a2OgBQ2iCs3EnOUkmvomtxRWwyZRGCZEZpKIj03umnciDr D9jSkLPeE5AJepqXrxQapIFcOQuFMyaVoU8Iw4Rs3YfWkiUwNn9/ItXTuhmVY66sDwgK k8and9JaJorrxYmZjYoZ2FxMqJKhGKW9Erw8ZFWI7NvyCegKNqdiJ9RHCMmtxr/uxv// cprz2EdyCLkco4aePNXoSxie6MakqmDNgtchPmOWxlCTS5H3DZnl04e8m/GnOyGsVIh3 jZzhLdAOZn5L9dyDsxU9ulLmejmI11wDG18FT0z4fiZuXDV73ct/yl7jpV72FyiHrDBa 9QJw== X-Gm-Message-State: AOAM530FrPQF5O00UbLXSnryxLgiO7oD+QFAP7Nqu8FTRkWlstSy1NXX vYJSHPSkwOqmm0B1g6O+GZ4LLA== X-Google-Smtp-Source: ABdhPJyXqwcjuRLOyxaVBml7qJ4BCPzhcgyTRET9wu3MIBbk2siktzE5bFjuPpnmrwxGhB9SX66d3g== X-Received: by 2002:a17:906:bcd3:: with SMTP id lw19mr7954491ejb.276.1590757498648; Fri, 29 May 2020 06:04:58 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id s19sm7422810eja.91.2020.05.29.06.04.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2020 06:04:56 -0700 (PDT) Date: Fri, 29 May 2020 15:04:54 +0200 From: Tomas Vondra To: Jeff Davis Cc: Melanie Plageman , Thomas Munro , pgsql-hackers , HeikkiLinnakangas Subject: Re: Trouble with hashagg spill I/O pattern and costing Message-ID: <20200529130454.el6qyuukj3ofs4eu@development> References: <20200525225909.aifxjm53hw4xyzxd@development> <20200526141524.ohtvrp23vxosgivl@development> <20200526191511.d7pwz4awrvdm4p6j@development> <0589f869969de2f3e9713689159c7c4fbe1fcfa5.camel@j-davis.com> <20200527090704.onkjogxeg34ib5cn@development> <20200528185750.aamptuqdzrggqi5c@development> <0fb3456095d469cc20a9ee7e76edfe9237dae24f.camel@j-davis.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="aldvszp5mqw37ch7" Content-Disposition: inline In-Reply-To: <0fb3456095d469cc20a9ee7e76edfe9237dae24f.camel@j-davis.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --aldvszp5mqw37ch7 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Thu, May 28, 2020 at 06:14:55PM -0700, Jeff Davis wrote: >On Thu, 2020-05-28 at 20:57 +0200, Tomas Vondra wrote: >> Attached is a patch adding CP_SMALL_TLIST to create_agg_plan and >> create_groupingsets_plan. > >Looks good, except one question: > >Why would aggstrategy ever be MIXED when in create_agg_path? Wouldn't >that only happen in create_groupingsets_path? > Ah, right. Yeah, we only need to check for AGG_HASH here. Moreover, AGG_MIXED probably does not need the tlist tweak, because the input should be pre-sorted as with AGG_SORTED. And we should probably do similar check in the create_groupinsets_path, I guess. At first I thought we can't do that before inspecting rollups, which only happens later in the function, but now I see there's aggstrategy in GroupingSetsPath too. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --aldvszp5mqw37ch7 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="hashagg-tlist-fix-v2.patch" diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index 9941dfe65e..eab57fd74b 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -2113,12 +2113,21 @@ 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) + flags |= CP_SMALL_TLIST; + + subplan = create_plan_recurse(root, best_path->subpath, flags); tlist = build_path_tlist(root, &best_path->path); @@ -2200,6 +2209,7 @@ create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path) int maxref; List *chain; ListCell *lc; + int flags; /* Shouldn't get here without grouping sets */ Assert(root->parse->groupingSets); @@ -2207,9 +2217,17 @@ 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 + * 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) + flags |= CP_SMALL_TLIST; + + subplan = create_plan_recurse(root, best_path->subpath, flags); /* * Compute the mapping from tleSortGroupRef to column index in the child's --aldvszp5mqw37ch7--