Parameter · Query Tuning / Planner Method Configuration
enable_incremental_sort
Enables the planner's use of incremental sort steps.
- Default:
on - Type: bool
- Applies: Session (any user)
In plain words
If rows already arrive sorted by a and you ask for ORDER BY a, b, PostgreSQL only needs to sort each small group of equal a values by b. That is an incremental sort. It can return the first rows almost immediately, which is great with LIMIT.
For DBAs and operators
Incremental Sort (PostgreSQL 13+) shows Sort Key: a, b and Presorted Key: a. It sorts batches of rows that share the presorted prefix, so it needs far less memory than a full sort and can stream: the first group is output before the rest is even read.
On the test table, SELECT * FROM t ORDER BY a, b LIMIT 10 planned Incremental Sort over an index scan on a: total cost 18. With enable_incremental_sort = off: a full parallel Sort under Gather Merge, cost 6,993, because every row has to be read before the first one comes out.
Risk: the cost model assumes the presorted groups are of similar size. If one value of the leading key holds most of the table, an incremental sort can end up sorting almost everything in one batch, slower than a plain sort. EXPLAIN ANALYZE shows Full-sort Groups and Pre-sorted Groups with memory per group; that is where to look. Incremental sort is also used under aggregates (see enable_presorted_aggregate).
How to change it
Any user can change it for their session with SET.
See it in context
Related parameters
enable_sort enable_presorted_aggregate work_mem enable_group_by_reordering