I've been playing around with the attached patch which does:
1. Adjusts add_paths_to_grouping_rel so that we don't add a Sort path when we can add an Incremental Sort path instead. This removes quite a few redundant lines of code.
For unsorted paths, the original logic here is to explicitly add a Sort path only for the cheapest-total path. This patch changes that and may add a Sort path for other paths besides the cheapest-total path. I think this may introduce in some unnecessary path candidates.
I think it's good that this patch removes redundant codes. ISTM we can do the same when we try to finalize the partially aggregated paths from partially_grouped_rel.