enable_presorted_aggregate
Read PG 18 manual ↗Controls if the query planner will produce a plan which will provide rows which are presorted in the order required for the query's ORDER BY / DISTINCT aggregate functions.
- Type
- bool
- Context
- user
- Measured default
- on
- Unit
- —
- Metadata snapshot
- 18
Definition PG 18 manual
Controls if the query planner will produce a plan which will provide rows which are presorted in the order required for the query's ORDER BY / DISTINCT aggregate functions. When disabled, the query planner will produce a plan which will always require the executor to perform a sort before performing aggregation of each aggregate function containing an ORDER BY or DISTINCT clause. When enabled, the planner will try to produce a more efficient plan which provides input to the aggregate functions which is presorted in the order they require for aggregation. The default value is on.
Measured default history
| Version interval | Default |
|---|---|
| 16 – 19 | on |
Authored guidance from the GUC source snapshot; the version-specific manual above is the definition reference. View source ↗
How it works
enable_presorted_aggregate controls whether the planner may choose plans that deliver input already ordered for aggregate ORDER BY or DISTINCT clauses.
It is consulted while a plan is built. A session-level change is useful for comparing EXPLAIN alternatives, but an already cached plan is not retroactively rewritten; invalidation or replanning is required to observe a different choice.
The switch changes which candidate paths the planner may cost; it does not make the chosen method faster by itself. Its user context permits session- or transaction-local changes; newly performed or newly planned work sees the value.
Operational considerations
Treating enable_presorted_aggregate as a query hint even though it affects every plan built in its scope.
Testing an already cached prepared plan and concluding that the setting has no effect.
Masking stale statistics or cardinality errors by disabling a plan method globally.
The switch changes which candidate paths the planner may cost; it does not make the chosen method faster by itself.
Workload guidance
OLAP: Benchmark representative analytical plans with and without enable_presorted_aggregate. Judge planning time, memory, spill, and execution time together; a win for one report is not evidence for a global setting.
OLTP: Keep the upstream default for routine OLTP. Use a transaction- or session-local change to diagnose whether choosing plans that deliver input already ordered for aggregate ORDER BY or DISTINCT clauses explains a regression, then repair statistics, indexes, estimates, or query shape instead of leaving a cluster-wide method ban.
SMALL: Do not use enable_presorted_aggregate as a permanent hint on a small host. Resource pressure may change the best method, so verify with EXPLAIN (ANALYZE, BUFFERS) and keep the change scoped to the affected workload if it is still needed.
Version history 1
- PG 15 → 16added
Related entries
Further reading
Definition snapshot: english-manuals:8d8616fcead4eb36b3690c6c837… · English manual source