Get started with TimescaleDB time-series database functionality in Relyt ONE. Learn how to create hypertables, insert time-series data, and run efficient queries.
Relyt ONE integrates TimescaleDB, an open-source time-series database extension for PostgreSQL. We use TimescaleDB Apache 2 Edition v2.21.3 to provide powerful time-series capabilities. For a detailed comparison of features between Apache 2 Edition and Community Edition, see the TimescaleDB editions feature comparison.
TimescaleDB is a powerful PostgreSQL extension that transforms your database into a high-performance time-series database. It’s designed for handling large volumes of time-stamped data while maintaining full SQL compatibility and leveraging PostgreSQL’s reliability.Key benefits:
Automatic partitioning via hypertables for efficient time-series data storage
Fast queries with optimized indexing and query planning
Full SQL support - no need to learn new query languages
Horizontal scaling capabilities for massive datasets
A hypertable is TimescaleDB’s core abstraction - it looks like a regular PostgreSQL table but is automatically partitioned by time.
Copy
-- Create a regular table firstCREATE TABLE sensor_data ( time TIMESTAMPTZ NOT NULL, sensor_id INTEGER NOT NULL, temperature DOUBLE PRECISION, humidity DOUBLE PRECISION, location TEXT);-- Convert it to a hypertableSELECT create_hypertable('sensor_data', by_range('time'));
The time column must be the first argument to create_hypertable() and should be of type TIMESTAMPTZ, TIMESTAMP, or DATE.
Always include a time column in your hypertables and ensure it’s properly indexed for optimal performance.
TimescaleDB provides powerful functions for time-series analysis:
Copy
-- Get latest readings per sensorSELECT DISTINCT ON (sensor_id) sensor_id, time, temperature, humidity, locationFROM sensor_data ORDER BY sensor_id, time DESC;-- Calculate hourly averages using time_bucketSELECT time_bucket('1 hour', time) AS hour, sensor_id, AVG(temperature) AS avg_temp, AVG(humidity) AS avg_humidityFROM sensor_data WHERE time > NOW() - INTERVAL '24 hours'GROUP BY hour, sensor_idORDER BY hour DESC, sensor_id;-- Find temperature trends over the last weekSELECT time_bucket('1 day', time) AS day, location, MIN(temperature) AS min_temp, MAX(temperature) AS max_temp, AVG(temperature) AS avg_tempFROM sensor_data WHERE time > NOW() - INTERVAL '7 days'GROUP BY day, locationORDER BY day DESC;
The most important function for time-series analysis - groups data into time intervals:
Copy
-- 15-minute intervalsSELECT time_bucket('15 minutes', time) AS bucket, AVG(temperature)FROM sensor_data GROUP BY bucket ORDER BY bucket;-- Daily intervals with offsetSELECT time_bucket('1 day', time, '6 hours'::INTERVAL) AS bucket, COUNT(*)FROM sensor_data GROUP BY bucket;
-- Get first and last temperature reading per daySELECT time_bucket('1 day', time) AS day, first(temperature, time) AS first_temp, last(temperature, time) AS last_tempFROM sensor_data GROUP BY dayORDER BY day;
Pre-compute common queries for faster performance:
Copy
-- Create a continuous aggregate for hourly averagesCREATE MATERIALIZED VIEW hourly_sensor_avg ASSELECT time_bucket('1 hour', time) AS hour, sensor_id, AVG(temperature) AS avg_temp, AVG(humidity) AS avg_humidityFROM sensor_data GROUP BY hour, sensor_id;-- Query the pre-computed viewSELECT * FROM hourly_sensor_avg WHERE hour > NOW() - INTERVAL '24 hours'ORDER BY hour DESC;
This guide covers the basics of using TimescaleDB in Relyt ONE. For comprehensive documentation on advanced features, best practices, and detailed API references, visit the official TimescaleDB documentation:
Getting Started: Begin with simple hypertables and basic queries. As your data grows, explore advanced features like continuous aggregates and compression in the official TimescaleDB docs.