{"Entry":{"collection":"guc","key":"enable_presorted_aggregate","name":"enable_presorted_aggregate","aliases":[],"metadata":{"baseline":false,"boot_human":"Not specified","boot_val":null,"category":"Query Tuning / Planner Method Configuration","category_zh":"","changed_in":[],"changes":[{"documentation_changed":false,"fields":{},"from":"15","status":"added","to":"16"}],"content_hash":"44f677ae864b9bf47ce0cae89295f27a75e59a16d4fdd111df66b3ffbf2ec91f","context":"","default_changed_in":[],"default_history":[{"from":"16","to":"19","value":"on"}],"editorial":{"advice":{"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."},"mechanism":["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."],"pitfalls":["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."],"references":[{"title":"PostgreSQL 19 Beta 4: enable_presorted_aggregate","url":"https://www.postgresql.org/docs/19/runtime-config-query.html#GUC-ENABLE-PRESORTED-AGGREGATE"},{"title":"PostgreSQL 19 release notes","url":"https://www.postgresql.org/docs/19/release-19.html"}],"related":["enable_sort","enable_incremental_sort","enable_group_by_reordering","work_mem"],"summary":"enable_presorted_aggregate — Enables the planner's ability to produce plans that provide presorted input for ORDER BY / DISTINCT aggregate functions. Observed in PG16–19 Beta 4; its last measured boot default is on in PG19 Beta 4, with user context. This is a beta-snapshot fact and can change before PostgreSQL 19 GA."},"enumvals":[],"first_version":"16","group":"Query Tuning","group_slug":"query","imported_at":"2026-09-27T17:57:31.234196+08:00","intro_commit":{"authored_at":"2022-12-20T22:28:58+13:00","discussion":["https://postgr.es/m/CAApHDvr1Sm+g9hbv4REOVuvQKeDWXcKUAhmbK5K+dfun0s9CvA@mail.gmail.com"],"hash":"3226f47282a05979483475d1e4a11aab8c1bfc39","subject":"Add enable_presorted_aggregate GUC","url":"https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=3226f47282a05979483475d1e4a11aab8c1bfc39"},"key":"enable_presorted_aggregate","last_version":"20","max_val":"","min_val":"","name":"enable_presorted_aggregate","position":133,"present_in":["16","17","18","19","20"],"short_desc":"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.","short_desc_zh":"","source_rev":"english-manuals:335e4d5b1a0c584b79ff76e90d248a92823c768044dd69613f43cc29a6785dd0","unit":"","vartype":"bool"}},"Definition":{"Collection":"guc","Key":"enable_presorted_aggregate","SourceDatabase":"center","Version":"18","SourceTable":"guc","SourceKey":"enable_presorted_aggregate","SourceRevision":"english-manuals:335e4d5b1a0c584b79ff76e90d248a92823c768044dd69613f43cc29a6785dd0","Facts":{"boot_val":"on","category":"Query Tuning / Planner Method Configuration","context":"user","description":"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.","doc":{"anchor":"GUC-ENABLE-PRESORTED-AGGREGATE","file":"runtime-config-query.html","lang":"en","sha256":"33456ca7ef52bf28e93e34b932a411db7a1b95800179add2cf867dc6ec9144bc","slug":"18"},"documented":true,"enumvals":null,"extra_desc":"Allows the query planner to build plans that provide presorted input for aggregate functions with an ORDER BY / DISTINCT clause.  When disabled, implicit sorts are always performed during execution.","lang":"en","max_val":null,"metadata_version":"18","min_val":null,"name":"enable_presorted_aggregate","short_desc":"Enables the planner's ability to produce plans that provide presorted input for ORDER BY / DISTINCT aggregate functions.","source":"pg-settings-source-snapshot","unit":null,"vartype":"bool"},"ManualEvidence":{"doc":{"anchor":"GUC-ENABLE-PRESORTED-AGGREGATE","file":"runtime-config-query.html","lang":"en","sha256":"33456ca7ef52bf28e93e34b932a411db7a1b95800179add2cf867dc6ec9144bc","slug":"18"}},"MeasuredEvidence":{"metadata_version":"18"}},"Text":{"Collection":"guc","Key":"enable_presorted_aggregate","SourceDatabase":"center","Version":"18","Locale":"en","Title":"enable_presorted_aggregate","Summary":"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.","BodyHTML":"\u003cp\u003eControls if the query planner will produce a plan which will provide rows which are presorted in the order required for the query\u0026#39;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.\u003c/p\u003e","SourceRevision":"english-manuals:335e4d5b1a0c584b79ff76e90d248a92823c768044dd69613f43cc29a6785dd0","ContentHash":"37733c1bce6e4399333d0df295f541267f839697ac7bb411e9d82845f35716eb","Payload":{"description":"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."}},"RequestedLocale":"zh-Hans","Fallback":true,"Versions":["16","17","18","19","20"],"Locales":["en"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
