model-writtenrun 9456 - may be wrong, links go to the source messages. Commit-by-commit LLM analysis is in the Commits dialog.
- Suresh Kumar R reported a nested DISTINCT and UNION ALL query failing with “could not find pathkey item to sort” on PostgreSQL 10.3 in #1; after David G. Johnston recommended upgrading in #2, Suresh confirmed the failure on 10.7 in #3.
- Tom Lane supplied a self-contained reproduction and initially observed failures only on 9.6 and 10 in #4; Amit Langote traced the extra MergeAppend sort key to convert_subquery_pathkeys and proposed limiting or truncating its output in #5.
- Tom rejected indiscriminate truncation because it could worsen mergejoin plans, identified the distinction between a subquery's syntactic output and its scan target list, and attached a separate fix enforcing exact or narrow target lists for Append and MergeAppend in #6.
- Tom then attached a complementary v10 patch checking sort-key availability against rel->reltarget in #7, concluding that genuinely useful outer sort keys would already be present there; this also removed the extra sort key that exposed the target-list assertion failure.
- The thread ended with Tom planning to apply both fixes and defer stronger regression cases in #7; 24c19e9f66 subsequently combined both attachments with branch-adjusted expected output and was backpatched through 9.6, leaving broader test coverage unresolved.