liveBook · Manning

Retention policy

As explained in Just Use Postgres!

A data retention policy is a rule that automatically removes time-series chunks containing data older than a specified age.

A retention policy automatically removes time-series chunks containing data older than a configured age. On a hypertable, TimescaleDB schedules a job to drop chunks that contain only expired data. section 9.4

Configure retention around the data lifecycle:

  • Create a 30-day policy with add_retention_policy('watch.heart_rate_measurements', INTERVAL '30 days'). section 9.4
  • Change the schedule_interval to run cleanup every 12 hours instead of daily. section 9.4
  • Apply the same mechanism to continuous aggregates, such as keeping aggregate chunks for seven days. section 9.6.2

Protect sample data before testing

A dataset dated 2024 and 2025 may fall entirely outside a 30-day window if the policy is created later. Test carefully: the policy can remove all records from the example measurements table. section 9.4

SELECT add_retention_policy(
  'watch.heart_rate_measurements', INTERVAL '30 days');

Inspect the basic SQL command for adding a 30-day retention policy to a hypertable. section 9.4

How Just Use Postgres! covers it

How deep it goes
Defines retention intervals and cleanup schedules, but defers broader administration and lifecycle design.
Examples
Uses 30-day heart-rate measurement retention and seven-day low-heart-rate aggregate retention.
What it assumes
Assumes a TimescaleDB hypertable or continuous aggregate exists and requires choosing an appropriate data-age window.
What you can do after
Operate/configure TimescaleDB cleanup with add_retention_policy and adjustable schedule intervals.
How it connects
Links hypertables, chunks, time-series data, and continuous aggregates.

Also explained in

Compare how 10 books explain retention policy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.