Hyperfunctions overview
Functions that enable you to analyze time-series data efficiently. These functions provide essential capabilities for time bucketing, data distribution analysis, and gapfilling.
For additional hyperfunctions, use the TimescaleDB Toolkit PostgreSQL extension.
Samples
Section titled “Samples”Time bucketing with aggregates
Section titled “Time bucketing with aggregates”Bucket temperature readings into 5-minute intervals and calculate statistics:
SELECT time_bucket('5 minutes', time) AS bucket, avg(temperature) AS avg_temp, first(temperature, time) AS first_temp, last(temperature, time) AS last_tempFROM readingsGROUP BY bucketORDER BY bucket DESC;Gapfilling missing data
Section titled “Gapfilling missing data”Fill gaps in time-series data using last observation carried forward (LOCF):
SELECT time_bucket_gapfill('1 hour', time) AS bucket, device_id, locf(avg(temperature)) AS filled_avg_tempFROM readingsWHERE time >= NOW() - INTERVAL '1 day'GROUP BY bucket, device_idORDER BY bucket DESC;Analyzing data distribution
Section titled “Analyzing data distribution”Create a histogram of response times to understand distribution patterns:
SELECT histogram(response_time_ms, 0, 1000, 10)FROM api_requestsWHERE time > NOW() - INTERVAL '1 day';Approximate row counting
Section titled “Approximate row counting”Get a fast estimate of table size without scanning all data:
SELECT approximate_row_count('readings');Available functions
Section titled “Available functions”Time series utilities
Section titled “Time series utilities”time_bucket(): bucket rows by time intervalfirst(): get the first value ordered by another columnlast(): get the last value ordered by another columndays_in_month(): calculate days in a monthmonth_normalize(): normalize monthly metrics
Gapfilling
Section titled “Gapfilling”time_bucket_gapfill(): bucket time and fill gaps in resultslocf(): last observation carried forward for filling gapsinterpolate(): linear interpolation for filling gaps
Distribution analysis
Section titled “Distribution analysis”histogram(): create histograms to visualize data distributionapproximate_row_count(): fast approximate count of rows in a table
Additional hyperfunctions
Section titled “Additional hyperfunctions”For advanced time-series analysis including statistical analysis, percentile approximation, state tracking, and more, see the TimescaleDB Toolkit API reference.
Deprecated hyperfunctions
Section titled “Deprecated hyperfunctions”time_bucket_ng(): next generation time bucketing with additional features