Missing docs for new enable_group_by_reordering GUC

Started by Bruce Momjianover 1 year ago8 messages
#1Bruce Momjian
bruce@momjian.us

This commit added enable_group_by_reordering:

commit 0452b461bc4
Author: Alexander Korotkov <akorotkov@postgresql.org>
Date: Sun Jan 21 22:21:36 2024 +0200

Explore alternative orderings of group-by pathkeys during optimization.

When evaluating a query with a multi-column GROUP BY clause, we can minimize
sort operations or avoid them if we synchronize the order of GROUP BY clauses
with the ORDER BY sort clause or sort order, which comes from the underlying
query tree. Grouping does not imply any ordering, so we can compare
the keys in arbitrary order, and a Hash Agg leverages this. But for Group Agg,
we simply compared keys in the order specified in the query. This commit
explores alternative ordering of the keys, trying to find a cheaper one.

The ordering of group keys may interact with other parts of the query, some of
which may not be known while planning the grouping. For example, there may be
an explicit ORDER BY clause or some other ordering-dependent operation higher up
in the query, and using the same ordering may allow using either incremental
sort or even eliminating the sort entirely.

The patch always keeps the ordering specified in the query, assuming the user
might have additional insights.

This introduces a new GUC enable_group_by_reordering so that the optimization
may be disabled if needed.

Discussion: /messages/by-id/7c79e6a5-8597-74e8-0671-1c39d124c9d6@sigaev.ru
Author: Andrei Lepikhov, Teodor Sigaev
Reviewed-by: Tomas Vondra, Claudio Freire, Gavin Flower, Dmitry Dolgov
Reviewed-by: Robert Haas, Pavel Borisov, David Rowley, Zhihong Yu
Reviewed-by: Tom Lane, Alexander Korotkov, Richard Guo, Alena Rybakina

It mentions it was added as a GUC to postgresql.conf, but I see no SGML
docs for this new GUC value. Would someone please add docs for this?
Thanks.

--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com

Only you can decide what is important to you.

#2Andrei Lepikhov
lepihov@gmail.com
In reply to: Bruce Momjian (#1)
1 attachment(s)
Re: Missing docs for new enable_group_by_reordering GUC

On 6/18/24 09:32, Bruce Momjian wrote:

This commit added enable_group_by_reordering:

commit 0452b461bc4
Author: Alexander Korotkov <akorotkov@postgresql.org>
Date: Sun Jan 21 22:21:36 2024 +0200
It mentions it was added as a GUC to postgresql.conf, but I see no SGML
docs for this new GUC value. Would someone please add docs for this?
Thanks.

It is my mistake, sorry for that. See the patch in attachment.

--
regards, Andrei Lepikhov

Attachments:

0001-Add-doc-entry-for-the-new-paramenter-enable_group_by.patchtext/x-patch; charset=UTF-8; name=0001-Add-doc-entry-for-the-new-paramenter-enable_group_by.patchDownload
From b2d5aca0547bf912032ad60a30a55ef86a28708e Mon Sep 17 00:00:00 2001
From: "Andrei V. Lepikhov" <lepihov@gmail.com>
Date: Tue, 18 Jun 2024 13:11:52 +0700
Subject: [PATCH] Add doc entry for the new paramenter
 enable_group_by_reordering

---
 doc/src/sgml/config.sgml | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 698169afdb..61093a033d 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -5327,6 +5327,20 @@ ANY <replaceable class="parameter">num_sync</replaceable> ( <replaceable class="
       </listitem>
      </varlistentry>
 
+     <varlistentry id="guc-enable-groupby-reordering" xreflabel="enable_group_by_reordering">
+      <term><varname>enable_group_by_reordering</varname> (<type>boolean</type>)
+      <indexterm>
+       <primary><varname>enable_group_by_reordering</varname> configuration parameter</primary>
+      </indexterm>
+      </term>
+      <listitem>
+       <para>
+        Enables or disables reordering of keys in a <literal>GROUP BY</literal>
+        clause. The default is <literal>on</literal>.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry id="guc-enable-gathermerge" xreflabel="enable_gathermerge">
       <term><varname>enable_gathermerge</varname> (<type>boolean</type>)
       <indexterm>
-- 
2.39.2

#3Alexander Korotkov
aekorotkov@gmail.com
In reply to: Andrei Lepikhov (#2)
1 attachment(s)
Re: Missing docs for new enable_group_by_reordering GUC

On Tue, Jun 18, 2024 at 9:14 AM Andrei Lepikhov <lepihov@gmail.com> wrote:

On 6/18/24 09:32, Bruce Momjian wrote:

This commit added enable_group_by_reordering:

commit 0452b461bc4
Author: Alexander Korotkov <akorotkov@postgresql.org>
Date: Sun Jan 21 22:21:36 2024 +0200
It mentions it was added as a GUC to postgresql.conf, but I see no SGML
docs for this new GUC value. Would someone please add docs for this?
Thanks.

It is my mistake, sorry for that. See the patch in attachment.

Bruce, thank for noticing. Andrei, thank you for providing a fix.
Please, check the revised patch.

------
Regards,
Alexander Korotkov
Supabase

Attachments:

v2-0001-Add-doc-entry-for-the-new-GUC-paramenter-enable_g.patchapplication/octet-stream; name=v2-0001-Add-doc-entry-for-the-new-GUC-paramenter-enable_g.patchDownload
From f44436b9ebd3f63cdd4d1d3e1b46a8f660e5edac Mon Sep 17 00:00:00 2001
From: Alexander Korotkov <akorotkov@postgresql.org>
Date: Tue, 18 Jun 2024 15:08:17 +0300
Subject: [PATCH v2] Add doc entry for the new GUC paramenter
 enable_group_by_reordering

0452b461bc4 adds alternative orderings of group-by keys during the query
optimization. This new feature is controlled by the new GUC parameter
enable_group_by_reordering, which accidentally came without the documentation.
This commit adds the missing documentation for that GUC.

Reported-by: Bruce Momjian
Discussion: https://postgr.es/m/ZnDx2FYlba_OafQd%40momjian.us
Author: Andrei Lepikhov
---
 doc/src/sgml/config.sgml | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 698169afdb6..d45cb02ad15 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -5327,6 +5327,24 @@ ANY <replaceable class="parameter">num_sync</replaceable> ( <replaceable class="
       </listitem>
      </varlistentry>
 
+     <varlistentry id="guc-enable-groupby-reordering" xreflabel="enable_group_by_reordering">
+      <term><varname>enable_group_by_reordering</varname> (<type>boolean</type>)
+      <indexterm>
+       <primary><varname>enable_group_by_reordering</varname> configuration parameter</primary>
+      </indexterm>
+      </term>
+      <listitem>
+       <para>
+        Enables or disables the reordering of keys in a
+        <literal>GROUP BY</literal> clause to match the ordering keys of a
+        child node of the plan, such as an index scan. When turned off, keys
+        in a <literal>GROUP BY</literal> clause are only reordered to match
+        the <literal>ORDER BY</literal> clause, if any. The default is
+        <literal>on</literal>.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry id="guc-enable-gathermerge" xreflabel="enable_gathermerge">
       <term><varname>enable_gathermerge</varname> (<type>boolean</type>)
       <indexterm>
-- 
2.39.3 (Apple Git-145)

#4Pavel Borisov
pashkin.elfe@gmail.com
In reply to: Alexander Korotkov (#3)
Re: Missing docs for new enable_group_by_reordering GUC

Hi, Alexander!

On Tue, 18 Jun 2024 at 16:13, Alexander Korotkov <aekorotkov@gmail.com>
wrote:

On Tue, Jun 18, 2024 at 9:14 AM Andrei Lepikhov <lepihov@gmail.com> wrote:

On 6/18/24 09:32, Bruce Momjian wrote:

This commit added enable_group_by_reordering:

commit 0452b461bc4
Author: Alexander Korotkov <akorotkov@postgresql.org>
Date: Sun Jan 21 22:21:36 2024 +0200
It mentions it was added as a GUC to postgresql.conf, but I see no SGML
docs for this new GUC value. Would someone please add docs for this?
Thanks.

It is my mistake, sorry for that. See the patch in attachment.

Bruce, thank for noticing. Andrei, thank you for providing a fix.
Please, check the revised patch.

I briefly looked into this docs patch. Planner gucs are arranged
alphabetically, so enable_group_by_reordering is better to come after
enable-gathermerge not before.

+  Enables or disables the reordering of keys in a
+        <literal>GROUP BY</literal> clause to match the ordering keys of a
+        child node of the plan, such as an index scan. When turned off,
keys
+        in a <literal>GROUP BY</literal> clause are only reordered to match
+        the <literal>ORDER BY</literal> clause, if any. The default is
+        <literal>on</literal>.
I'd also suggest the same style as already exists
for enable_presorted_aggregate guc i.e:

Controls if the query planner will produce a plan which will provide
<literal>GROUP BY</literal> keys presorted in the order of keys of a child
node of the plan, such as an index scan. When disabled, the query planner
will produce a plan with <literal>GROUP BY</literal> keys only reordered to
match
the <literal>ORDER BY</literal> clause, if any. When enabled, the planner
will try to produce a more efficient plan. The default value is on.

Regards,
Pavel Borisov
Supabase

#5Pavel Borisov
pashkin.elfe@gmail.com
In reply to: Pavel Borisov (#4)
Re: Missing docs for new enable_group_by_reordering GUC

Controls if the query planner will produce a plan which will provide
<literal>GROUP BY</literal> keys presorted in the order of keys of a child
node of the plan, such as an index scan. When disabled, the query planner
will produce a plan with <literal>GROUP BY</literal> keys only reordered to
match
the <literal>ORDER BY</literal> clause, if any. When enabled, the planner
will try to produce a more efficient plan. The default value is on.

A correction of myself: presorted -> sorted, reordered ->sorted

Regards,
Pavel

#6Alexander Korotkov
aekorotkov@gmail.com
In reply to: Pavel Borisov (#5)
1 attachment(s)
Re: Missing docs for new enable_group_by_reordering GUC

On Tue, Jun 18, 2024 at 4:14 PM Pavel Borisov <pashkin.elfe@gmail.com> wrote:

Controls if the query planner will produce a plan which will provide <literal>GROUP BY</literal> keys presorted in the order of keys of a child node of the plan, such as an index scan. When disabled, the query planner will produce a plan with <literal>GROUP BY</literal> keys only reordered to match
the <literal>ORDER BY</literal> clause, if any. When enabled, the planner will try to produce a more efficient plan. The default value is on.

A correction of myself: presorted -> sorted, reordered ->sorted

Thank you for your review. I think all of this make sense. Please,
check the revised patch attached.

------
Regards,
Alexander Korotkov
Supabase

Attachments:

v3-0001-Add-doc-entry-for-the-new-GUC-paramenter-enable_g.patchapplication/octet-stream; name=v3-0001-Add-doc-entry-for-the-new-GUC-paramenter-enable_g.patchDownload
From 9a20dfd04a9dcd14beaf13773a6d1b695b67e36f Mon Sep 17 00:00:00 2001
From: Alexander Korotkov <akorotkov@postgresql.org>
Date: Tue, 18 Jun 2024 15:08:17 +0300
Subject: [PATCH v3] Add doc entry for the new GUC paramenter
 enable_group_by_reordering

0452b461bc4 adds alternative orderings of group-by keys during the query
optimization. This new feature is controlled by the new GUC parameter
enable_group_by_reordering, which accidentally came without the documentation.
This commit adds the missing documentation for that GUC.

Reported-by: Bruce Momjian
Discussion: https://postgr.es/m/ZnDx2FYlba_OafQd%40momjian.us
Author: Andrei Lepikhov
---
 doc/src/sgml/config.sgml | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 698169afdb6..0c7a9082c54 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -5341,6 +5341,25 @@ ANY <replaceable class="parameter">num_sync</replaceable> ( <replaceable class="
       </listitem>
      </varlistentry>
 
+     <varlistentry id="guc-enable-groupby-reordering" xreflabel="enable_group_by_reordering">
+      <term><varname>enable_group_by_reordering</varname> (<type>boolean</type>)
+      <indexterm>
+       <primary><varname>enable_group_by_reordering</varname> configuration parameter</primary>
+      </indexterm>
+      </term>
+      <listitem>
+       <para>
+        Controls if the query planner will produce a plan which will provide
+        <literal>GROUP BY</literal> keys sorted in the order of keys of
+        a child node of the plan, such as an index scan.  When disabled, the
+        query planner will produce a plan with <literal>GROUP BY</literal>
+        keys only sorted to match the <literal>ORDER BY</literal> clause,
+        if any. When enabled, the planner will try to produce a more
+        efficient plan. The default value is <literal>on</literal>.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry id="guc-enable-hashagg" xreflabel="enable_hashagg">
       <term><varname>enable_hashagg</varname> (<type>boolean</type>)
       <indexterm>
-- 
2.39.3 (Apple Git-145)

#7Pavel Borisov
pashkin.elfe@gmail.com
In reply to: Alexander Korotkov (#6)
Re: Missing docs for new enable_group_by_reordering GUC

Hi, Alexander!

On Wed, 19 Jun 2024 at 05:27, Alexander Korotkov <aekorotkov@gmail.com>
wrote:

On Tue, Jun 18, 2024 at 4:14 PM Pavel Borisov <pashkin.elfe@gmail.com>
wrote:

Controls if the query planner will produce a plan which will provide

<literal>GROUP BY</literal> keys presorted in the order of keys of a child
node of the plan, such as an index scan. When disabled, the query planner
will produce a plan with <literal>GROUP BY</literal> keys only reordered to
match

the <literal>ORDER BY</literal> clause, if any. When enabled, the

planner will try to produce a more efficient plan. The default value is on.

A correction of myself: presorted -> sorted, reordered ->sorted

Thank you for your review. I think all of this make sense. Please,
check the revised patch attached.

To me patch v3 looks good.

Regards,
Pavel Borisov
Supabase

#8Alexander Korotkov
aekorotkov@gmail.com
In reply to: Pavel Borisov (#7)
Re: Missing docs for new enable_group_by_reordering GUC

On Wed, Jun 19, 2024 at 6:02 PM Pavel Borisov <pashkin.elfe@gmail.com> wrote:

On Wed, 19 Jun 2024 at 05:27, Alexander Korotkov <aekorotkov@gmail.com> wrote:

On Tue, Jun 18, 2024 at 4:14 PM Pavel Borisov <pashkin.elfe@gmail.com> wrote:

Controls if the query planner will produce a plan which will provide <literal>GROUP BY</literal> keys presorted in the order of keys of a child node of the plan, such as an index scan. When disabled, the query planner will produce a plan with <literal>GROUP BY</literal> keys only reordered to match
the <literal>ORDER BY</literal> clause, if any. When enabled, the planner will try to produce a more efficient plan. The default value is on.

A correction of myself: presorted -> sorted, reordered ->sorted

Thank you for your review. I think all of this make sense. Please,
check the revised patch attached.

To me patch v3 looks good.

Ok, thank you. I'm going to push this if no objections.

------
Regards,
Alexander Korotkov
Supabase