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 1v4WG5-00HVdF-AZ for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Oct 2025 03:15:01 +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 1v4WG1-00AavQ-Hn for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Oct 2025 03:14: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 1v4WG1-00Aav9-6W for pgsql-hackers@lists.postgresql.org; Fri, 03 Oct 2025 03:14:57 +0000 Received: from mail-yw1-x1132.google.com ([2607:f8b0:4864:20::1132]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v4WFy-0007oC-2r for pgsql-hackers@lists.postgresql.org; Fri, 03 Oct 2025 03:14:57 +0000 Received: by mail-yw1-x1132.google.com with SMTP id 00721157ae682-7501c24a731so20533017b3.3 for ; Thu, 02 Oct 2025 20:14:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759461291; x=1760066091; 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=kTc+T3VIeTSFlRRSlzrfjxMNyc+wWWIReCvzcmHnR4k=; b=ZIEW818yi8LZ5NQaxqH8kyAIigZpFrNGdb/rGE1vdwurykdmGm1ygH0CNIWs0pPge3 uDUG5UgetqRBsJav3j93A6IWKdwoFT7sndRIY5+AORt8WzShW2RIcK9leQFTol/d3Q5K j5yH087Spxf4MKCyiQxxdyfOtYyjHJD6joZtYju4WmYae6oEr7DUZZ5qEJ7ASx5+pIgC rnJizOoGuGRCsMOM9SILUzTqqVt2F0rCukYnzyB+W6DVaTPADyOP709/W3IXF8v/HyJK UfFLE72smAtypOGdwKQy2NCIxvHU+bGCmVxdKBXXG8zolwb5EMGEQ6W6UN0c1jiq1vVw zpzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759461291; x=1760066091; 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=kTc+T3VIeTSFlRRSlzrfjxMNyc+wWWIReCvzcmHnR4k=; b=mx606Gnh1Tl5zAv7oy9jBf81utMkHOBFVem6FtYEny80FaEtbgKOKKlkr/0vt001Sg pOp0DwzcGD/N8K3WsEQYmymlYsQrL49GCWwF5KTOxoGDYHSzFtyAHDDwNidFMNJbwPeB g4t9QbwE7rkt+NXGokvhyWuQozlIxOQ2sy1Nh2+HZsUxBWcXgbWXg2l0xQo6zDQiodE0 g8TrJcskQcIT9Sz3WrPTS05YboLMNlg0qH5lppzOnesPvLqOuDKcdUTDHk0EuJYW4rv/ yBtgB/Gviafx9SI9MiJEB64ESdHJhDZkFZaGtbPeihotRvBNoJrtoau1C3HbRwLgb7dK HHLQ== X-Forwarded-Encrypted: i=1; AJvYcCW03vVQl8t7ZHqViphqU3LwogkAHMKJ9YYE8/gIYRWWYN+LNqcDWQdcc6WS/CaQHBigGMWfqz5hWFn/bDab@lists.postgresql.org X-Gm-Message-State: AOJu0YwsR96yNJ7rfjTSNo3eMsl3pPH13AvYkfPdit1yVOuoFrNExOAK 8XGKsWw81h+zDwQ84PLkjGcLwhjGJu0r7wMVGLqnBW9z1qut9hKiSpWmLrefIu0uQy1dfOjAXH5 YXEkjLO5+g3cEw2RWUN/6SSR984lnI5c= X-Gm-Gg: ASbGncs+k7XE4nGr11kN4YR3jveymdU7flkzQFr+lUwX1eU3ATYeCcGpdiPFjRLOINz 9IJLnetI44YLSNXk6HjElIrO6w6YceOCk37d2Wa4ivDLQtG+l+bAYHPcaItD5pfG8rkspVkaWbM ZbPDtXuIvac8kVLWSJizmxhFBG0Ot8ShJElkk2PdllDNvtXkd4AH8MUDtTlVO+MQpSQqXoLAYSy 2/6qJ1pRDpieBc6vpa+k25+xt2A2MuckGBRS1SYBIh1 X-Google-Smtp-Source: AGHT+IEmRZuI0viw4CzZI7cRG/TLhzFDpMoyITxfhFVt18AQ/nBD+RL+LiVFopCPTWlDkcxfK+K5VNdgOHqNBdChs9w= X-Received: by 2002:a53:5a11:0:b0:635:4ed0:5725 with SMTP id 956f58d0204a3-63b9a105edbmr1269694d50.41.1759461291246; Thu, 02 Oct 2025 20:14:51 -0700 (PDT) MIME-Version: 1.0 References: <87il22cj51.fsf@163.com> In-Reply-To: From: Richard Guo Date: Fri, 3 Oct 2025 12:14:40 +0900 X-Gm-Features: AS18NWC-LtkEkjlx29sRl4amubgTFTvRnL92Ia5rvlrDdqQmx9f_EqJBPTbmeXg Message-ID: Subject: Re: Eager aggregation, take 3 To: Matheus Alcantara Cc: Robert Haas , 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, Oct 3, 2025 at 3:41=E2=80=AFAM Matheus Alcantara wrote: > Thanks for all the details. I've disabled the nested loops and executed > the benchmark again and the results look much better! I see a 55% > improvement on query_31 on my machine now (MacOS M3 Max). Great! That is 2.23 times faster. > The only query that I see a considerable regression is query 23 which I > get a 23% worst execution time. I'm attaching the EXPLAIN(ANALYZE) > output from master and from the patched version if it's interesting. I tested query 23 in my local environment but didn't observe the regression. -- on master Planning Time: 1.950 ms Execution Time: 3260.924 ms -- on patched Planning Time: 2.197 ms Execution Time: 3237.287 ms I ran the benchmark at scale factor 1 and executed ANALYZE beforehand. For the build configuration, I disabled cassert. Comparing the plans, I noticed one key difference: in the plan you provided (query-23.patch.explain), the frequent_ss_items CTE uses parallel aggregation, whereas in my local environment it does not. This leads to a different final join order between the two plans. However, given the highly inaccurate size and cost estimates for the CTE Scan nodes, I'm not sure it's worth investigating further. I'm starting to feel that trying to tune performance here, with such inaccurate underlying estimates for CTEs, is like building on sand. > I'm also attaching a csv with the planning time and execution time from > master and the patched version for all queries. It contains the % of > difference between the executions. Negative numbers means that the > patched version using eager aggregation is faster. (I loaded this csv on > a postgres table and played with some queries to analyze the results). I really appreciate this; it's very helpful. > I'm just wondering if there is anything that can be done on the planner > to prevent this type of situation? I think the ideal solution is to improve our estimates for CTE relations to make the plans for TPC-DS queries more reasonable. Of course, for queries from other benchmarks, the issues may stem from other plan nodes. IMHO, we really need some improvements in our cost estimation. - Richard