liveBook · Manning

Cardinality

As explained in PostgreSQL Mistakes and How to Avoid Them

Cardinality is the number or diversity of distinct values in a set or key column.

Cardinality is the number or diversity of distinct values in a set or key column. For PostgreSQL partitioning, a partition key needs sufficient cardinality to distribute rows evenly; otherwise partitions may become unbalanced or contain only a few rows. section 4.2

A timestamp key with useful cardinality

Partitioning erp.payments by monthly ranges of tstamp distributes 150,001,000 rows across time-based partitions. A query filtering on tstamp can use partition pruning and scan only payments_p_2022_3 instead of the entire table. section 4.2

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Explains distribution consequences and stops short of a general mathematical treatment.
Examples
Uses monthly timestamp partitions and contrasts full-table scanning with a plan scanning one partition.
What you can do after
Evaluate partition keys for even distribution and anticipate excessive scans or poor execution plans.
How it connects
Connects partition key selection with table partitioning, partition pruning, execution plans, and I/O.

Also explained in

Compare how 8 books explain cardinality →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.