Bookmarks

Partitioning

Splitting one logical database table into several physical parts — partitions — by a rule on a given key.

Also: table partitioning

checked 18 Nov

All terms

The application still sees one table: queries against it stay the same, while the data physically sits in separate partitions. Partitioning is applied to large tables: it speeds up queries on the partitioning key and lets you delete stale data a whole partition at a time instead of walking the table row by row.

How it works

The partitioning rule is set by the partition key — a column or an expression over it, usually a date, a region or a customer identifier. A partitioned table stores nothing itself — it is a “virtual” container, and the data lives in the partitions: a row lands in its partition by the key value. A query with a condition on the key prunes the unneeded partitions before reading: instead of the whole table the database scans one or two, and if most of the load falls on a few partitions, their indexes fit into memory more easily. PostgreSQL’s declarative partitioning supports three partitioning methods:

  • range (RANGE) — by key intervals: month by month, year by year;
  • list (LIST) — by a list of values: region, document type, branch;
  • hash (HASH) — by the hash of the key, to spread the load evenly.

An example from the PostgreSQL documentation: a company records peak temperatures and ice-cream sales across cities, with a partition created for each month by the measurement date — queries with a condition on the date read one or two partitions, and “deleting” an old month is a quick partition detach instead of billions of row-by-row deletions:

sql
CREATE TABLE measurement (    city_id   int NOT NULL,    logdate   date NOT NULL,    peaktemp  int,    unitsales int) PARTITION BY RANGE (logdate); CREATE TABLE measurement_y2006m02 PARTITION OF measurement    FOR VALUES FROM ('2006-02-01') TO ('2006-03-01');

Benefits

  • fast queries on the key: partition pruning instead of scanning the whole table;
  • fast deletion of stale data by a whole partition (DETACH or DROP);
  • moving and maintaining partitions individually: VACUUM, indexes, dedicated storage;
  • moving partitions to other disks and tablespaces.

The trade-off is choosing the rules with care: a query condition that omits the partitioning key scans every partition. Too many partitions mean slow query planning and memory spent on their metadata; too few mean large indexes and poor data locality. The key must be part of the table’s primary key and unique constraints, and changing a row’s key value moves the row to another partition.

Partitioning and sharding

Both words mean “splitting data into parts”, but the scale differs. Partitioning divides one table within one database; sharding spreads data across the nodes of a cluster. Partitioning is about data management and query speed; sharding is about scale, when a single machine can no longer cope. In Kafka, “partition” means something different: it is a unit of a topic’s parallelism, a part of its log, and events with the same key are always written to the same partition.

CriterionPartitioningSharding
Where the parts livewithin one databaseon different cluster nodes
What it solvesthe size of a single table and query speedwrite and storage scale beyond a single node
What the application needsnothing: the queries stay the samerouting to a shard or a proxy

Often the two techniques work together: a table is partitioned by date, and the partitions of a sharded database are themselves spread across nodes. The terms are used loosely, so in documentation it is worth clarifying what exactly is being split: a table within one database is partitioning, a dataset spread across nodes is sharding.

Sources

the term is written from these pages

Verified 18 Nov

Not to be confused with

what it is mixed up with and how it differs

  • sharding

    Sharding spreads data across the nodes of a cluster; partitioning divides a table within one database.

In depth in the references

where the topic is covered in detail