I wrote:
> Immediate impression is that the logic for planning partial grouped
> aggregation did not get the zero-sort-keys case right.
Yeah: after flattening your view, the planner was left with GROUP BY
'S3'::character varying, which is a no-op, but it mistakenly inserted
a no-op Sort into the plan to sort by that. One-liner fix is here
if you need it right away:
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=7defc3b97a31537547053946808a83e7234d1b61
regards, tom lane