Approximate percentiles
Construct and run approximate percentage queries using uddsketch or tdigest
This walkthrough uses a response_times table to show approximate percentiles end to end. TimescaleDB implements them with bounded-memory sketches instead of sorting every value, which keeps latency predictable on large datasets and continuous aggregates.
By default, TimescaleDB Toolkit uses uddsketch, but you can also choose
tdigest. For algorithm trade-offs, see advanced aggregation methods.
Run an approximate percentage query
Section titled “Run an approximate percentage query”In this procedure, we use an example table called response_times that contains
information about how long a server takes to respond to API calls.
- Create a continuous aggregate for daily aggregatesCREATE MATERIALIZED VIEW response_times_dailyWITH (timescaledb.continuous)AS SELECTtime_bucket('1 day'::interval, ts) as bucket,percentile_agg(response_time_ms)FROM response_timesGROUP BY 1;
- Query the ninety-fifth percentile over the last 30 days
Re-aggregate the aggregate to get the last 30 days:
SELECT approx_percentile(0.95, percentile_agg) as thresholdFROM response_times_dailyWHERE bucket >= time_bucket('1 day'::interval, now() - '30 days'::interval); - Create an alertWITH t as (SELECT approx_percentile(0.95, percentile_agg(percentile_agg)) as thresholdFROM response_times_dailyWHERE bucket >= time_bucket('1 day'::interval, now() - '30 days'::interval))SELECT count(*)FROM response_timesWHERE ts > now()- '1 minute'::intervalAND response_time_ms > (SELECT threshold FROM t);
For more information about percentile approximation API calls, see the hyperfunction API documentation.