Wich strikes me is that if I try to simplify it a lot, removing all data but the main table (occtax.observation) primary key cd_nom and aggregate, the query plan should be able tu use the cd_nom index for sorting and provide better query plan (hash aggregate), but it does not seems so :
HashAggregate doesn't support aggregates with DISTINCT. I don't think there is any reason it can't, it is just that no one has gotten around to it.
Aggregates with DISTINCT also kill your ability to get parallel queries.