# Postgres SELECT DISTINCT Scans Every Row, No Matter How You Index It

Original title: Postgres SELECT DISTINCT Does Not Scale

> Hacker News story 49835096 · language: en · submitted 2026-09-24
> Web version: https://zeli.app/story/49835096
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 102 · by [KraftyOne](<https://news.ycombinator.com/user?id=KraftyOne>) · 💬 28
[original](<https://www.dbos.dev/blog/postgres-select-distinct-does-not-scale>) · [HN discussion](<https://news.ycombinator.com/item?id=49835096>)

## Summary

Peter Kraft of DBOS describes a Postgres performance trap: SELECT DISTINCT always scans every row matching its predicates, even with a perfect index on queue name, status, and partition key. Diagnosing a partitioned queue workload, the team found the simplest-looking query was the most expensive. The post explains why Postgres makes this choice and how to work around it.

## Key quote

> 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.

## HN discussion (28 comments)

### [nattaylor](<https://news.ycombinator.com/item?id=49852837>)

> Loose index scan is made for this https://dev.mysql.com/doc/refman/8.0/en/group-by-optimizatio...
>
> Postgres doesn't have it yet https://wiki.postgresql.org/wiki/Loose_indexscan

### [procaryote](<https://news.ycombinator.com/item?id=49854032>)

> I've generally started to treat use of SELECT DISTINCT as a warning flag, as it's very common that it indicates bad code
>
> Some use it because they don't understand uniqueness constraints and try to fix it in post so to say. Some use it because they forgot a join condition and are absolute amateurs. Some use it because it fixed a problem for them once and now they add it everywhere
>
> These people seem to outnumber the people who use SELECT DISTINCT in a well thought out manner

### [sandeepkd](<https://news.ycombinator.com/item?id=49853718>)

> It says that the company is co-founded by Postgres creator. I find that bit hard to believe given that there is nothing novel in the article, probably discovery for them. I do understand that everyone has to go through their own journey to learn these things but at the same time when you are running business then seeking professional help isnt a bad idea.
>
> Based on my experience queries like these cannot scale, whatever you do. However if you are already on a path where you had invested a lot in such queries then hire a DBA, if you are not far off then hire an architect to model the data for better performance.

---

## Cite

- Page: https://zeli.app/story/49835096
- Date: 2026-09-24
- Source: dbos.dev (https://www.dbos.dev/blog/postgres-select-distinct-does-not-scale)
- Hacker News: https://news.ycombinator.com/item?id=49835096

## Other languages

- zh: https://zeli.app/zh/story/49835096 · markdown: https://zeli.app/zh/story/49835096.md
- ja: https://zeli.app/ja/story/49835096 · markdown: https://zeli.app/ja/story/49835096.md
- ko: https://zeli.app/ko/story/49835096 · markdown: https://zeli.app/ko/story/49835096.md
- de: https://zeli.app/de/story/49835096 · markdown: https://zeli.app/de/story/49835096.md
- es: https://zeli.app/es/story/49835096 · markdown: https://zeli.app/es/story/49835096.md
- ru: https://zeli.app/ru/story/49835096 · markdown: https://zeli.app/ru/story/49835096.md
