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 1tbQpz-00AfQM-Fa for pgsql-hackers@arkaria.postgresql.org; Fri, 24 Jan 2025 21:03:37 +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 1tbQgg-00HCB3-SI for pgsql-hackers@arkaria.postgresql.org; Fri, 24 Jan 2025 20:53:58 +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 1tbQgg-00HCAu-HY for pgsql-hackers@lists.postgresql.org; Fri, 24 Jan 2025 20:53:58 +0000 Received: from mail-ej1-x62b.google.com ([2a00:1450:4864:20::62b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tbQgc-001KWP-3A for pgsql-hackers@postgresql.org; Fri, 24 Jan 2025 20:53:57 +0000 Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-aa67ac42819so405135366b.0 for ; Fri, 24 Jan 2025 12:53:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737752035; x=1738356835; darn=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=oIg1Dl7mxxPTea5ePxVw1+gMB1m9lq3tQg4WIufkElk=; b=cz+RgSRK7/bhUqQhyfFA598hLigCeHOIC7CshHPTWwa47Zh2doYW4cr156yDYoEC8T msSgPWWOH128CgfBMGogCvSYI/PrjB5fe47pB8S4ysUIXdXcPj5w83iOKrajdud53MgR an5UrYRgtFxMjPECVVmR3mbIxC7uAPsnWtZ3Ie3EdSqF2ScgBFkP5sSCfXrwgRCdPw/i pmaocFhVdNTd7njXYX6fgL509udSFQwZIbIQapbagTJJ0YIs+FmuMSDjBAqN2vdxA6Nw txF9/yqra+90aZC7ZbqaXMOEhwYHiWKZBquY4USG5BCM3Z8KJlOcGOHQc7MqCE22xwEN 1epQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737752035; x=1738356835; 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=oIg1Dl7mxxPTea5ePxVw1+gMB1m9lq3tQg4WIufkElk=; b=CkDO6ukjqSejkFuzj3J023WQ7A1V3tKZCS7aRBIp6wKsVmxVPh3C2+vTAGNOXpw60A 9Dlg8H+JcN5QEbG+wYPee9qRtNjaoGt/ho+W7H3fY9BjnEQ3Dcvtis3Le7z6n/qJSxa3 1cECk4Njwi9CwPWCDPeUZdG8frQolfo2SZ22OwrKbMOMKz0GLfgW2vL+eyV1cFxQHLYx dwyvuRGYuX1H+e/UIqKJWIyPs/vu3/Ol12C1xohR7r7WDnq+HczJwHHSy3CoXJJIaQVd B4CBQMtIp8MEnLQS9gZsoA/dP30CtzhI2xt5dfk8KBi9p83MDuIN3PLS3JRldt6Pmipm kzxg== X-Forwarded-Encrypted: i=1; AJvYcCU4jZO+j9koQbxGD4v23TvxxwBpJ4tufYgLPjOpdeucM1QbO4napwqV7bnLLWmw7LWWQkcDUzCKW44A7aqX@postgresql.org X-Gm-Message-State: AOJu0YzZ1paxWYpVlPG77aoG6xyv68c5Dko8bwHn+aQ/+Pxe0vhyOPGZ dLynHydW4UvNwys7jKFO7O3Sc4tQw41lXshNfiXPVpIJy5u5Ar8lv095i5hRaznkxdVd0/rmAri tcs+U8p1Jg4ZaJWtjNy/pWrVWDWPOdg== X-Gm-Gg: ASbGncvCplCIFtNtxmJukrcwaCud+piX12V01E7932+iOgEvKTuFbcjCg2o+VF9qrZK WNOVXrPmMx2C5TTCpZzRzAWWA0XSA2eFie0JCWlHBr8v0BJN0+hO2Or8rMkQFFQ== X-Google-Smtp-Source: AGHT+IE67OUTjJbGIOLmSXGtBeVDwMbGWDSircmptXk+9t/8ynR1kUMz0qLSjQ+rKdPiKJt48aMRhX+bLdyRGExmakM= X-Received: by 2002:a17:907:7da5:b0:ab2:fefe:7156 with SMTP id a640c23a62f3a-ab38b3af754mr3252582566b.43.1737752034366; Fri, 24 Jan 2025 12:53:54 -0800 (PST) MIME-Version: 1.0 References: <87il22cj51.fsf@163.com> In-Reply-To: From: Robert Haas Date: Fri, 24 Jan 2025 15:53:42 -0500 X-Gm-Features: AWEUYZk-wvjZyCECvSB_V94Mou-fb5eH6ih7Ry4f9L58DR9Z9Xodjjwtb090gM4 Message-ID: Subject: Re: Eager aggregation, take 3 To: Richard Guo Cc: Tom Lane , Tender Wang , Paul George , Andy Fan , PostgreSQL-development , pgsql-hackers@lists.postgresql.org 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 Wed, Jan 22, 2025 at 1:48=E2=80=AFAM Richard Guo wrote: > This approach would require injecting multiple intermediate > aggregation nodes into the path tree, for which we currently lack the > necessary architecture. As a result, I didn't pursue this idea > further. However, I'm really glad you mentioned this approach, though > it's still unclear whether it's a feasible or reasonable idea. I think the biggest question in my mind is really whether we can accurately judge when such a strategy is likely to be a win. In this instance it looks like we could have figured it out, but as we've discussed, I fear a lot of estimates will be inaccurate. If we knew they were going to be good, then I see no reason not to apply the technique when it's sensible. > I don't have much experience with end-user scenarios, so I'm not sure > if it's common to have queries where the row count increases with more > and more tables joined. I don't think it's very common to see it increase as dramatically as in your test case. > > To be honest, I was quite surprised this was a percentage like 50% or > > 80% and not a multiple like 2 or 5. And I had thought the multiplier > > might even be larger, like 10 or more. The thing is, 50% means we only > > have to form 2-item groups in order to justify aggregating twice. > > Maybe SUM() is cheap enough to justify that treatment, but a more > > expensive aggregate might not be, especially things like string_agg() > > or array_agg() where aggregation creates bigger objects. > > Hmm, if I understand correctly, the "percentage" and the "multiple" > work in the same way. Percentage 50% and multiple 2 both mean that > the average group size is 2, and percentage 90% and multiple 10 both > mean that the average group size is 10. In general, this relationship > should hold: percentage =3D 1 - 1/multiple. However, I might not have > grasped your point correctly. Yes, they're equivalent. However, a percentage to me suggests that we think that the meaningful values might be something like 20%, 50%, 80%; whereas with a multiplier someone might be more inclined to think of values like 10, 100, 1000. You can definitely write those values as 90%, 99%, 99.9%; however, it seems less natural to me to express it that way when we think the value will be quite close to 1. The fact that you chose a percentage suggested to me that you were aiming for a less-strict threshold than I had supposed we would want. > Yeah, as you summarized, this heuristic is primarily used to discard > unpromising paths, ensuring they aren't considered further. For the > paths that pass this heuristic, the cost model will then determine the > appropriate aggregation and join methods. If we take this into > consideration when applying the heuristic, it seems to me that we > would essentially be duplicating the work that the cost model > performs, which doesn't seem necessary. Well, I think we do ideally want heuristics that can reject unpromising paths earlier. The planning cost of this is really quite high. But I'm not sure how far we can get with this particular heuristic. True, we could raise it to a larger value, and that might help to rule out unpromising paths earlier. But I fear you'll quickly find examples where it also rules out promising paths early. A good heuristic is easy to compute and highly accurate. This heuristic is easy to compute, but the accuracy is questionable. --=20 Robert Haas EDB: http://www.enterprisedb.com