TimescaleDB Toolkit provides specialized functions for time-series analytics using a two-step aggregation pattern. Most functions create intermediate representations that accessor functions then query, enabling efficient reuse and multiple analyses.
Approximate Analytics
HyperLogLog - Distinct Count Estimation
Probabilistic distinct counting with configurable precision for high-cardinality datasets.
-- Estimate unique users per day
SELECTdate_trunc('day',timestamp)asday,distinct_count(hyperloglog(64,user_id))asunique_usersFROMeventsGROUPBYday;-- Combine counts across partitions
SELECTdistinct_count(rollup(hll))FROM(SELECThyperloglog(32,session_id)ashllFROMevents_2023UNIONALLSELECThyperloglog(32,session_id)FROMevents_2024)t;
T-Digest - Quantile Approximation
High-accuracy percentile estimation optimized for tail quantiles (P95, P99).
-- Track response time percentiles
SELECTservice_name,approx_percentile(0.50,tdigest(100,response_time))asp50,approx_percentile(0.95,tdigest(100,response_time))asp95,approx_percentile(0.99,tdigest(100,response_time))asp99FROMapi_metricsGROUPBYservice_name;-- Hourly percentiles with continuous aggregation
CREATEMATERIALIZEDVIEWhourly_percentilesASSELECTtime_bucket('1 hour',timestamp)ashour,tdigest(200,response_time)asdigestFROMrequestsGROUPBYhour;
UddSketch - Bounded Error Quantiles
Quantile estimation with guaranteed maximum relative error bounds.
-- CPU utilization percentiles with 1% max error
SELECThost_id,approx_percentile(0.95,uddsketch(100,0.01,cpu_percent))asp95_cpu,error(uddsketch(100,0.01,cpu_percent))asactual_errorFROMsystem_metricsGROUPBYhost_id;
Counter Analytics
Counter Aggregates - Monotonic Metrics
Handle counters that increase monotonically with automatic reset detection.
Analytics for metrics that vary up and down (temperature, memory usage).
-- Temperature change analysis
SELECTsensor_id,delta(gauge_agg(timestamp,temperature))astemp_delta,rate(gauge_agg(timestamp,temperature))astemp_rate_per_secFROMweather_dataGROUPBYsensor_id;
Time-Weighted Analytics
Time-Weighted Averages
Handle irregularly sampled data with interpolation methods (LOCF, Linear).
-- Weighted average for irregular sensor readings
SELECTdevice_id,average(time_weight('LOCF',timestamp,sensor_value))asweighted_avg,average(time_weight('Linear',timestamp,sensor_value))aslinear_avgFROMiot_readingsGROUPBYdevice_id;-- Combining multiple time ranges
SELECTaverage(rollup(tw))FROM(SELECTtime_weight('LOCF',ts,val)astwFROMreadings_2023UNIONALLSELECTtime_weight('LOCF',ts,val)FROMreadings_2024)t;
Data Visualization
LTTB Downsampling
Downsample time series while preserving visual similarity for charts.
-- Reduce 100K points to 1K for visualization
SELECTtime,valueFROMunnest((SELECTlttb(timestamp,price,1000)FROMstock_pricesWHEREsymbol='AAPL'));
ASAP Smoothing
Generate human-readable graphs by reducing noise while preserving trends.
-- Smooth daily data to weekly resolution
SELECTtime,valueFROMunnest((SELECTasap_smooth(date,daily_sales,52)FROMsales_dataWHEREdate>='2023-01-01'));
Statistical Analysis
Stats Aggregates
Comprehensive statistical analysis with 1D and 2D regression capabilities.
Store intermediate aggregates for multiple analyses:
-- Step 1: Create aggregates
CREATETABLEdaily_summariesASSELECTdate_trunc('day',timestamp)asday,tdigest(200,response_time)asresponse_digest,stats_agg(response_time,request_size)asstatsFROMrequestsGROUPBYday;-- Step 2: Multiple analyses from same data
SELECTday,approx_percentile(0.50,response_digest)asmedian,approx_percentile(0.99,response_digest)asp99,average(stats)asavg_response,slope(stats)assize_correlationFROMdaily_summaries;
All functions in the experimental schema (toolkit_experimental) may change between versions. Use stable functions for production workloads requiring API stability.
Version Notes
timescaledb_toolkit 1.23.0 has two behavior changes to account for in tests: max_n, max_n_by, min_n, and min_n_by return an empty result instead of raising on empty streams, and 1D aggregations now error on values that would lose 53-bit precision instead of silently returning wrong data. The release also marks time_weight and its rollup parallel-unsafe, updates to pgrx 0.18, and fixes ASAP smoothing sortedness checks.