Partitioning, Pruning & Retention in SQL
DROP PARTITION beats giant DELETE — prune at plan time
RANGE-partition by time (events_2024_01…), LIST by region, HASH for spread. Planner prunes untouched children via constraint exclusion — scans skip whole partitions. Retention drops whole partitions instantly with no bloat or WAL storm. Sub-partition hot tenants/regions. Measure: partition count vs planning overhead — hundreds fine, thousands need care.
Partition by time
RANGE monthly children — one table image, many physical files.
Prune at plan
created_at predicates skip untouched children via exclusion.
Retain by drop
DROP PARTITION instant — no row WAL, no bloat.
Size the count
Hundreds fine; thousands slow planning. Local indexes per child.
1-- retention gold:2ALTER TABLE events DROP PARTITION FOR ('2022-01-01');
| strategy | cost |
|---|---|
| drop partition | instant, no bloat |
| giant DELETE | locks + WAL storm |
Catalog change detaches January 2022 — pages freed without row logging. The DELETE twin would log every row, bloat, and lag replicas for hours.
January touches January
Planner skips eleven siblings.
1SELECT * FROM events WHERE created_at >= DATE '2024-01-01' AND created_at < DATE '2024-02-01';
| child | fate |
|---|---|
| events_2024_01 | scanned |
| others | pruned |
Drop 2022 whole
Catalog change, not row delete.
1ALTER TABLE events DROP PARTITION FOR ('2022-01-01');
| action | cost |
|---|---|
| drop | instant, no WAL storm |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.