model-writtenrun 9354 - may be wrong, links go to the source messages. Commit-by-commit LLM analysis is in the Commits dialog.
- Richard Guo reported that grouping-set reordering could discard a usable ORDER BY ordering and introduce an unnecessary sort #1.
- Richard Guo supplied a patch that stops processing the current grouping set once its remaining elements are exhausted, with regression checks demonstrating the improved plan #1.
- Andres Freund asked Andrew Gierth to investigate, and Andrew agreed #2 #3.
- Andrew Gierth committed a rewritten version of the fix from #1, simplifying the loop and list handling and retaining a smaller plan test; the change was backpatched through PostgreSQL 9.5 da53be23d1.
- Andrew Gierth closed the thread by confirming that Richard Guo's fix worked and explaining that he had committed an alternative refactoring of the conditional logic #4.