Query icon
Log icon
If icon
SlackIncomingWebhook icon
Loop icon
Fail icon
Schedule icon

TimescaleDB Compression Policy Gap Guard

Detect TimescaleDB hypertables that never compress, report uncompressed bytes at risk, compress the backlog chunks and verify against the catalog.

Categories
DataInfrastructure

A hypertable keeps its rows in chunks. Compression is what keeps the volume from filling. Two things go wrong quietly. A hypertable gets created without a compression policy, so every chunk it ever writes stays uncompressed. Or a policy exists but the chunks past its window are still uncompressed, because the job is failing, somebody paused it or the policy was added long after the data. Nothing errors. The dashboards stay green until the disk is full.

This blueprint reads timescaledb_information.hypertables, timescaledb_information.jobs, timescaledb_information.job_stats and timescaledb_information.chunks, classifies each hypertable and reports the uncompressed bytes sitting past the window. It can then enable columnstore, compress those chunks one at a time, turn the policy on and read the catalog again to confirm every targeted chunk is compressed.

This blueprint was created by zkasuran.

Findings

The four states it separates and what each one means on a real cluster:

Status What the catalog shows Why it costs money
NO_POLICY No row in timescaledb_information.jobs with proc_name policy_compression Nothing will ever compress. Storage grows with raw row size forever
POLICY_FAILING A policy row exists, but scheduled is false or last_run_status is Failed or compression_enabled is false on the hypertable The policy sits in the inventory so the hypertable looks covered while it compresses nothing
CHUNKS_PAST_WINDOW A policy is present and its job is healthy, yet chunks with range_end older than compress_after are still is_compressed = false A backlog the job is not clearing, usually because the policy arrived after the data or the window was shortened
OK Policy present, job healthy, nothing past the window Recent chunks inside the window are uncompressed by design and are not reported

compression_enabled false while a policy job exists is its own trap. ALTER TABLE ... SET (timescaledb.compress = false) is accepted while the policy stays in place. The job then fails on every run with columnstore policy failure.

total_failures is a lifetime counter, so it is reported as context but never sets the status. A job that failed once in March and has succeeded every night since is not a failing job. Treating it as one would leave the hypertable flagged forever.

Safety

  • Read-only unless auto_remediate is true and dry_run is false. The audit and the plan are two SELECT queries.
  • The plan is printed before anything runs, one line per statement, so the change is reviewable from the log of a dry run.
  • Compression is ordered: columnstore is enabled first, then the backlog chunks are compressed oldest first with a per hypertable ceiling, then the policy is added or resumed. The policy comes last so its job does not compete with the flow for the same chunk locks.
  • compress_chunk runs with if_not_compressed => true, so a chunk another job already compressed is a notice rather than a failure.
  • Nothing is dropped, deleted or decompressed. The worst case of a wrong window is a chunk compressed earlier than intended, which decompress_chunk reverses.

How it works

  1. audit (io.kestra.plugin.jdbc.postgresql.Query, read-only) joins the hypertable list, the compression policy jobs with their run statistics, plus every chunk. It returns one row per hypertable with the status, the chunk counts, the uncompressed bytes past the window from pg_total_relation_size, the hypertable total from hypertable_detailed_size() and a plain reason.
  2. plan returns one row per remediation statement, ordered, with the chunk ceiling applied. Nothing in it runs unless both gates are open.
  3. summary logs every hypertable, the megabytes at risk and the plan.
  4. route (io.kestra.plugin.core.flow.If):
    • clean: logs it.
    • gaps found: alert posts to Slack.
      • remediate_gate opens only when auto_remediate is true and dry_run is false.
      • enable_columnstore, compress_backlog and enable_policy each run their statements with concurrencyLimit: 1.
      • recheck counts, from the catalog, how many of the named chunks are still uncompressed and how many touched hypertables still lack columnstore or a scheduled policy.
      • compression_stats reads chunk_compression_stats() for the bytes before and after.
      • verify_result fails the run when a targeted chunk is still uncompressed or a policy is still not in place. Otherwise it logs the bytes reclaimed and what is left for the next run.
  5. errors alerts Slack when the database cannot be reached.

What you get

  • A per hypertable verdict that separates "no policy" from "policy that is not working" from "backlog", instead of one compression ratio number.
  • The uncompressed bytes already past the window, so the gap is a figure rather than a feeling.
  • A backlog cleared in bounded runs, oldest chunk first, with the policy switched on only after the backlog is gone.
  • A run that fails when the compression did not actually land, checked against the catalog rather than against the exit code of the statement.

Who it's for

  • Teams running TimescaleDB for metrics, events or IoT history, where storage is the main bill.
  • Platform teams who inherited hypertables created by several services over several years.
  • Anyone who found out a policy had been paused for months only when the volume alert fired.

Why orchestrate this with Kestra

The gap appears whenever somebody adds a hypertable, shortens a window or pauses a job, so a one time check is worth little. Kestra runs the audit every morning, sends the hypertables that will not compress and the bytes at risk to Slack and keeps the fix behind auto_remediate and dry_run. Compression is slow and holds a lock per chunk, so a scheduled, sequential, bounded, re-verified run suits it.

Tested end to end

On Kestra 2.0.5 OSS against TimescaleDB 2.30.2 on PostgreSQL 16, with a local HTTP sink standing in for Slack. Four hypertables, each 57,601 rows over 20 days in 21 daily chunks, built into one state each:

Hypertable State built Catalog before
sensor_readings Never given a policy 0 of 21 chunks compressed, 13 past a 7 day window
app_events Policy 1007 present, columnstore switched off underneath it Last run Failed, 0 of 21 compressed
metrics_raw Policy 1006 created with a 60 day window, then shortened to 2 days Last run Success, 0 of 21 compressed, 17 past the window
api_latency Policy 1005 with a 2 day window, caught up 18 of 21 compressed, the 3 recent chunks inside the window
Run Execution Inputs Result
1 4V8nRTG6TMTKeyHenYBT0q defaults 3 gaps, 16.4 MB at risk. NO_POLICY, POLICY_FAILING and CHUNKS_PAST_WINDOW each reported once, api_latency OK. Plan 50 statements. Alert sent
2 4AYCHqqkUTB7XTz7vSKZx7 auto_remediate: true, dry_run: true Identical report, with psql confirming the same 21 uncompressed chunks on every table
3 4Gpn0YnQWGt7mWJzuV021m auto_remediate: true, dry_run: false 47 chunks compressed, columnstore and the missing policy added. chunk_compression_stats(): 16.4 MB to 5.2 MB, 11.1 MB reclaimed, 66.5 to 68.9 percent saved
4 ijhRop8EGYNVrYQaVysNa auto_remediate: true, dry_run: false, one late chunk held under a competing write lock 20 of 21 compressed, recheck reports the blocked chunk still uncompressed, verify_result fails the run, errors alerts
5 6uoPb7cVFZe2xwatqlvnYe same, lock released The last chunk compressed, gate passes
6 6U2vN1KblZz9usmJmE5ufV defaults Clean, all four hypertables OK
7 4c3CN9TGPP7EhwROgpWI6H defaults, after remove_compression_policy and a full decompress NO_POLICY caught again, 21 of 21 chunks uncompressed in psql, 13 past the window
8 2sNCG840IGRcuTZjPf1pw7 then 2kYzV7ZSMxJK9obKjns659 detect, then remediate Policy dropped back to unsigned plus a paused job on another table: NO_POLICY and POLICY_FAILING reported, then 13 chunks compressed and the paused job resumed, 10 MB to 3.5 MB
9 4lTHU6u9wy81LXsAL6FDsP unreachable database Audit fails, errors alerts

Every number in the table was read back from psql against timescaledb_information.chunks and chunk_compression_stats(), not from the Kestra log alone.

Prerequisites

  • TimescaleDB 2.x. The flow uses timescaledb_information views, hypertable_detailed_size(), chunk_compression_stats(), add_compression_policy(), compress_chunk() and alter_job(), all present in 2.9 and later.
  • The audit needs a role that can read the information views and call the size functions. Remediation needs the owner of the hypertables.
  • Time partitioned hypertables only. A hypertable partitioned on an integer column is skipped, because its compress_after is a number of units rather than an interval.
  • Local testing:
    • docker run -d --name ts -p 5432:5432 -e POSTGRES_PASSWORD=ts -e POSTGRES_DB=metrics timescale/timescaledb:latest-pg16;
    • create a hypertable, load a few weeks of rows, then leave one without a policy.

Secrets

  • POSTGRES_USERNAME / POSTGRES_PASSWORD: credentials used by every query.
  • SLACK_WEBHOOK_URL: used by alert and the errors block.
  • In Kestra OSS, provide each one as a base64-encoded environment variable prefixed SECRET_.

Inputs

Input Default Purpose
jdbc_url jdbc:postgresql://localhost:5432/metrics TimescaleDB database
schemas public Comma separated schemas to scan
fallback_compress_after 7 days Window used for a hypertable that has no policy
grace_period 1 hour Added to the window before a chunk counts as late
max_chunks_per_hypertable 25 Ceiling on chunks compressed per hypertable per run
auto_remediate false Allow the plan to run
dry_run true Must be false for anything to be compressed

Quick start

  1. Add the POSTGRES_USERNAME, POSTGRES_PASSWORD and SLACK_WEBHOOK_URL secrets.
  2. Import the flow, set jdbc_url and schemas.
  3. Run it with the defaults and read the plan in the summary log.
  4. Pick a fallback_compress_after that matches your retention, then run with auto_remediate: true and dry_run: false.
  5. Enable the daily trigger, which always runs in safe mode.

How to extend

  • Add a Pause before compress_backlog when the bytes at risk cross a threshold, so a first large backlog gets an approval.
  • Also check the retention policies in the same pass, since a hypertable with neither compression nor retention is the real storage risk.
  • Loop over several JDBC URLs to cover every database of a fleet from one run.
  • Record the per hypertable bytes into a table and chart how the compressed share moves week to week.
  • Set compress_segmentby per hypertable before enabling columnstore, since the default picks no segmenting column and the ratio depends on it.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.