Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ujeRb-00AI56-9x for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Aug 2025 13:44:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1ujeRa-00FyEW-8H for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Aug 2025 13:44:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ujeRZ-00FyE3-Tm for pgsql-hackers@lists.postgresql.org; Wed, 06 Aug 2025 13:44:37 +0000 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ujeRX-0014Wn-1p for pgsql-hackers@lists.postgresql.org; Wed, 06 Aug 2025 13:44:37 +0000 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-76bddb92dc1so6665689b3a.0 for ; Wed, 06 Aug 2025 06:44:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754487874; x=1755092674; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:references:mime-version:from :subject:cc:to:message-id:date:from:to:cc:subject:date:message-id :reply-to; bh=4hJ39oHlaUiBVJSB3QDvzdzzhsPTM2duBfjwvq3tN4s=; b=F9Xzpt2Sw6y845bLHO+qDFgSDvclnkvxFvSLWeyTvFo4dDWXjcoxLmJbPJ+e4MQWni HZCfxMCuj/mKje8JXOz9gUqZgD5v680vV/ZM4JhBxFwcDT15wk4GAlQOQiKgqyVG4uAl 8Iresxq5vwrZzI1vf5Y9B6r51n/+ttyI+/21vkQMPnX67ehvQWgzfh+Ik5fa87JGd0wS spYn2Z6F2Uvd1ExQ3UyYD//U/wbWCMbD9hH0PQaCZfVb8YrnkjYCN7L0arWkBp/Z8Pmq ueXFXmOrFNtjhWuhCbEEotVGVmzr3FJInS8UcF3L7W1CtP5RGO5Y6Pc38a7yS8cSX4Me s8Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754487874; x=1755092674; h=content-transfer-encoding:in-reply-to:references:mime-version:from :subject:cc:to:message-id:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=4hJ39oHlaUiBVJSB3QDvzdzzhsPTM2duBfjwvq3tN4s=; b=wnrdBpS+SWesdmdoSPtpzeyTJLMo+a5gx11jSoKm9GnHwpVfi51yLZKzcCiAgJb58c iwA19WGbh6xwF6GU9HpKPK/P1abZOSCD0eZ3ikR3qOKxR80pKlvUynP3f4axlfq99H7T AKCus3y6d0JjLKX3HE04nWisNC921WwdloCIxsL5f4FIRY0kMY+h2w8PWM1Fb09mo09v zL70gUnOuwAKop99Vi9OkfYGdCgSSYIsFqNukpC5RaT6ulIMCNFVmr7FSZch6EvbzlCj mf8JaWupd3Zv8UH2nNy49yRhMHxzDsKU4ZkAY6XhgUzAjPD++m7qJvd8s0niELp4ptow CBew== X-Forwarded-Encrypted: i=1; AJvYcCXDd/hA4wiYFqvsk7OU8XXKTyskUnAnmZQH9HkoT20dqk3ohW95zsAof2N4zdCNriiYJz/Lw7wk2Jutrkq6@lists.postgresql.org X-Gm-Message-State: AOJu0Yw5sgleUT/XoGK4R7gctHQzJTX4ByAEL922IPmyCwQKNQv15Aoy KYbbuvDvGApgzzDPS2bg32LiJYjovCzz6KsI9F2iSjrkKIfWDNW/+ASt X-Gm-Gg: ASbGncuHmMY0MR6/fhyRJX2J6GTrB0UEGn9qjELdvWMHAIS3R1eWxUNeyhmXIX+3kIW UZcslQ4oc727B6WI02CqPgS8gSX+ofNxL008iCwQ1g/pLleMBMTJOevhEhyGzDi8eicaJ/VGvb7 /47DtNHF46emj35Eo2DYW/GuPgP5IH/KEAYQn3iidPJtXVWSoTazgGECcWxhjVPeVu/NvUcuZpz p25vgOS21e/z1E4/LnTxz8mvvCKGHv6Zll1M1U71H1v/vp+JoZNDNaO6YNVYBGVdNV2QQLg3wl0 SNaPHMDUsW/8zr7gEpFO6d3xkjJCpBoO5B98HyppsRU5ftInOFA7KwoPWPGiLkcYzkDW9EXCTkS AHP4DsM1HBgGxpOfHb+TfGH1GVWFRFE/d1xlt9X4t7YdI6A== X-Google-Smtp-Source: AGHT+IHPDXLp/QsD5e4aX2O8D0ssqFRYTqEJO4FsXU8nrtBPNvhWusecGIIYrRCAmkhOV9QvGySjHQ== X-Received: by 2002:a17:903:46c8:b0:234:d292:be7a with SMTP id d9443c01a7336-2429f2f52e8mr45835465ad.1.1754487873760; Wed, 06 Aug 2025 06:44:33 -0700 (PDT) Received: from localhost ([2804:14d:328a:a59c:c4e2:d5bb:c52d:bca9]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-241e8aabda2sm160711875ad.163.2025.08.06.06.44.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 06 Aug 2025 06:44:33 -0700 (PDT) Content-Type: text/plain; charset=UTF-8 Date: Wed, 06 Aug 2025 10:44:29 -0300 Message-Id: To: "Richard Guo" , "Robert Haas" Cc: "Tom Lane" , "Tender Wang" , "Paul George" , "Andy Fan" , "PostgreSQL-development" , Subject: Re: Eager aggregation, take 3 From: "Matheus Alcantara" MIME-Version: 1.0 References: <87il22cj51.fsf@163.com> In-Reply-To: Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed Aug 6, 2025 at 4:52 AM -03, Richard Guo wrote: > On Thu, Jul 24, 2025 at 12:21=E2=80=AFPM Richard Guo wrote: >> This patch no longer applies; here's a rebased version. Nothing >> essential has changed. > > Based on some off-list testing by Matheus (CC'ed), several TPC-DS > queries that used to apply eager aggregation no longer do, which > suggests that the v18 patch is too strict about when eager aggregation > can be used. > > I looked into query 4 and query 11, and found two reasons why they no > longer apply eager aggregation with v18. > > * The has_internal_aggtranstype() check. > > To avoid potential memory blowout risks from large partial aggregation > values, v18 avoids applying eager aggregation if any aggregate uses an > INTERNAL transition type, as this typically indicates a large internal > data structure (as in string_agg or array_agg). However, this also > excludes aggregates like avg(numeric) and sum(numeric), which are > actually safe to use with eager aggregation. > > What we really want to exclude are aggregate functions that can > produce large transition values by accumulating or concatenating input > rows. So I'm wondering if we could instead check the transfn_oid > directly and explicitly exclude only F_ARRAY_AGG_TRANSFN and > F_STRING_AGG_TRANSFN. We don't need to worry about json_agg, > jsonb_agg, or xmlagg, since they don't support partial aggregation > anyway. > I think it makes sense to me. I just wondering if we should follow an "allow" or "don't-allow" strategy. I mean, instead of a list aggregate functions that are not allowed we could list functions that are actually allowed to use eager aggregation, so in this case we ensure that for the functions that are enabled the eager aggregation can work properly. > * The EAGER_AGG_MIN_GROUP_SIZE threshold > > This threshold defines the minimum average group size required to > consider applying eager aggregation. It was previously set to 2, but > in v18 it was increased to 20 to be cautious about planning overhead. > This change was a snap decision though, without any profiling or data > to back it. > > Looking at TPC-DS queries 4 and 11, a threshold of 10 is the minimum > needed to consider eager aggregation for them. The resulting plans > show nice performance improvements without any measurable increase in > planning time. So, I'm inclined to lower the threshold to 10 for now. > (Wondering whether we should make this threshold a GUC, so users can > adjust it based on their needs.) > Having a GUC may sound like a good idea to me TBH. This threshold may vary from workload to workload (?). > > With these two changes, here are the planning and execution time for > queries 4 and 11 (scale factor 1) on my snail-paced machine, with and > without eager aggregation. > > query 4: > -- without eager aggregation > Planning Time: 6.765 ms > Execution Time: 34941.713 ms > -- with eager aggregation > Planning Time: 6.674 ms > Execution Time: 13994.183 ms > > query 11: > -- without eager aggregation > Planning Time: 3.757 ms > Execution Time: 20888.076 ms > -- with eager aggregation > Planning Time: 3.747 ms > Execution Time: 7449.522 ms > > Any comments on these two changes? > It sounds like a good way to go for me, looking forward to the next patch version to perform some other tests. Thanks -- Matheus Alcantara