Video in progress
Data has grown over the years and a table has become huge. How do you archive old data (hot/cold)?
Recent, actively used data stays in the fast database, while old records are moved to cheaper storage (a separate table, a partition or object storage). Partitioning by time turns moving and deleting into a single DETACH/DROP.
📖 Answer
The orders table reached 800 million rows in 5 years. Users look at the last 3 months 95% of the time. The indexes don't fit in memory, vacuum takes hours, and the backup is huge.
The main idea is to split data by age:
- Hot — the last few months, main DB, fast disk, full indexes.
- Warm — 1–2 years, separate partitions in the same DB or a cheaper replica.
- Cold — older years, Parquet files in object storage or in an analytical warehouse; for rare, slow queries.
Time-based partitioning in PostgreSQL:
CREATE TABLE orders (
id BIGINT, created_at TIMESTAMPTZ NOT NULL, ...
) PARTITION BY RANGE (created_at);
CREATE TABLE orders_2026_10 PARTITION OF orders
FOR VALUES FROM ('2026-10-01') TO ('2026-11-01');
-- detach the old month, move it to the archive, then drop it
ALTER TABLE orders DETACH PARTITION orders_2021_01;
Deleting a million rows with DELETE FROM orders WHERE created_at < ..., on the other hand, means a long transaction, a WAL explosion and bloat.
Don't forget:
- agree with the business on the retention period (tax and legal requirements);
- what happens when a user opens an old order — is it OK for it to work slowly, or is an "request from archive" feature enough;
- the partition key should be part of the queries, otherwise the DB looks at every partition.
Sign up to mark studied questions and save your progress.