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 1umjRo-002kMU-U9 for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Aug 2025 01:41: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 1umjRl-00BCvL-S6 for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Aug 2025 01:41:34 +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 1umjRl-00BCv5-C2 for pgsql-hackers@lists.postgresql.org; Fri, 15 Aug 2025 01:41:33 +0000 Received: from mail-yb1-xb34.google.com ([2607:f8b0:4864:20::b34]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1umjRj-000dih-0c for pgsql-hackers@postgresql.org; Fri, 15 Aug 2025 01:41:32 +0000 Received: by mail-yb1-xb34.google.com with SMTP id 3f1490d57ef6-e932c984249so521537276.1 for ; Thu, 14 Aug 2025 18:41:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755222090; x=1755826890; 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=rCrCYE9MqeDo6VJzVgfYpEx1m7xQPEO3BYT8mPTeanc=; b=ISzVJePiv3GHqj4GMOtrpVOFn7Je9U5zvnZoLQt+Law1MM2Vc8mw6fRC0wBTeEkJpM 3yW7aWermdV29NT9+xFAiOTwLoMOXq9FFuDcSlgFSE9/Kj3GF5F4C+eC3WxKEtpKXz2+ tc8ESnsBQ68M149Ov/qAyXnkAIh9Pv7kScC3JGUD/Kxm4s6R4Lm1D+GNg2FfTbpIUILp WSHxTlSYetR+Qcv++NgiFIw4wAxYkV3sJZZU/61d+W8aGvV83rTqOIziL9TUFa79OJ2s hASRRxKB7DV8+vRlsN8HH1iWwsAE/N5prInCtTNEYEVW3APtK4M/imJg4bJt7R3poqGC BirQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755222090; x=1755826890; 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=rCrCYE9MqeDo6VJzVgfYpEx1m7xQPEO3BYT8mPTeanc=; b=KNhZmR9eC3qmW6lr8L3niPdQ44Ak7XPtvwHwWAzCe8i5mLQZ28W7CpaDX4TbBKi2Cl ILQuxxcZYXurcERnEsOlNrd36NEed2KqCkqF3Sx/1Wpm9ieqcFe1QgNxpu8j3TgZcYGg u0GzKEKcF++d9LjM+LW1qTvDssHSgiTl4PIEfUEoAwsXxewKWbim8QaJQOfNaeBjbY1t X7YvQegyibrBsqTwsddGIPJUFdBg3uYt1a9gjUPx3QxuDwplpjxPekQ/wjgybgzOHL9R s+JHyA5s67cWWmVsjKdpid/OqDSzAEBaQ3wWckOhRTnUXlTnWUn0MLU/HtIy9iATzfOA H0gw== X-Forwarded-Encrypted: i=1; AJvYcCUPk6GtlRWwe8QuGObp4i6lv4jjLvu0Wgb3tAN98NVa33rjwkdK7kk4ltOThMMdDR55IqJsEqS5ePmzOIu/@postgresql.org X-Gm-Message-State: AOJu0YxH3BGgC/zM8yfElKWUhOwGiYqGYM8Hej7WZJSGV/y5k72KSs5R j+FUN4N5Sz9xVYwZdeE6ZrePm/aP7FDsUhx5ea7adc/lFp9Jc8dRfVWOGmgZzvbjMitqsrWe/ki d4ct0GT+TVWNkr9110y4HzQcHzZF+b/o= X-Gm-Gg: ASbGnctANCiyHAX22Ce//wzuceyJdcuFmnGzaTxUabUTpo5TrjfG3UvT/x2QtRcDVRS jfNJfiS/tqzwtIXigwmNYMZLu5Iikl9RDbJ7Azc6lmcKgnt4gc/hooetZ+fAU0r22rrhl7JJpOX VcFamDtTGDJ4sizp6B+eG1vYdbf485QOX1JvCYyKAA+X1ySwuRNV3CKeXfTxs3qmK6rcakFTDy+ Jc0EprCAA== X-Google-Smtp-Source: AGHT+IFAg2D+02gDK5ai5Lw833uUjUCBPVszsUu0x7fYqKDWiBgG+5HdzSVvfO262UAsVgM77C+QJUqcKwm0S9Lhfik= X-Received: by 2002:a05:6902:140d:b0:e90:6b79:23b3 with SMTP id 3f1490d57ef6-e93323b0151mr485152276.12.1755222089916; Thu, 14 Aug 2025 18:41:29 -0700 (PDT) MIME-Version: 1.0 References: <87il22cj51.fsf@163.com> In-Reply-To: From: Richard Guo Date: Fri, 15 Aug 2025 10:41:18 +0900 X-Gm-Features: Ac12FXznfbRMB3QAUGiJpHQVPMmvFlOSRhrJDOZ9JQq9Vc_9OiVvYLNYk6ZSeaI 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, Aug 15, 2025 at 4:22=E2=80=AFAM Matheus Alcantara wrote: > Debugging this query shows that all if conditions on > setup_eager_aggregation() returns false and create_agg_clause_infos() > and create_grouping_expr_infos() are called. The RelAggInfo->agg_useful > is also being set to true so I would expect to see Finalize and Partial > agg nodes, is this correct or am I missing something here? Well, just because eager aggregation *can* be applied does not mean that it *will* be; it depends on whether it produces a lower-cost execution plan. This transformation is cost-based, so it's not the right mindset to assume that it will always be applied when possible. In your case, with the filter "t2.c =3D 5", the row estimate for t2 is just 1 after the filter has been applied. The planner decides that adding a partial aggregation on top of such a small result set doesn't offer much benefit, which seems reasonable to me. -> Hash (cost=3D18.50..18.50 rows=3D1 width=3D12) (actual time=3D0.864..0.865 rows=3D1.00 loops=3D1) Buckets: 1024 Batches: 1 Memory Usage: 9kB -> Seq Scan on eager_agg_t2 t2 (cost=3D0.00..18.50 rows=3D1 width= =3D12) (actual time=3D0.060..0.851 rows=3D1.00 loops=3D1) Filter: (c =3D '5'::double precision) Rows Removed by Filter: 999 With the filter "t2.c > 5", the row estimate for t2 is 995 after filtering. A partial aggregation can reduce that to 10 rows, so the planner decides that adding a partial aggregation is beneficial -- and does so. That also seems reasonable to me. -> Partial HashAggregate (cost=3D23.48..23.58 rows=3D10 width=3D36) (actual time=3D2.427..2.438 rows=3D10.00 loops= =3D1) Group Key: t2.b Batches: 1 Memory Usage: 32kB -> Seq Scan on eager_agg_t2 t2 (cost=3D0.00..18.50 rows=3D995 width= =3D12) (actual time=3D0.053..0.989 rows=3D995.00 loops=3D1) Filter: (c > '5'::double precision) Rows Removed by Filter: 5 > Is this behavior correct? If it's correct, would be possible to check > this limitation on setup_eager_aggregation() and maybe skip all the > other work? Hmm, I wouldn't consider this a limitation; it's just the result of the planner's cost-based tournament for path selection. Thanks Richard