| From: | David Rowley <drowley(at)postgresql(dot)org> |
|---|---|
| To: | pgsql-committers(at)lists(dot)postgresql(dot)org |
| Subject: | pgsql: Have the planner replace COUNT(ANY) with COUNT(*), when possible |
| Date: | 2025-11-26 21:44:05 |
| Message-ID: | E1vONIy-001Ye2-34@gemulon.postgresql.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-committers |
Have the planner replace COUNT(ANY) with COUNT(*), when possible
This adds SupportRequestSimplifyAggref to allow pg_proc.prosupport
functions to receive an Aggref and allow them to determine if there is a
way that the Aggref call can be optimized.
Also added is a support function to allow transformation of COUNT(ANY)
into COUNT(*). This is possible to do when the given "ANY" cannot be
NULL and also that there are no ORDER BY / DISTINCT clauses within the
Aggref. This is a useful transformation to do as it is common that
people write COUNT(1), which until now has added unneeded overhead.
When counting a NOT NULL column. The overheads can be worse as that
might mean deforming more of the tuple, which for large fact tables may
be many columns in.
It may be possible to add prosupport functions for other aggregates. We
could consider if ORDER BY could be dropped for some calls, e.g. the
ORDER BY is quite useless in MAX(c ORDER BY c).
There is a little bit of passing fallout from adjusting
expr_is_nonnullable() to handle Const which results in a plan change in
the aggregates.out regression test. Previously, nothing was able to
determine that "One-Time Filter: (100 IS NOT NULL)" was always true,
therefore useless to include in the plan.
Author: David Rowley <dgrowleyml(at)gmail(dot)com>
Reviewed-by: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>
Reviewed-by: Matheus Alcantara <matheusssilv97(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAApHDvqGcPTagXpKfH=CrmHBqALpziThJEDs_MrPqjKVeDF9wA@mail.gmail.com
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/42473b3b31238b15cc3c030b4416b2ee79508d8c
Modified Files
--------------
contrib/postgres_fdw/expected/postgres_fdw.out | 20 ++--
src/backend/optimizer/plan/initsplan.c | 20 +---
src/backend/optimizer/util/clauses.c | 73 +++++++++++++++
src/backend/utils/adt/int8.c | 49 +++++++++-
src/include/nodes/supportnodes.h | 25 +++++
src/include/optimizer/optimizer.h | 3 +
src/test/regress/expected/aggregates.out | 125 +++++++++++++++++++++++--
src/test/regress/sql/aggregates.sql | 44 ++++++++-
src/tools/pgindent/typedefs.list | 1 +
9 files changed, 324 insertions(+), 36 deletions(-)
| From | Date | Subject | |
|---|---|---|---|
| Next Message | David Rowley | 2025-11-27 01:05:25 | pgsql: Add parallelism support for TID Range Scans |
| Previous Message | Nathan Bossart | 2025-11-26 21:21:13 | Re: pgsql: Teach DSM registry to ERROR if attaching to an uninitialized ent |