---
title: "Postgres SELECT DISTINCT Does Not Scale"
description: "Postgres SELECT DISTINCT always scans every index entry matching its predicates, so its cost grows with total matching rows rather than with the number of distinct values."
url: "https://swe.quest/articles/postgres-select-distinct-does-not-scale-d7d1eb15cb6eae99"
canonical: "https://www.dbos.dev/blog/postgres-select-distinct-does-not-scale"
source: "DBOS"
published: "2026-08-10"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 5
tags: ["postgres","query-engines","sql","performance"]
---

# Postgres SELECT DISTINCT Does Not Scale

A post on [DBOS](https://swe.quest/sources/dbos.md), published 2026-08-10, 5 min read, Deep dive. Original: [https://www.dbos.dev/blog/postgres-select-distinct-does-not-scale](https://www.dbos.dev/blog/postgres-select-distinct-does-not-scale)

## Summary

Postgres SELECT DISTINCT always scans every index entry matching its predicates, so its cost grows with total matching rows rather than with the number of distinct values. In DBOS's Postgres-backed partitioned queue, finding active partitions took seconds on 'narrow but deep' queues, even with a matching partial index. Postgres has no loose index scan like MySQL's, and the Postgres 18 skip scan doesn't help here. The workaround is a recursive CTE that repeatedly selects min(key) greater than the previous key, giving O(distinct values) cost and a flat ~1 ms latency up to 1M rows per partition.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Query engines](https://swe.quest/tags/query-engines.md) (Data & databases)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- SQL query basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- B-tree indexes ([easy posts on Storage engines](https://swe.quest/tags/storage-engines.md?difficulty=easy))
- Postgres EXPLAIN plans ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))

## Keywords

PostgreSQL, SELECT DISTINCT, loose index scan, skip scan, recursive CTE, DBOS, MySQL

## Related posts

- [PostgreSQL Stories: From slow query to fast—via stats](https://swe.quest/articles/postgresql-stories-from-slow-query-to-fast-via-stats-ac7001ab0fde8224.md): Render · 2024-05-14 · Medium · 13 min read · PostgreSQL, Query engines, Performance engineering, Debugging
  A simple LIMIT 1 join between a ~100k-row postgres\_dbs table and a ~90M-row events table ran for hours.
- [You should shard your database](https://swe.quest/articles/you-should-shard-your-database-12cdc70ed1f6d2d7.md): PgDog · 2025-11-13 · Medium · 7 min read · PostgreSQL, Distributed databases, Scaling, Query engines
  Sharding Postgres early avoids the operational pain of large single instances, illustrated by an outage where stale planner statistics caused a 300 GB table to be sequentially scanned.
- [Dynamic Table Partitioning in Postgres](https://swe.quest/articles/dynamic-table-partitioning-in-postgres-13de90b4fe06270e.md): Supabase · 2023-10-03 · Medium · 20 min read · PostgreSQL, Schema migrations, SQL, Scaling
  Postgres range partitioning can be retrofitted onto an existing "large table" (here, 500 million chat messages) without blocking queries by creating standalone daily child tables, bulk-loading them, adding a CHECK constraint that matches the partition range, and only then running ATTACH PARTITION so Postgres skips the validation scan and its blocking lock.
- [Implementing "seen by" functionality with Postgres](https://swe.quest/articles/implementing-seen-by-functionality-with-postgres-42ef1db52a88742a.md): Supabase · 2022-07-18 · Medium · 21 min read · PostgreSQL, Data structures, Performance engineering, Databases
  Four ways to track post views in Postgres are benchmarked: a naive counter column, an hstore of viewer IDs per post, an association table, and the HyperLogLog extension.
- [Primary keys in sharded databases](https://swe.quest/articles/primary-keys-in-sharded-databases-4b73737ab618f6da.md): PgDog · 2025-09-03 · Medium · 5 min read · PostgreSQL, Distributed databases, Storage engines
  PgDog generates globally unique primary keys across Postgres shards without extensions or external ID services.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
