Create a TimescaleDB continuous aggregate with an incremental refresh policy and enable real-time aggregation for sub-interval queries

domain: docs.timescale.com · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Create a continuous aggregate as a materialized view over the hypertable: CREATE MATERIALIZED VIEW hourly_metrics WITH (timescaledb.continuous) AS SELECT time_bucket('1 hour', time) AS bucket, device_id, avg(value) AS avg_val FROM metrics GROUP BY bucket, device_id
  2. Add an automatic refresh policy to keep the aggregate current: SELECT add_continuous_aggregate_policy('hourly_metrics', start_offset => INTERVAL '3 hours', end_offset => INTERVAL '1 hour', schedule_interval => INTERVAL '1 hour')
  3. The start_offset/end_offset define the refresh window relative to now(); end_offset should be at least one bucket width to avoid refreshing the still-writing current bucket repeatedly
  4. Enable real-time aggregation (default on since TimescaleDB 2.x) so queries against the continuous aggregate automatically union the materialized data with uncompressed live data: the view behaves as if fully up-to-date without waiting for the refresh job
  5. Query the continuous aggregate like a regular table: SELECT bucket, avg_val FROM hourly_metrics WHERE device_id = 'dev1' AND bucket > now() - INTERVAL '24 hours'
  6. Monitor refresh job health: SELECT * FROM timescaledb_information.job_stats WHERE job_id = (SELECT job_id FROM timescaledb_information.continuous_aggregates WHERE view_name = 'hourly_metrics')

Known gotchas

Related routes

Create a TimescaleDB continuous aggregate refresh policy with add_continuous_aggregate_policy
docs.timescale.com · 5 steps · unrated
Create TimescaleDB continuous aggregates to pre-compute hourly rollups
docs.timescale.com · 5 steps · unrated
Create a BigQuery materialized view with automatic incremental refresh for a frequently queried aggregation
cloud.google.com · 5 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans