Skip a redundant singleton GROUP BY node
Thread notes
1 note#performance#moderate#enhancement
hackorum-bot#performance#moderate#enhancement
@global #performance #moderate #enhancement
Hi all,
This two-patch series avoids a plain GROUP BY step when the planner can
prove that every input row already forms its own group.
The motivation is not to encourage redundant SQL. ORMs, report builders,
and generated query templates often retain GROUP BY keys that are useful
to the query contract even when a primary key already makes each input row
a singleton. For such queries the grouping node only repeats a partitioning
that is already known to produce one row per group.
The proof is deliberately narrow. The input must be a single ordinary base
relation or partitioned table. For a partitioned table, the unique index
must include the partition key, so covering it in the grouping key proves
cross-partition uniqueness as well. The query must be a plain GROUP BY with
no aggregates, HAVING, window functions, set operations, DISTINCT, SRFs, or
row locking. There must be an immediate, non-partial, non-expression unique
index whose key columns are covered by simple grouping Vars. A NULLS
DISTINCT unique index also requires each key column to be NOT NULL; NULLS
NOT DISTINCT removes that requirement. The index and grouping key must
agree on equality semantics, including opfamily and collation. Additional
grouping expressions are safe because they can only subdivide the singleton
groups.
When the proof holds, the planner adds projection paths to the grouping
upper relation instead of building ordinary aggregate paths. It does not
rewrite the parse tree. GetForeignUpperPaths and create_upper_paths_hook
are still called, and set_cheapest is performed after those hooks, so
extensions retain the same upper-relation entry point.
Patch 1 extracts the existing unique-index/GROUP BY matching logic in
remove_useless_groupby_columns() into a helper. It is behavior-preserving
and lets the new proof share the NOT NULL, NULLS NOT DISTINCT, opfamily,
and collation checks rather than maintaining a second implementation.
Patch 2 adds the planner optimization, documentation, and regression
coverage. New tests cover opfamily and collation mismatches, prepared-plan
invalidation after DROP INDEX and DROP NOT NULL, and implicit and degenerate
grouping cases. make check passes all core regression tests.
Benchmarks use paired release builds (--disable-debug --disable-cassert,
-O2), each on its own disposable cluster. On synthetic unlogged workloads,
GROUP BY on a 300k-row primary-key table drops from 83.0 ms to 14.1 ms
median. Unique-key plus expression cases improve 2.5x-3.5x, and a forced
hash-aggregate spill case improves from 272.2 ms to 40.5 ms. Non-target
aggregate/join cases retain identical plan shapes; sampled execution medians
change by +0.2%-+3.0%, and 51-run planning medians are not higher than
baseline in this run. I am not claiming zero runtime change for non-target
workloads.
Open questions I would especially appreciate review on:
* Is the zero-method GroupPathExtraData contract (i.e. no aggregate
methods present) sufficient for FDWs and upper-path hooks that see this
optimized grouped relation?
* Should the helper live in indxpath.c, or is there a better home now
that both initsplan.c and the path-level proof use it?
Expression indexes, partial unique predicates, join-output uniqueness, and
LIMIT-driven opportunities are intentionally outside this series.
Thanks,
Xiangxin Zeng
Attachments:
t254116_10001-Refactor-unique-index-GROUP-BY-key-matching.patchapplication/octet-stream; charset=utf-8; name=0001-Refactor-unique-index-GROUP-BY-key-matching.patchDownload+85-78
0002-Skip-grouping-when-every-input-row-is-its-own-group.patchapplication/octet-stream; charset=utf-8; name=0002-Skip-grouping-when-every-input-row-is-its-own-group.patchDownload+1230-51
On Fri, 9 Oct 2026 at 13:23, zengxx <xiangxin_zeng@qq.com> wrote:
This two-patch series avoids a plain GROUP BY step when the planner can
prove that every input row already forms its own group.
It seems you're opening a new thread about this every week. Please do
not do this. It will fragment any discussion and make things harder to
follow. It also causes unnecessary traffic on the mailing lists.
3 threads:
18th Sept: /messages/by-id/tencent_4DDFEE26EA4A2E1BA967729AF20822D61207@qq.com
24th Sept: /messages/by-id/tencent_1D9637E157D2D9043FE7D28C443CB036FB0A@qq.com
today: /messages/by-id/tencent_F52C0D4C4415EB90AF70934BE597CDCDB306@qq.com
The correct procedure for ensuring patches do not get forgotten is to
add them in the commitfest app [0]https://commitfest.postgresql.org/.
For this one, I wouldn't bother as the idea gets proposed regularly on
this mailing list. Please research the previous proposals. For example
[1]: /messages/by-id/CAKU4AWpxABop1ydF-478NXjObG8r-g2yy0=E1i2bf88HvjS04g@mail.gmail.com
The proof is deliberately narrow. The input must be a single ordinary base
relation or partitioned table. For a partitioned table, the unique index
must include the partition key, so covering it in the grouping key proves
cross-partition uniqueness as well.
See discussions in [2]/messages/by-id/CAApHDvq7i0=O97r4Y1pv68+tprVczKsXRsV28rM9H-rVPOfeNQ@mail.gmail.com. There also seems to already be a patch in the
commitfest for this; see [3]https://commitfest.postgresql.org/patch/7045/
David
[0]: https://commitfest.postgresql.org/
[1]: /messages/by-id/CAKU4AWpxABop1ydF-478NXjObG8r-g2yy0=E1i2bf88HvjS04g@mail.gmail.com
[2]: /messages/by-id/CAApHDvq7i0=O97r4Y1pv68+tprVczKsXRsV28rM9H-rVPOfeNQ@mail.gmail.com
[3]: https://commitfest.postgresql.org/patch/7045/
Hi David,
Thank you for pointing this out. I realize now that I accidentally
started a new thread when I was only trying to fix a formatting error
in my previous email. I did not realize my email client would send it
as a new message.
I apologize for the confusion and the unnecessary mailing list traffic.
I will make sure this does not happen again. All future updates regarding
this patch will be posted as replies within this same thread. If I need
to update the patch, I will generate a new version with git format-patch
and reply to the original email.
I will also review the previous proposals and the existing CommitFest
patch you linked.
Thanks for your patience.
Regards,
Xiangxin