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 1uuWCm-00BXka-9c for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Sep 2025 13:10:17 +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 1uuWCl-007Wqr-3T for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Sep 2025 13:10:15 +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 1uuWCk-007Wqb-MG for pgsql-hackers@lists.postgresql.org; Fri, 05 Sep 2025 13:10:15 +0000 Received: from mail-ej1-x62b.google.com ([2a00:1450:4864:20::62b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uuWCi-000gOs-2A for pgsql-hackers@lists.postgresql.org; Fri, 05 Sep 2025 13:10:14 +0000 Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-aff0775410eso426514666b.0 for ; Fri, 05 Sep 2025 06:10:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1757077811; x=1757682611; 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=iaDuzOS3ScRtFc0vNYjUnKmOrx38chXjVsahVfu3xiE=; b=AITolXyI/Q0ME4R/8HZkW0SuSLYCxxSMdDt6skCF180WLFoWDTNvdujfswb/f0C+dM E2klSdyQNOjiRgYpS+WDi46LpOUWMsmHJ0V1JFDJe9vJgnfn5H43f3vaxQKrfhooaORt XXrPssfWnHWfaSbAEyU9VzJFDV18kiZ5EufICe31sKh1es1aMv+ph/DaDB1GrUDOvkDG wqhaYrzAizpd83En9bTP2p++xLHivsbRtFWX8jpwtYYuok9DW/SrvpUhuq9yBVzo5aCo P3OM/eljnyBKMJWhf/+vzRweXk+TtK+RWaFy1jzPf+Rm2tQghPIc/OCXnAZYUk7Uu7im Zh/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757077811; x=1757682611; 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=iaDuzOS3ScRtFc0vNYjUnKmOrx38chXjVsahVfu3xiE=; b=bq/dp7mZKWq4V51EIxmUgosl/h2CozDhjB+f4c3ePEZiQj+ij3H9DWPGiBTS6kJADP HnOoL5y7oUnoC51rhjMBIw7AV7+FsM1DuzW8hlsjzO6n1mcOLW5NGWMIml6fwJNUSvkm QbwGDyif546RmJ27pxZiAGroAleJfNI9jymQzezg59AY64AtlOOm3eIF8WO1UkVzQoNF JxIuOhO3knbVEq0zlvmoAWUzdA/02friwm/w9fACY327UBoU0GIWEYlpGygODqJpQgQq TII1d4DO/SZR6IIc+TDCmsAx+nuEHI5329ZJbPTbQHO/MI1vvrWJ2DnREeF1msJ4DTiH ydXQ== X-Forwarded-Encrypted: i=1; AJvYcCUfWJpMC1Hij6paPaMlk1CkX0cwI+LEVzOVhMnF22ZDGmxYUevIZD2N8bZ0Vu10E2EPG+nirnu+DVjptPld@lists.postgresql.org X-Gm-Message-State: AOJu0YyCcn/B9AJsRVBdkRBjN4cFzDypOOm7IQ7KH3DY1KrLf0ERJ40y mo0PJkLqs5mEf/AU4tbRVq6SFBosgft75aa+h7FWpPAUYabyQYnXsN852tfGTmomQ4iYaap339k fLf7ufioouD24kTe/xNJmYfAGTET6P5M= X-Gm-Gg: ASbGncvr9A/jQDjgPabfhSqAiHdiZjRgvYN7J1Xz4p+xetQIvOtTcqWpWKpmTrWrQiw CtBYynnds06wSAVDO5DL0oVjuRwp4zSJAbRWSWVXYjCe/4BodNdaGUgsVe6dH3aPbwZmEpSmFW0 fGOlEwUuLSImbFa07U/0xGcnxOffAz7WUwNZCkpvnxGtbig5NFqPnKI98MOQeG0gRy74jrGydVw OOEM8oCEIpVuZ0FHQ== X-Google-Smtp-Source: AGHT+IGqcFrG6ZO9GIcXKcjP1vx5PiieRSrj07CmzlZDXWs1ZT+d4+A0LuvbXCgoSl2GT372Kd9vDOwIhIWLm4aGHs4= X-Received: by 2002:a17:906:eec3:b0:b04:3cd2:265b with SMTP id a640c23a62f3a-b0493084d84mr353112166b.5.1757077809064; Fri, 05 Sep 2025 06:10:09 -0700 (PDT) MIME-Version: 1.0 References: <87il22cj51.fsf@163.com> In-Reply-To: From: Robert Haas Date: Fri, 5 Sep 2025 09:09:56 -0400 X-Gm-Features: Ac12FXweP0X96897bIEVcIgeghkUNqobvIT0S_4OQtixIP4BGYIDtgw3zWRyFUk 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 Sorry for the slow response. On Fri, Jun 13, 2025 at 3:42=E2=80=AFAM Richard Guo wrote: > The transformation of eager aggregation is: > > GROUP BY G, AGG(A) on (R1 JOIN R2 ON J) > =3D > GROUP BY G, AGG(agg_A) on ((GROUP BY G1, AGG(A) AS agg_A on R1) > JOIN R2 ON J) > > This equivalence holds under the following conditions: > > 1) AGG is decomposable, meaning that it can be computed in two stages: > a partial aggregation followed by a final aggregation; > 2) The set G1 used in the pre-aggregation of R1 includes: > * all columns from R1 that are part of the grouping keys G, and > * all columns from R1 that appear in the join condition J. > 3) The grouping operator for any column in G1 must be compatible with > the operator used for that column in the join condition J. This proof seems to ignore join-order constraints. I'm not sure to what degree that influences the ultimate outcome here, but given A LEFT JOIN (B INNER JOIN C), we cannot simply decide that A and C comprise R1 and B comprises R2, because it is not actually possible to do the A-C join first and treat the result as a relation to be joined to B. That said, I do very much like the explicit enumeration of criteria that must be met for the optimization to be valid. That makes it a lot easier to evaluate whether the theory of the patch is correct. > To address these concerns, I'm thinking that maybe we can adopt a > strategy where partial aggregation is only pushed to the lowest > possible level in the join tree that is deemed useful. In other > words, if we can build a grouped path like "AGG(B) JOIN A" -- and > AGG(B) yields a significant reduction in row count -- we skip > exploring alternatives like "AGG(A JOIN B)". I really like this idea. I believe we need some heuristic here and this seems like a reasonable one. I think there could be a better one, potentially. For instance, it would be reasonable (in my opinion) to do some kind of evaluation of AGG(A JOIN B) vs. AGG(B) JOIN A that does not involve performing full path generation for both cases; e.g. one could try to decide considering only row counts, for instance. However, I'm not saying that would work better than your proposal here, or that it should be a requirement for this to be committed; it's just an idea. IMHO, the requirement to have something committable is that there is SOME heuristic limiting the search space and at the same time the patch can still be demonstrated to give SOME benefit. I think what you propose here meets those criteria. I also like the fact that it's simple and easy to understand. If it does go wrong, it will not be too difficult for someone to understand why it has gone wrong, which is very desirable. > I think this heuristic serves as a good starting point, and we can > look into extending it with more advanced strategies as the feature > evolves. So IOW, +1 to what you say here. --=20 Robert Haas EDB: http://www.enterprisedb.com