PostgreSQL 17 and 18 introduced two planner GUCs — enable_group_by_reordering and enable_distinct_reordering — that allow the query planner to permute the keys of GROUP BY and SELECT DISTINCT operations to minimize comparison costs and avoid redundant sorts. Since column order in these clauses carries no semantic meaning, the planner can reorder keys based on cardinality and comparison cost statistics, or align them with existing sort orders to skip explicit sorts. Both default to on and are micro-optimizations that matter mainly for large multi-key operations. The primary diagnostic use case is a query that regressed after upgrading to PostgreSQL 17 or 18, where the plan shows a key order different from what was written. The recommended fix is usually improving statistics via ANALYZE rather than disabling the GUC permanently.