Learn SQL
-- performance.partitioning_pruning_retention

Partitioning, Pruning & Retention in SQL

DROP PARTITION beats giant DELETE — prune at plan time

--concept

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.

--how it works
1

Partition by time

RANGE monthly children — one table image, many physical files.

2

Prune at plan

created_at predicates skip untouched children via exclusion.

3

Retain by drop

DROP PARTITION instant — no row WAL, no bloat.

4

Size the count

Hundreds fine; thousands slow planning. Local indexes per child.

--visualization
WHERE Jan-2024 → events_2024_01 scanned, 11 siblings pruned
childfate
events_2024_01scanned
otherspruned (exclusion)
Stage 1 / 1
DROP PARTITION instant retention. BRIN fits ordered time children.
--example
query.sql
1-- retention gold:
2ALTER TABLE events DROP PARTITION FOR ('2022-01-01');
Result
strategycost
drop partitioninstant, no bloat
giant DELETElocks + 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.

--variants

January touches January

Planner skips eleven siblings.

query.sql
1SELECT * FROM events WHERE created_at >= DATE '2024-01-01' AND created_at < DATE '2024-02-01';
Result
childfate
events_2024_01scanned
otherspruned
1Constraint exclusion matches range to child bounds.
2EXPLAIN shows one child appended.
3Unbounded created_at scans all — always bound time.

Drop 2022 whole

Catalog change, not row delete.

query.sql
1ALTER TABLE events DROP PARTITION FOR ('2022-01-01');
Result
actioncost
dropinstant, no WAL storm
1Pages detached, not logged per row.
2Replicas apply one catalog change.
3Batched DELETE is the non-partitioned fallback.
--key facts
Pruninguntouched children skipped at plan
DROP PARTITIONretention gold — instant
Count carehundreds fine, thousands slow planning
--use cases
Event/log retention at scale
Time-scoped analytics with bounded scans
--under the hood
Declarative partitioning routes via partition bound — ATTACH/DETACH is the online archive path.
BRIN indexes shine on naturally-ordered time children — tiny, sufficient.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.