Skip to content

Commit 07a0643

Browse files
author
Commitfest Bot
committed
[CF 5814] Add enable_groupagg GUC parameter to control GroupAggregate usage
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5814 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CAOKkKFvgjAwtUFKh3baZ7BcQ5u+wS_DO+2n7dh0un+19v_VOzQ@mail.gmail.com Author(s): Tatsuro Yamada
2 parents 0810fbb + 4414588 commit 07a0643

File tree

8 files changed

+232
-1
lines changed

8 files changed

+232
-1
lines changed

doc/src/sgml/config.sgml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5519,6 +5519,20 @@ ANY <replaceable class="parameter">num_sync</replaceable> ( <replaceable class="
55195519
</listitem>
55205520
</varlistentry>
55215521

5522+
<varlistentry id="guc-enable-groupagg" xreflabel="enable_groupagg">
5523+
<term><varname>enable_groupagg</varname> (<type>boolean</type>)
5524+
<indexterm>
5525+
<primary><varname>enable_groupagg</varname> configuration parameter</primary>
5526+
</indexterm>
5527+
</term>
5528+
<listitem>
5529+
<para>
5530+
Enables or disables the query planner's use of grouped
5531+
aggregation plan types. The default is <literal>on</literal>.
5532+
</para>
5533+
</listitem>
5534+
</varlistentry>
5535+
55225536
<varlistentry id="guc-enable-hashjoin" xreflabel="enable_hashjoin">
55235537
<term><varname>enable_hashjoin</varname> (<type>boolean</type>)
55245538
<indexterm>

src/backend/optimizer/path/costsize.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ bool enable_tidscan = true;
150150
bool enable_sort = true;
151151
bool enable_incremental_sort = true;
152152
bool enable_hashagg = true;
153+
bool enable_groupagg = true;
153154
bool enable_nestloop = true;
154155
bool enable_material = true;
155156
bool enable_memoize = true;
@@ -2763,6 +2764,8 @@ cost_agg(Path *path, PlannerInfo *root,
27632764
/* Here we are able to deliver output on-the-fly */
27642765
startup_cost = input_startup_cost;
27652766
total_cost = input_total_cost;
2767+
if (aggstrategy == AGG_SORTED && !enable_groupagg && enable_hashagg)
2768+
++disabled_nodes;
27662769
if (aggstrategy == AGG_MIXED && !enable_hashagg)
27672770
++disabled_nodes;
27682771
/* calcs phrased this way to match HASHED case, see note above */

src/backend/utils/misc/guc_tables.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,16 @@ struct config_bool ConfigureNamesBool[] =
869869
true,
870870
NULL, NULL, NULL
871871
},
872+
{
873+
{"enable_groupagg", PGC_USERSET, QUERY_TUNING_METHOD,
874+
gettext_noop("Enables the planner's use of grouped aggregation plans."),
875+
NULL,
876+
GUC_EXPLAIN
877+
},
878+
&enable_groupagg,
879+
true,
880+
NULL, NULL, NULL
881+
},
872882
{
873883
{"enable_material", PGC_USERSET, QUERY_TUNING_METHOD,
874884
gettext_noop("Enables the planner's use of materialization."),

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@
408408
#enable_bitmapscan = on
409409
#enable_gathermerge = on
410410
#enable_hashagg = on
411+
#enable_groupagg = on
411412
#enable_hashjoin = on
412413
#enable_incremental_sort = on
413414
#enable_indexscan = on

src/include/optimizer/cost.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ extern PGDLLIMPORT bool enable_tidscan;
5757
extern PGDLLIMPORT bool enable_sort;
5858
extern PGDLLIMPORT bool enable_incremental_sort;
5959
extern PGDLLIMPORT bool enable_hashagg;
60+
extern PGDLLIMPORT bool enable_groupagg;
6061
extern PGDLLIMPORT bool enable_nestloop;
6162
extern PGDLLIMPORT bool enable_material;
6263
extern PGDLLIMPORT bool enable_memoize;

src/test/regress/expected/aggregates.out

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3581,3 +3581,123 @@ drop table agg_hash_1;
35813581
drop table agg_hash_2;
35823582
drop table agg_hash_3;
35833583
drop table agg_hash_4;
3584+
-- create table to check enable_groupagg
3585+
CREATE TABLE test_groupagg(
3586+
id serial primary key,
3587+
c1 text,
3588+
c2 text,
3589+
c3 numeric);
3590+
INSERT INTO test_groupagg (c1, c2, c3) VALUES
3591+
('a', 'GGG', 100),
3592+
('a', 'GGG', 150),
3593+
('a', 'rrr', 200),
3594+
('b', 'ooo', 300),
3595+
('b', 'ooo', 250),
3596+
('b', 'uuu', 100),
3597+
('c', 'ppp', 500),
3598+
('c', 'ppp', 600),
3599+
('c', 'aaa', 550);
3600+
ANALYZE;
3601+
-- default: GroupAgg and HashAgg are mixed and selected
3602+
SET max_parallel_workers to 0;
3603+
SET max_parallel_workers_per_gather to 0;
3604+
SET enable_hashagg to default;
3605+
SET enable_groupagg to default;
3606+
EXPLAIN(costs off, settings)
3607+
SELECT c1, AVG(total)
3608+
FROM (
3609+
SELECT c1, c2, SUM(c3) AS total
3610+
FROM test_groupagg
3611+
GROUP BY c1, c2
3612+
) AS sub
3613+
GROUP BY c1
3614+
ORDER BY c1;
3615+
QUERY PLAN
3616+
-----------------------------------------------------------------------------
3617+
GroupAggregate
3618+
Group Key: sub.c1
3619+
-> Sort
3620+
Sort Key: sub.c1
3621+
-> Subquery Scan on sub
3622+
-> HashAggregate
3623+
Group Key: test_groupagg.c1, test_groupagg.c2
3624+
-> Seq Scan on test_groupagg
3625+
Settings: max_parallel_workers = '0', max_parallel_workers_per_gather = '0'
3626+
(9 rows)
3627+
3628+
-- Only GroupAgg is selected
3629+
set enable_hashagg to off;
3630+
EXPLAIN(costs off, settings)
3631+
SELECT c1, AVG(total)
3632+
FROM (
3633+
SELECT c1, c2, SUM(c3) AS total
3634+
FROM test_groupagg
3635+
GROUP BY c1, c2
3636+
) AS sub
3637+
GROUP BY c1
3638+
ORDER BY c1;
3639+
QUERY PLAN
3640+
-----------------------------------------------------------------------------------------------------
3641+
GroupAggregate
3642+
Group Key: test_groupagg.c1
3643+
-> GroupAggregate
3644+
Group Key: test_groupagg.c1, test_groupagg.c2
3645+
-> Sort
3646+
Sort Key: test_groupagg.c1, test_groupagg.c2
3647+
-> Seq Scan on test_groupagg
3648+
Settings: max_parallel_workers = '0', max_parallel_workers_per_gather = '0', enable_hashagg = 'off'
3649+
(8 rows)
3650+
3651+
-- Only HashAgg is selected
3652+
SET enable_hashagg to on;
3653+
SET enable_groupagg to off;
3654+
EXPLAIN(costs off, settings)
3655+
SELECT c1, AVG(total)
3656+
FROM (
3657+
SELECT c1, c2, SUM(c3) AS total
3658+
FROM test_groupagg
3659+
GROUP BY c1, c2
3660+
) AS sub
3661+
GROUP BY c1
3662+
ORDER BY c1;
3663+
QUERY PLAN
3664+
------------------------------------------------------------------------------------------------------
3665+
Sort
3666+
Sort Key: test_groupagg.c1
3667+
-> HashAggregate
3668+
Group Key: test_groupagg.c1
3669+
-> HashAggregate
3670+
Group Key: test_groupagg.c1, test_groupagg.c2
3671+
-> Seq Scan on test_groupagg
3672+
Settings: max_parallel_workers = '0', max_parallel_workers_per_gather = '0', enable_groupagg = 'off'
3673+
(8 rows)
3674+
3675+
-- Only GroupAgg is selected as a fallback
3676+
SET enable_hashagg to off;
3677+
SET enable_groupagg to off;
3678+
EXPLAIN(costs off, settings)
3679+
SELECT c1, AVG(total)
3680+
FROM (
3681+
SELECT c1, c2, SUM(c3) AS total
3682+
FROM test_groupagg
3683+
GROUP BY c1, c2
3684+
) AS sub
3685+
GROUP BY c1
3686+
ORDER BY c1;
3687+
QUERY PLAN
3688+
------------------------------------------------------------------------------------------------------------------------------
3689+
GroupAggregate
3690+
Group Key: test_groupagg.c1
3691+
-> GroupAggregate
3692+
Group Key: test_groupagg.c1, test_groupagg.c2
3693+
-> Sort
3694+
Sort Key: test_groupagg.c1, test_groupagg.c2
3695+
-> Seq Scan on test_groupagg
3696+
Settings: max_parallel_workers = '0', max_parallel_workers_per_gather = '0', enable_hashagg = 'off', enable_groupagg = 'off'
3697+
(8 rows)
3698+
3699+
RESET enable_hashagg;
3700+
RESET enable_groupagg;
3701+
RESET max_parallel_workers;
3702+
RESET max_parallel_workers_per_gather;
3703+
DROP TABLE test_groupagg;

src/test/regress/expected/sysviews.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ select name, setting from pg_settings where name like 'enable%';
153153
enable_distinct_reordering | on
154154
enable_gathermerge | on
155155
enable_group_by_reordering | on
156+
enable_groupagg | on
156157
enable_hashagg | on
157158
enable_hashjoin | on
158159
enable_incremental_sort | on
@@ -172,7 +173,7 @@ select name, setting from pg_settings where name like 'enable%';
172173
enable_seqscan | on
173174
enable_sort | on
174175
enable_tidscan | on
175-
(24 rows)
176+
(25 rows)
176177

177178
-- There are always wait event descriptions for various types. InjectionPoint
178179
-- may be present or absent, depending on history since last postmaster start.

src/test/regress/sql/aggregates.sql

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,3 +1644,84 @@ drop table agg_hash_1;
16441644
drop table agg_hash_2;
16451645
drop table agg_hash_3;
16461646
drop table agg_hash_4;
1647+
1648+
-- create table to check enable_groupagg
1649+
CREATE TABLE test_groupagg(
1650+
id serial primary key,
1651+
c1 text,
1652+
c2 text,
1653+
c3 numeric);
1654+
INSERT INTO test_groupagg (c1, c2, c3) VALUES
1655+
('a', 'GGG', 100),
1656+
('a', 'GGG', 150),
1657+
('a', 'rrr', 200),
1658+
('b', 'ooo', 300),
1659+
('b', 'ooo', 250),
1660+
('b', 'uuu', 100),
1661+
('c', 'ppp', 500),
1662+
('c', 'ppp', 600),
1663+
('c', 'aaa', 550);
1664+
ANALYZE;
1665+
1666+
-- default: GroupAgg and HashAgg are mixed and selected
1667+
SET max_parallel_workers to 0;
1668+
SET max_parallel_workers_per_gather to 0;
1669+
SET enable_hashagg to default;
1670+
SET enable_groupagg to default;
1671+
1672+
EXPLAIN(costs off, settings)
1673+
SELECT c1, AVG(total)
1674+
FROM (
1675+
SELECT c1, c2, SUM(c3) AS total
1676+
FROM test_groupagg
1677+
GROUP BY c1, c2
1678+
) AS sub
1679+
GROUP BY c1
1680+
ORDER BY c1;
1681+
1682+
-- Only GroupAgg is selected
1683+
set enable_hashagg to off;
1684+
1685+
EXPLAIN(costs off, settings)
1686+
SELECT c1, AVG(total)
1687+
FROM (
1688+
SELECT c1, c2, SUM(c3) AS total
1689+
FROM test_groupagg
1690+
GROUP BY c1, c2
1691+
) AS sub
1692+
GROUP BY c1
1693+
ORDER BY c1;
1694+
1695+
-- Only HashAgg is selected
1696+
SET enable_hashagg to on;
1697+
SET enable_groupagg to off;
1698+
1699+
EXPLAIN(costs off, settings)
1700+
SELECT c1, AVG(total)
1701+
FROM (
1702+
SELECT c1, c2, SUM(c3) AS total
1703+
FROM test_groupagg
1704+
GROUP BY c1, c2
1705+
) AS sub
1706+
GROUP BY c1
1707+
ORDER BY c1;
1708+
1709+
-- Only GroupAgg is selected as a fallback
1710+
SET enable_hashagg to off;
1711+
SET enable_groupagg to off;
1712+
1713+
EXPLAIN(costs off, settings)
1714+
SELECT c1, AVG(total)
1715+
FROM (
1716+
SELECT c1, c2, SUM(c3) AS total
1717+
FROM test_groupagg
1718+
GROUP BY c1, c2
1719+
) AS sub
1720+
GROUP BY c1
1721+
ORDER BY c1;
1722+
1723+
RESET enable_hashagg;
1724+
RESET enable_groupagg;
1725+
RESET max_parallel_workers;
1726+
RESET max_parallel_workers_per_gather;
1727+
DROP TABLE test_groupagg;

0 commit comments

Comments
 (0)