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:
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 (
DETACHorDROP); - 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.
| Criterion | Partitioning | Sharding |
|---|---|---|
| Where the parts live | within one database | on different cluster nodes |
| What it solves | the size of a single table and query speed | write and storage scale beyond a single node |
| What the application needs | nothing: the queries stay the same | routing 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- PostgreSQL 18 — Documentation: 5.12. Partitioningpostgresql.org/docs/current/ddl-partitioning.html(opens in a new tab)
- MySQL 8.4 Reference Manual — Partitioning: Overviewdev.mysql.com/doc/refman/8.4/en/partitioning-overview.html(opens in a new tab)
- Apache Kafka — Introduction to Kafkakafka.apache.org/intro(opens in a new tab)
Verified 18 Nov