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 1uuXZB-00C98C-1n for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Sep 2025 14:37:30 +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 1uuXZ9-008BPQ-Ow for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Sep 2025 14:37:28 +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 1uuXZ9-008BPG-Dm for pgsql-hackers@lists.postgresql.org; Fri, 05 Sep 2025 14:37:27 +0000 Received: from mail-ej1-x634.google.com ([2a00:1450:4864:20::634]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uuXZ6-000j93-0x for pgsql-hackers@postgresql.org; Fri, 05 Sep 2025 14:37:27 +0000 Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-afcb7322da8so440854066b.0 for ; Fri, 05 Sep 2025 07:37:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1757083043; x=1757687843; 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=SWlgQ8FP5cNeBpLzkmg8/BBjtbfu6vWQIrOdfeVpPw8=; b=CC7sgerjH5p+/DGtu4mIBIvfSZk3s/p4nBs1qfrM1XB56V+38kApsfCtQU11EwOyg3 E+8f/haxkHbqNui5Vo65FVZ3uGxQ9wxCnmeUAPcSQUfx6O8EmslvBAEqF3566QXai0o6 UN6uIKMUytYY5odvS90rozqy4pWYRWvNJT4/NNUJldqqyJaOt9L17tFUu7X3DMrhVSXW 67pXoauIGFRCnHr6ye+uH5nuTSwROd59O8xvcvfFvUoZ7E87x76tRw1oqSEWeiNg6lbv TtGDkZPRnxuTB92xyxHg1Cvq7WtiW85ip2qLSjA0kABiCdxFDf2sdxoGUwmEfc4btEbo nq4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757083043; x=1757687843; 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=SWlgQ8FP5cNeBpLzkmg8/BBjtbfu6vWQIrOdfeVpPw8=; b=maKwTC6DZb5i56mLcEuEcH2PjrfBBNa5V0IY3vCnpEMxJPI0ZY9+R06LZUWsfl61Ew K8oeNZ0yspHIFEk+JPItEUc5UI3kaz/1ZiJp+J8/H0gS2Vzm51PL688BZdNBYcbRqlVv APXGK5oVDHSPFof2eHNs51GTZlDHuD2rYP7A+PdT16EupNFhxcUfAKlSCKviQp5+SkAa W3syt4G1wyuJVfUZstfJKPjJNgiMmOn4MkVOki/cRb7VgB/8jvB2Cq5Tw+vr9fqH9Pmn VqoRFZGJ6G9t6A8R3WeCvL7RtzBrg/OvIEplHgQhWCX9QLjJWwvEzVe1+ZSxxHSxX/sG Y59A== X-Forwarded-Encrypted: i=1; AJvYcCXYbnTj9kznpMqdnK4c7yUyeAAk9AXL2PBGEpDzK3Dp8EburLKSuZB0R4AgSykrpwQ8XU4KDz0LAhinoI3l@postgresql.org X-Gm-Message-State: AOJu0YwmvNReAgEMZ3aAuUNWtLBumfbW+kT+RTVP2rHBjwOYS7Hd2klG C+FSULvJB13+Gv+VaMfzfcO7UlWNC6d8R/4w3OLDr+aCFVix2yHY/vlb4xEsJJsEXdR9c8UtckM SzEuwz/CLW61KnborlTHM01CnXJJ6tW4= X-Gm-Gg: ASbGnctIEuiXSdpONWJHKxcsNUBe0zfQNsX0InNfQY2fPU5VkUwnTDXbxlO/aaiOhGG 3nFKNpCaWDuUF4D6gXnh6moGtzE0dNCs/aqPDJeMo59y8u/JCWrDXMEXHrKKAZCefepXKnZAvyP d6uWFGYk8/1HDSzUKlctOyvhZhmh881+mQNj/AXy2Tc3q656QUBQtrOx64UEzOMI9hrig6lQW7S qAI3wki X-Google-Smtp-Source: AGHT+IHYHHMyiCbq/JOvuADSbB+oYq0EW1/0EmBI39ksBTagNodW2ciyPR2qahYvWVFVdez8VW7S74SI6Wf4NScYG5o= X-Received: by 2002:a17:907:9705:b0:b04:5a74:b674 with SMTP id a640c23a62f3a-b045a74c3e0mr1275580266b.58.1757083042603; Fri, 05 Sep 2025 07:37:22 -0700 (PDT) MIME-Version: 1.0 References: <87il22cj51.fsf@163.com> In-Reply-To: From: Robert Haas Date: Fri, 5 Sep 2025 10:37:10 -0400 X-Gm-Features: Ac12FXzeLKexmx1ukNguAN4OY0rqHoytz7oPRBZJHPbVXt7unh1cRWKzDPrSin0 Message-ID: Subject: Re: Eager aggregation, take 3 To: Richard Guo Cc: Matheus Alcantara , 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 Fri, Sep 5, 2025 at 3:35=E2=80=AFAM Richard Guo = wrote: > Here is a rebase after the GUC tables change. I spent a bit of time scrolling through this today. Here are a few observations/review comments. It looks as though this will create a bunch of RelOptInfo objects that don't end up getting used for anything once the apply_at test in generate_grouped_paths() fails. It seems to me that it would be better to altogether avoid generating the RelOptInfo in that case. I think it would be worth considering generating the partially grouped relations in a second pass. Right now, as you progress from the bottom of the join tree towards the top, you created grouped rels as you go. But you could equally well finish planning everything up to the scan/join target first and then go back and add grouped_rels to relations where it seems worthwhile. I don't know if this would really make a big difference as you have things today, but I think it might provided a better structure for the future, because you would then have a lot more information with which to judge where to do aggregation. For instance, you could looked at the row counts of any number of those ungrouped-rels before deciding where to put the partial aggregation. That seems like it could be pretty valuable. I haven't done a detailed comparison of generate_grouped_paths() to other parts of the code, but I have an uncomfortable feeling that it might be rather similar to some existing code that probably already exists in multiple, slightly-different versions. Is there any refactoring we could do here? Do you need a test of this feature in combination with GEQO? You have code for it but I don't immediately see a test. I didn't check carefully, though. Overall I like the direction this is heading. I don't feel well-qualified to evaluate whether all of the things that you're doing are completely safe. The logic in is_var_in_aggref_only() and is_var_needed_by_join() scares me a bit because I worry that the checks are somehow non-exhaustive, but I don't know of a specific hazard. That said, I think that modulo such issues, this has a good chance of significantly improving performance for certain query shapes. One thing to check might be whether you can construct any cases where the strategy is applied too boldly. Given the safeguards you've put in place that seems a little a little hard to construct. The most obvious thing that occurs to me is an aggregate where combining is more expensive than aggregating, so that the partial aggregation gives the appearance of saving more work than it really does, but I can't immediately think of a problem case. Another case could be where the row counts are off, leading to us mistakenly believing that we're going to reduce the number of rows that need to be processed when we really don't. Of course, such a case would arguably be a fault of the bad row-count estimate rather than this patch, but if the patch has that problem frequently, it might need to be addressed. Still, I have a feeling that the testing you've already been doing might have surfaced such cases if they were common. Have you looked into how many queries in the regression tests, or in TPC-H/DS, expend significant planning effort on this strategy before discarding it? That might be a good way to get a sense of whether the patch is too aggressive, not aggressive enough, a mix of the two, or just right. --=20 Robert Haas EDB: http://www.enterprisedb.com