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 1ujYxB-009CoJ-8T for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Aug 2025 07:52:53 +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 1ujYx9-00EIdT-S8 for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Aug 2025 07:52:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ujYx9-00EIdB-ES for pgsql-hackers@lists.postgresql.org; Wed, 06 Aug 2025 07:52:51 +0000 Received: from mail-yb1-xb36.google.com ([2607:f8b0:4864:20::b36]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ujYx6-00106g-06 for pgsql-hackers@lists.postgresql.org; Wed, 06 Aug 2025 07:52:50 +0000 Received: by mail-yb1-xb36.google.com with SMTP id 3f1490d57ef6-e902849978bso554844276.1 for ; Wed, 06 Aug 2025 00:52:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754466768; x=1755071568; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=EepWLHoDHpznxv+qVqR6gL6MWkQUJzEeGwnDBQyaPoA=; b=j+HmSMBwnZikEAZFNjLHs55t2j/4S0SCa9oW8jbFWXTXozt77UCn7EdgL+kDQFZw6L hl1c3rj13g7bc5cYALyxdFlI5/tyefZ+BxzQhWRNI+8VG5OhZ2Hp2i0NuJ6JYl6czTOM 7De8+YbUoJlnGZKW6sX+jXQMKUi+lzAI0cW9dM9EzoYl3igyUN4iL4LwrYzqbF4oQ1cM qxHLsvss3zFrX0EfklDY1xeWf1jQ7AvCWtfJwqTEEeVsm3z1TKNXX4mq/4oiDeJILl4b IgqwUZSDHYoqtFj5FTIMi3TFcl1M9EULlBS1YVYI3MhpHU6ZMqC1d/KKrmCR3aERMVal FK8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754466768; x=1755071568; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EepWLHoDHpznxv+qVqR6gL6MWkQUJzEeGwnDBQyaPoA=; b=P5EvhHnN9xsc1l3xhKKOBpFKnEW4yVkVY8INJKIq5QAXrvzSHkTbBjvbWR7k58vdva tj3GiKviW9gTekKG52D6sz31YTTCqlBOs1YYwZ0Lnasi+I4t8t3F8m1syvDOhjWNECcy bAoVKCcm60nvESGMEToLEx9Tl9SltJh9egsP05x8aGE+5TGPoenx3HHSSj34z9/CKTT/ 3uWKiqN3amg8Yzk/4SS9YVzH882SNBAWj9lgP7OOsnDERUDlf54fsbk//nf8EkAZP0Xc HEzb2foKGT/PqB42VIWlIoJRDVpoSLF7oWsH28VO2W0cZYCYQGp0YombRZBz34+zppTW +rAA== X-Forwarded-Encrypted: i=1; AJvYcCU+7aeyvREbzZ17O0KTdyjYUldXI9N/uejLXJAI4uK1a5KD0BIgK/sUCYs0LblhJmbIMxUJNhCOEy9Xgb7Z@lists.postgresql.org X-Gm-Message-State: AOJu0Yzx0MLSBTisAe8nH9cPg6KnoAux9QJfoQ4TdJ44YoFqj2YJfKEZ lVAQwEE3T3ITRptNBlMYhM7aXPEmxiiCxjRezfQhmMAKN24lH8I+q6B0WyASaYNO9QDfa5ceGUp WN83lZGbckN9Ob/wUOSoYvkkNkT02cVM= X-Gm-Gg: ASbGncvzRPWt9dFMwBFInj80tBgCB4vTW5sbkEXq6kQPWltE9zHJVCV3RviYMvyhqGh rRd3Tc6fLOD7lr/BsjLdyhYucJogmh0jSCHadVfEpTDfJwQasdDd392pirmtnIBRaJH0Jnzl+Nb iN8tEjWuR1eaYJf9Nw0qFiXn0uTS48Bva6rrq9LWOUlyKq/kwji5Fief9clT9HdsTuvgWcGWA/l OMB2+fUz8416UpwagawQdjm9yGFM/CmQG6gARdfig== X-Google-Smtp-Source: AGHT+IEpJ2bctogvE7+yUApFvdGa9WNEOlpGrI/UHvhLar3xKBSsszOSR4ummQaJ7WM/3ck5FJj5fUfgnNSLaZep5Ao= X-Received: by 2002:a05:6902:6d04:b0:e8e:c03:75ac with SMTP id 3f1490d57ef6-e90288465fdmr2488790276.7.1754466768126; Wed, 06 Aug 2025 00:52:48 -0700 (PDT) MIME-Version: 1.0 References: <87il22cj51.fsf@163.com> In-Reply-To: From: Richard Guo Date: Wed, 6 Aug 2025 16:52:36 +0900 X-Gm-Features: Ac12FXxgCK68qx4ON3_9rCsLgED9EjXH7MiOwovITgKOGCfAXMD6hgOQrxtv6pQ Message-ID: Subject: Re: Eager aggregation, take 3 To: Robert Haas Cc: Tom Lane , Tender Wang , Paul George , Andy Fan , PostgreSQL-development , pgsql-hackers@lists.postgresql.org, Matheus Alcantara Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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. * 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.) 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? Thanks Richard