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.
At a glance
| Property | Value |
|---|---|
| Parameter | enable_presorted_aggregate |
| Category | Query Planning |
| Default | on |
| Value type | boolean (on/off) |
| Change scope | Per-session (SET) |
| Available in | PostgreSQL 16, 17, 18, 19 (added in 16) |
What it does
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.
(Description quoted from the official PostgreSQL documentation.)
How to apply a change
Can be set per session with SET, per role/database with ALTER ROLE/DATABASE ... SET, or globally in postgresql.conf.
Inspect the current value and source with SHOW enable_presorted_aggregate; or SELECT name, setting, unit, context, source FROM pg_settings WHERE name = 'enable_presorted_aggregate';.
Tuning guidance
This is a diagnostic switch, not a production tuning knob. Turn it off briefly (per session) to confirm why the planner avoids or prefers a plan, then leave it on. Forcing it off in production masks bad estimates instead of fixing them — fix statistics, costs or indexes instead.