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 1uOa5h-006RVE-Du for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Jun 2025 10:50:57 +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 1uOa5f-00847N-96 for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Jun 2025 10:50:55 +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 1uOa5e-00847F-SD for pgsql-hackers@lists.postgresql.org; Mon, 09 Jun 2025 10:50:55 +0000 Received: from mail-vs1-xe2c.google.com ([2607:f8b0:4864:20::e2c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uOa5d-0012jU-1W for pgsql-hackers@lists.postgresql.org; Mon, 09 Jun 2025 10:50:54 +0000 Received: by mail-vs1-xe2c.google.com with SMTP id ada2fe7eead31-4e592443229so1412105137.1 for ; Mon, 09 Jun 2025 03:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749466251; x=1750071051; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=eENYSkVkGljVWrX4U2dXoyw2ezI54h+DmpI4iG7yZ2A=; b=WzFonv+IhGwUyCmqX96YNrAhBOLwb8GW71+o3cn2dmh0BcqNwI5eWpGThdCq5Ckdw2 hHJcL3Sh0fjoOGyswscCC5O19aQxyK8JeTNbrJuI7gnxeVx9Le+DZ5CEd2ahWCeY5gju 9Feqs5ekmSc1MyYaYbe3iBpTawhS6HIVsgvIIxfQ5mnKbhWrSPsoier9spjEB1jXNt+o 2cXTgNTAKH8dwoL8i2Fr26DQrFlh4It717XBW7KtTsAhHkJwX37hfLZCJnPbLyqoFO0g ecpiQpRtq1i7JR0ifqw4b0xF0sEdgCHrBbEQCZAORf8FpQw+NEwO8X+q82VDhSt+Ds74 guBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749466251; x=1750071051; h=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=eENYSkVkGljVWrX4U2dXoyw2ezI54h+DmpI4iG7yZ2A=; b=Rf1PZTZ9Pib4hN9Ji77S+WaW5q9OVpTCxmgr4fmUxqrA3svekFp+vHnEoU+xd4/UEZ mCEaM5jdHaWKnoAQA1e36QGHZQZyY6M7Q5dpy9X92b8fskatx5KHi2BGXG9II1dETem/ 4yV6FLq1IgMIBiheiiXTWEKAEggYqFacnngDUuSdlvCy3hfL8xf9lrSQFlLru1uGz3BI 0q6UFu7DQzyFsvkWNjpyR8LfMKVV9Rg4PYStT4RBfICxYGYHruH9W+iUTjNidICF+8Z0 gnkaVygLtRI9jyGKCc4fCPxyqkFyh+4s2Lo51AczlvIaiW187x+G8JdQVCrKf/LKpSs2 SS4Q== X-Gm-Message-State: AOJu0YzjqRmOSkGKTDyey5JHA32zDJlhvMbG7p2W/c5gQpsWgMa5qPE7 nAI0RpabU7WwRZs3E3wl6XabS9J8uXAxScNXYOWMi+bNyMzeZwBNJQ0ATOcaEfIiaUXg/wtU/0F S6JtcBTWr72zuNN2+x+ejLQjycdgmzKE= X-Gm-Gg: ASbGncsxZhm8qXsO34rCdLaznPAnsQptIRsBHy6eQK8dWB6V4nZ1bHQKSvQ+A4zxJJj 4GZ7Uqeh5cZZAJw2z+tawzrRURn62yRQRpGoVujKvFJPmT2OZp9Wvi2HT0gC9dpE7jm5HMrp2lg w+ldmvArsgZCoqKssaNywiUpUiWUMaECsmtjtF3dXzyizThOj7tQcdScc= X-Google-Smtp-Source: AGHT+IHS65W6gU1LeIVb9sF4o2zDOXjeUA5Il7X2WJWsKV6USyDZ8wnqK5TNIyBmOxq6a9VaWC5Zu82YTwqwpDBytro= X-Received: by 2002:a05:6102:b03:b0:4e2:a235:2483 with SMTP id ada2fe7eead31-4e772aa1019mr10206118137.19.1749466251024; Mon, 09 Jun 2025 03:50:51 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ashutosh Bapat Date: Mon, 9 Jun 2025 16:20:39 +0530 X-Gm-Features: AX0GCFu04X9fD-KXkps0vWSePXXdMznok2liHFAZoTVlNjypjxSGqlnTqw8zzSY Message-ID: Subject: Re: Add enable_groupagg GUC parameter to control GroupAggregate usage To: Tatsuro Yamada Cc: pgsql-hackers@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000705d610637215ac6" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000705d610637215ac6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Jun 6, 2025 at 1:29=E2=80=AFPM Tatsuro Yamada wrote: > Hi hackers, > > When I measured the execution time of a certain query with parallel query > enabled and disabled, I found that the execution time was slower when > parallel query was enabled. > > To improve the performance of the parallel query, I considered adjusting > the execution plan and attempted to switch from GroupAggregate to > HashAggregate. However, I noticed that there was no GUC parameter to > disable GroupAggregate. > > Therefore, I propose adding a new GUC parameter: enable_groupagg. > > Below are the results of a performance test where I disabled > GroupAggregate using enable_groupagg. In this case, the planner chose > HashAggregate instead, which improved performance by about 35 times. > > # Query Execution Results (Average of 3 measurements) > - With parallel query: 39546 seconds > - With parallel query and enable_groupagg turned off: 1115 seconds > > # Query and Data Used (attached to this email) > - Query: test_query.sql > - Data: create_table.sql > > # The steps to run the test are as follows. > For example, on psql: > > 1. Create tables: > \i create_table.sql > > 2. Execute a query: > \i test_query.sql > > 3. Execute a query using the new GUC parameter: > set enable_groupagg to off; > \i test_query.sql > > As a benefit to users, while there has previously been a GUC parameter > to control HashAggregate, there was no corresponding way to control > GroupAggregate. This patch addresses that, giving users more flexibility > in tuning execution plans. > > I've attached a WIP patch that adds this GUC parameter. I would > appreciate any feedback, especially regarding how many test cases I > should create. > I first thought enable_hashagg should be sufficient to choose one strategy over the other. But that is not true, enable_hashagg =3D true allows both t= he strategies, enable_hashagg =3D false disables just hash strategy. There's n= o way to disable group agg alone. So I think it makes sense to have this GUC. I am surprised that we didn't see this being a problem for so long. We seem to disable mixed strategy when enable_hashagg is false. Do we want to do the same when enable_groupagg =3D false as well? > > To create new test cases for enable_groupagg, I looked into existing > test cases that use enable_hashagg and found that it is used in many > places (62 places). Should I add a test case for enable_groupagg in > the same place as enable_hashagg? I think that adding a new feature > requires a minimum number of test cases, so I would appreciate your > advice. > Some of those instances are for plan stability, all of which need not be replicated. But some of them explicitly test sort based grouping. For rest of them hash based plan seems to be the best one, so explicit enable_groupagg =3D false is not needed. We will need some test to test the switch though. > > > Additionally, based on the execution plan, I suspect the slowdown in the > parallel query might be caused by misestimates related to Sort or > Gather Merge. > While resolving those misestimates would ideally improve the root issue, > I'd like to keep the focus of this thread on adding the GUC parameter. > Then, I plan to report or address the estimation problem in a separate > thread. > > +1. --=20 Best Wishes, Ashutosh Bapat --000000000000705d610637215ac6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Fri, Jun 6, = 2025 at 1:29=E2=80=AFPM Tatsuro Yamada <yamatattsu@gmail.com> wrote:
Hi hackers,
=
When I measured the execution time of a certain query with parallel que= ry
enabled and disabled, I found that the execution time was slower when=
parallel query was enabled.

To improve the performance of the pa= rallel query, I considered adjusting
the execution plan and attempted to= switch from GroupAggregate to
HashAggregate. However, I noticed that th= ere was no GUC parameter to
disable GroupAggregate.

Therefore, I = propose adding a new GUC parameter: enable_groupagg.

Below are the r= esults of a performance test where I disabled
GroupAggregate using enabl= e_groupagg. In this case, the planner chose
HashAggregate instead, which= improved performance by about 35 times.

# Query Execution Results (= Average of 3 measurements)
- With parallel query: =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 39546 seconds
- With parallel query and enable_groupag= g turned off: =C2=A0 1115 seconds

# Query and Data Used (attached to= this email)
- Query: test_query.sql
- Data: =C2=A0create_table.sql
# The steps to run the test are as follows.
For example, on psql:<= br>
1. Create tables:
=C2=A0 =C2=A0 \i create_table.sql

2. Exe= cute a query:
=C2=A0 =C2=A0 \i test_query.sql

3. Execute a query = using the new GUC parameter:
=C2=A0 =C2=A0 set enable_groupagg to off;=C2=A0 =C2=A0 \i test_query.sql

As a benefit to users, while there= has previously been a GUC parameter
to control HashAggregate, there was= no corresponding way to control
GroupAggregate. This patch addresses th= at, giving users more flexibility
in tuning execution plans.

I= 9;ve attached a WIP patch that adds this GUC parameter. I would
apprecia= te any feedback, especially regarding how many test cases I
should crea= te.

I firs= t thought enable_hashagg should be sufficient to choose one strategy over t= he other. But that is not true, enable_hashagg =3D true allows both the str= ategies, enable_hashagg =3D false disables just hash strategy. There's = no way to disable group agg alone. So I think it makes sense to have this G= UC.

I am surprised that we didn't see this bei= ng a problem for so long.

We seem to disable mixed= strategy when enable_hashagg is false. Do we want to do the same when enab= le_groupagg =3D false as well?
=C2=A0

To cr= eate new test cases for enable_groupagg, I looked into existing
test ca= ses that use enable_hashagg and found that it is used in many
places (6= 2 places). Should I add a test case for enable_groupagg in
the same pla= ce as enable_hashagg? I think that adding a new feature
requires a mini= mum number of test cases, so I would appreciate your
advice.
<= /div>

Some of those instances a= re for plan stability, all of which need not be replicated. But some of the= m explicitly test sort based grouping. For rest of them hash based plan see= ms to be the best one, so explicit enable_groupagg =3D false is not needed.= We will need some test to test the switch though.
=C2=A0
=


Additionally, based on the execution plan, I suspect the slowd= own in the
parallel query might be caused by misestimates related to Sor= t or
Gather Merge.
While resolving those misestimates would ideally i= mprove the root issue,
I'd like to keep the focus of this thread on = adding the GUC parameter.
Then, I plan to report or address the estimati= on problem in a separate
thread.


+1.

--
Best Wishes,
Ashutosh Bapat
--000000000000705d610637215ac6--