Bruno Wolff III <bruno@wolff.to> writes:
> DISTINCT will require a sort step to remove duplicates. GROUP BY can use
> either a sort or hash aggregate plan. If there are few distinct values,
> the hash aggregate plan can be much faster.
The DISTINCT code hasn't been revisited in a long time. One obstacle to
improving it is that it's very tightly intertwined with ORDER BY. While
fixing that might be just a Small Matter Of Programming, it's not clear
how to decouple them without breaking DISTINCT ON.
regards, tom lane