Recently, there has been no shortage of popular blog posts about how Postgres scales or why you should use it for everything . In this post, we'll do something a little different: describe an issue we had with a Postgres feature that intuitively should scale, but actually doesn't. SELECT DISTINCT is an innocuous-seeming clause that finds all unique values of a column. However, its performance characteristics aren't what you'd expect: no matter how you index your table, no matter how few unique values there are to retrieve, SELECT DISTINCT will always scan every row that matches its predicates. We recently observed this when diagnosing the performance of a Postgres-backed queues workload, where SELECT DISTINCT turned out to be the most expensive query despite appearing to be the simplest and cheapest. In this blog post, we’ll explain what happened, what design decisions in Postgres make SELECT DISTINCT slow, and how to work around it.…