model-writtenrun 8211 - may be wrong, links go to the source messages. Commit-by-commit LLM analysis is in the Commits dialog.
- Paul Sivash reported that filtering a COALESCE expression returned incorrect rows because GROUPING SETS could turn an apparently constant input into NULL #1.
- David Rowley initially suspected CTE inlining after finding that materialization avoided the failure, but Andrew Gierth reproduced it without CTEs back to PostgreSQL 9.5 and identified the mistaken assumption that the grouping column remained constant #2 #3.
- Tom Lane traced the failure to pushing an outer WHERE condition into HAVING, where constant folding incorrectly removed it, and proposed rejecting references to unsafe grouping outputs; Andrew Gierth clarified that columns appearing in some but not all grouping sets were unsafe #4 #5.
- Andy Fan proposed tracking attribute nullability and questioned the benefit of pushing aggregate restrictions into HAVING; Tom Lane demonstrated the potential benefit for expressions of grouping columns while questioning why grouping sets prevented further pushdown #6 #7 #8.
- Andy Fan identified the existing restriction on moving HAVING conditions into WHERE and demonstrated that bypassing it could change results; Andrew Gierth explained that selectively allowing this would require additional grouping-set analysis and complexity #9 #10 #11.
- Tom Lane chose to disable qual pushdown for subqueries with nonempty grouping sets rather than introduce that analysis, posting a patch without tests in the thread's final message #12.
- The fix subsequently landed as 4d346def15 from #12, with an expanded comment and regression tests, and was backpatched through PostgreSQL 9.5; more selective pushdown remained a possible future improvement.