postgresql/src/include/optimizer
David Rowley 3226f47282 Add enable_presorted_aggregate GUC
1349d279 added query planner support to allow more efficient execution of
aggregate functions which have an ORDER BY or a DISTINCT clause.  Prior to
that commit, the planner would only request that the lower planner produce
a plan with the order required for the GROUP BY clause and it would be
left up to nodeAgg.c to perform the final sort of records within each
group so that the aggregate transition functions were called in the
correct order.  Now that the planner requests the lower planner produce a
plan with the GROUP BY and the ORDER BY / DISTINCT aggregates in mind,
there is the possibility that the planner chooses a plan which could be
less efficient than what would have been produced before 1349d279.

While developing 1349d279, I had in mind that Incremental Sort would help
us in cases where an index exists only on the GROUP BY column(s).
Incremental Sort would just replace the implicit tuplesorts which are
being performed in nodeAgg.c.  However, because the planner has the
flexibility to instead choose a plan which just performs a full sort on
both the GROUP BY and ORDER BY / DISTINCT aggregate columns, there is
potential for the planner to make a bad choice.  The costing for
Incremental Sort is not perfect as it assumes an even distribution of rows
to sort within each sort group.

Here we add an escape hatch in the form of the enable_presorted_aggregate
GUC.  This will allow users to get the pre-PG16 behavior in cases where
they have no other means to convince the query planner to produce a plan
which only sorts on the GROUP BY column(s).

Discussion: https://postgr.es/m/CAApHDvr1Sm+g9hbv4REOVuvQKeDWXcKUAhmbK5K+dfun0s9CvA@mail.gmail.com
2022-12-20 22:28:58 +13:00
..
appendinfo.h Harmonize more parameter names in bulk. 2022-09-20 13:09:30 -07:00
clauses.h Harmonize more parameter names in bulk. 2022-09-20 13:09:30 -07:00
cost.h Add enable_presorted_aggregate GUC 2022-12-20 22:28:58 +13:00
geqo.h Apply PGDLLIMPORT markings broadly. 2022-04-08 08:16:38 -04:00
geqo_copy.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
geqo_gene.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
geqo_misc.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
geqo_mutation.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
geqo_pool.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
geqo_random.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
geqo_recombination.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
geqo_selection.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
inherit.h Rework query relation permission checking 2022-12-06 16:09:24 +01:00
joininfo.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
optimizer.h Fix incorrect tests for SRFs in relation_can_be_sorted_early(). 2022-08-03 17:33:42 -04:00
orclauses.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
paramassign.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
pathnode.h Estimate cost of elided SubqueryScan, Append, MergeAppend nodes better. 2022-07-19 11:18:19 -04:00
paths.h Revert "Optimize order of GROUP BY keys". 2022-10-03 10:56:16 -04:00
placeholder.h Use an explicit state flag to control PlaceHolderInfo creation. 2022-08-17 15:52:53 -04:00
plancat.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
planmain.h Harmonize more parameter names in bulk. 2022-09-20 13:09:30 -07:00
planner.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
prep.h Harmonize more parameter names in bulk. 2022-09-20 13:09:30 -07:00
restrictinfo.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
subselect.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
tlist.h Update copyright for 2022 2022-01-07 19:04:57 -05:00