Planner & statistics

n_distinct

Also called: distinct estimate, stadistinct

n_distinct is PostgreSQL's estimate of how many different values a column holds. A positive number is a plain count; a negative number is a fraction of the table, for example -1 means every value is unique.

What this means

An estimate of how many different values a column holds. A positive number is a straight count; a negative number is a fraction of the table, so -1 means "every value is unique." The planner uses it to guess how many groups a GROUP BY or how many matches an equality filter will produce.

Why it matters operationally

It drives grouping, join, and DISTINCT estimates. Because it is calculated from a small sample, it is often wrong on large high-cardinality columns, and a wrong n_distinct is one of the most common causes of a bad plan. You can override it by hand with ALTER TABLE ... ALTER COLUMN ... SET (n_distinct = ...).

Related & next

Mentioned from

← All glossary terms · GUC reference · Error catalog