The dashboard takes 14 seconds to load. The query is a SELECT time_bucket('5 min', ts), avg(value) over the last 7 days of a 400-million-row hypertable. The first reflex is to add an index. The right reflex is to stop scanning 400 million rows for a question whose answer is a few thousand pre-aggregated points.
What’s happening
A continuous aggregate is a materialized view backed by a TimescaleDB hypertable. You write a normal time_bucket() query; TimescaleDB stores its output as a second hypertable that is incrementally maintained as new data arrives. Dashboards query the aggregate, not the raw data, and the planner reads kilobytes instead of gigabytes. Three design decisions decide whether this actually works:
- Bucket size. The bucket has to match the smallest resolution your dashboard ever displays. If you build a
5 minaggregate and someone wants per-minute granularity, the aggregate is useless for that view. Common pattern: build a1 minbase aggregate, then build hierarchical aggregates (5 min,1 hour,1 day) on top of the previous level — each one queries the level below, not the raw hypertable. - Refresh policy.
add_continuous_aggregate_policy()declares “refresh data older thanstart_offsetand younger thanend_offseteveryschedule_interval.” Setend_offsettoo small and you refresh data that’s still being inserted, producing partial buckets that get overwritten on the next run. Set it too large and the dashboard sees stale data for that lag window. Rule of thumb:end_offset= max expected late-arrival window + a couple of minutes of safety. - Real-time aggregates. Materialized continuous aggregates only have data up to the last refresh. By default TimescaleDB unions the materialized result with a live
time_bucket()over the raw hypertable to give you a real-time view — but if your raw hypertable is huge, that live tail can dominate query time. Disable real-time withWITH (timescaledb.materialized_only = true)if you can tolerate a few minutes of staleness for a 100x speedup.
How to verify
Inspect the aggregate definition and refresh policy:
SELECT view_name, materialization_hypertable_name,
view_definition
FROM timescaledb_information.continuous_aggregates;
SELECT job_id, application_name, schedule_interval, config
FROM timescaledb_information.jobs
WHERE proc_name = 'policy_refresh_continuous_aggregate';
Confirm the dashboard query actually hits the aggregate:
EXPLAIN ANALYZE
SELECT bucket, avg_value
FROM metrics_5min
WHERE bucket > now() - interval '7 days';
You should see Index Scan on the aggregate’s materialization hypertable — no scan of the raw metrics table. If you see the raw table being scanned, the query window is including data that hasn’t been materialized yet and materialized_only is false; either narrow the window or accept the read against the raw table for that tail.
The fix
A working hierarchy for a metrics workload looks like this:
-- Base 1-minute aggregate over the raw hypertable
CREATE MATERIALIZED VIEW metrics_1min
WITH (timescaledb.continuous, timescaledb.materialized_only = false) AS
SELECT time_bucket('1 minute', ts) AS bucket,
tenant_id,
metric_name,
avg(value) AS avg_value,
max(value) AS max_value,
min(value) AS min_value,
count(*) AS sample_count
FROM metrics
GROUP BY bucket, tenant_id, metric_name
WITH NO DATA;
SELECT add_continuous_aggregate_policy(
'metrics_1min',
start_offset => INTERVAL '7 days',
end_offset => INTERVAL '2 minutes',
schedule_interval => INTERVAL '1 minute'
);
-- 5-minute aggregate built FROM the 1-minute aggregate
CREATE MATERIALIZED VIEW metrics_5min
WITH (timescaledb.continuous, timescaledb.materialized_only = true) AS
SELECT time_bucket('5 minutes', bucket) AS bucket,
tenant_id, metric_name,
avg(avg_value) AS avg_value,
max(max_value) AS max_value,
min(min_value) AS min_value,
sum(sample_count) AS sample_count
FROM metrics_1min
GROUP BY 1, tenant_id, metric_name
WITH NO DATA;
SELECT add_continuous_aggregate_policy(
'metrics_5min',
start_offset => INTERVAL '30 days',
end_offset => INTERVAL '10 minutes',
schedule_interval => INTERVAL '5 minutes'
);
-- 1-hour aggregate built FROM the 5-minute aggregate
CREATE MATERIALIZED VIEW metrics_1hour
WITH (timescaledb.continuous, timescaledb.materialized_only = true) AS
SELECT time_bucket('1 hour', bucket) AS bucket,
tenant_id, metric_name,
avg(avg_value) AS avg_value,
max(max_value) AS max_value,
min(min_value) AS min_value,
sum(sample_count) AS sample_count
FROM metrics_5min
GROUP BY 1, tenant_id, metric_name
WITH NO DATA;
SELECT add_continuous_aggregate_policy(
'metrics_1hour',
start_offset => INTERVAL '180 days',
end_offset => INTERVAL '1 hour',
schedule_interval => INTERVAL '30 minutes'
);
-- Refresh the base aggregate's history once
CALL refresh_continuous_aggregate('metrics_1min', NULL, NULL);
A few patterns that come up repeatedly:
- Percentiles.
avgandmaxare obvious. For p95/p99 dashboards, build atdigest-based aggregate using thetimescaledb_toolkitextension — store the digest object in the aggregate and runapprox_percentile(0.95, digest)at query time. You cannot do this with vanillapercentile_contbecause percentile aggregates aren’t algebraically composable. - Late-arriving data. If your ingest can be 30 minutes late (mobile clients buffering offline, for example), set
end_offsetto 35 minutes minimum. Smaller and you’ll see “fixed” buckets that change values as late samples arrive. - Backfill. A new continuous aggregate on a multi-year hypertable should be refreshed in chunks —
CALL refresh_continuous_aggregate('metrics_5min', '2025-01-01', '2025-02-01');etc. — to avoid one monstrous transaction and a memory-pressure incident. - Compression and aggregates. You can compress chunks of the underlying hypertable independently of the aggregate, and you can compress chunks of the aggregate’s materialization hypertable too. Don’t enable both at the same time without testing — query planning on doubly-compressed paths is materially slower in older TimescaleDB versions.
Dashboards should query metrics_1hour for views longer than 7 days, metrics_5min for 24h-7d views, and metrics_1min for the last 24 hours. Anything finer than a minute hits the raw hypertable directly — which is fine for short windows because the chunk planner is already efficient there.
For TimescaleDB workloads we operate end-to-end — aggregate design reviews, chunk-pressure alerting, late-data backfill runs — see Cloud Compute under Managed Cloud Ops, or book a query-performance audit on your existing dashboard fleet.