timescale/pg-aiguide/timescaledb-hyperfunctions
Claude Code and agent skill by timescale/pg-aiguide
- community
Checking that the SKILL.md file is still in the repository.
Use this skill when writing analytical SQL over time-series data with the TimescaleDB Toolkit (timescaledb_toolkit extension) hyperfunctions: approximate percentiles, statistical summaries, time-weighted averages, counter/gauge rates, uptime/heartbeat tracking, state durations, OHLC candlesticks, approximate distinct counts, top-N, and downsampling. **Trigger when user asks to:** - Compute percentiles/medians/p95/p99 over large or rolled-up time-series data - Compute rates or deltas from monotonic counters (Prometheus-style) or gauges - Compute time-weighted averages or integrals over irregularly sampled data - Track uptime/downtime from heartbeats, or time spent in each state - Build OHLC/candlestick or VWAP data for financial ticks - Store re-aggregatable summaries in continuous aggregates (two-step aggregation, rollup) - Approximate COUNT DISTINCT, find top-N / most frequent values, or downsample for charts **Keywords:** timescaledb_toolkit, hyperfunctions, percentile_agg, uddsketch, tdigest, approx_percentile, stats_agg, time_weight, counter_agg, gauge_agg, heartbeat_agg, state_agg, candlestick_agg, hyperloglog, approx_count_distinct, min_n, max_n, mcv_agg, lttb, asap_smooth, rollup, two-step aggregation
Install
npx vskill@latest install timescale/pg-aiguide/timescaledb-hyperfunctionsSKILL.md
The same SKILL.md was published earlier as timescale/pg-aiguide/timescaledb-hyperfunctions.
TimescaleDB Toolkit Hyperfunctions
Hyperfunctions are SQL aggregates and accessors from the timescaledb_toolkit extension for analysis that plain PostgreSQL aggregates handle badly: percentiles that can be re-aggregated, rates over resetting counters, averages over irregular samples, uptime, state durations, and more.
Setup
Only use functions from the default schema in persistent objects. Anything in the toolkit_experimental schema can change between releases, and ALTER EXTENSION ... UPDATE drops views, continuous aggregates, and functions that depend on it. All functions in this skill are stable.
The Two-Step Pattern (read this first)
Nearly every hyperfunction works in two steps:
- An aggregate builds a compact summary (
percentile_agg(val)→uddsketch,time_weight(...)→timeweightsummary, ...). - An accessor extracts a result from the summary (
approx_percentile(0.95, summary),average(summary), ...).
The pattern exists so summaries can be stored and re-aggregated:
- `rollup(summary)` merges summaries correctly. Use it to go from hourly to daily buckets.
- Combining entities depends on the summary type. Distribution summaries (
percentile_agg,uddsketch,tdigest,stats_agg,hyperloglog,mcv_agg,min_n/max_n) can be rolled up across entities, e.g. one p99 for all hosts. Time-series summaries (time_weight,counter_agg,gauge_agg,heartbeat_agg,state_agg) describe one series. They can only be rolled up over consecutive, non-overlapping time ranges of the same entity: rolling up overlapping summaries from different hosts raises an ordering error. Compute the per-entity result first, then combine the numbers (e.g.SUMof per-host rates for a fleet-wide rate). - Never average averages, percentiles, or rates.
AVG(p95_hourly)is not a daily p95.rollup(hourly_sketch) -> approx_percentile(0.95)is the correct value. - Store the summary in a continuous aggregate, not the final number. Then any accessor works at query time.
Choosing a Function
| Question | Aggregate | Key accessors | |
|---|---|---|---|
| p50/p95/p99, median | percentile_agg, uddsketch, tdigest | approx_percentile, approx_percentile_array, approx_percentile_rank, mean, error | |
| avg/stddev/variance that can be rolled up; regression | stats_agg (1D or 2D) | average, stddev, variance, skewness, kurtosis, num_vals, sum; 2D: slope, intercept, corr, determination_coeff, covariance | |
| Average over irregular sampling | time_weight | average, integral, interpolated_average, first_val, last_val | |
| Rate/delta of monotonic counters with resets | counter_agg | delta, rate, extrapolated_rate, irate_right, num_resets, interpolated_rate | |
| Rate/delta/trend of gauges (no reset handling) | gauge_agg (1.25+) | delta, rate, extrapolated_rate, irate_right, slope, num_changes, interpolated_delta | |
| Uptime/downtime from heartbeats | heartbeat_agg | uptime, downtime, live_ranges, dead_ranges, live_at, num_gaps, interpolated_uptime | |
| Time spent in each state | state_agg | duration_in, state_timeline, state_periods, state_at, into_values | |
| OHLC / VWAP | candlestick_agg (ticks), candlestick (pre-aggregated bars) | open, high, low, close, volume, vwap, open_time, ... | |
| Approx COUNT DISTINCT | hyperloglog, approx_count_distinct | distinct_count, stderror | |
| Top-N / bottom-N values (with rows) | max_n, min_n, max_n_by, min_n_by | into_values, into_array | |
| Most frequent values | mcv_agg | topn, max_frequency, min_frequency, into_values | |
| Downsample for charting | lttb, asap_smooth | unnest |
TimescaleDB core (not the toolkit) provides time_bucket, time_bucket_gapfill, locf, interpolate, first, and last. Combine them freely with hyperfunctions.
Percentiles
- `percentile_agg(value)`: default choice. It is a
uddsketchwith sensible defaults and a bounded relative error. - `uddsketch(size, max_error, value)`, e.g.
uddsketch(200, 0.001, v): guaranteed relative error. Use when you need to tune accuracy or memory. - `tdigest(buckets, value)`, e.g.
tdigest(100, v): more accurate at extreme tails (p99.9). Error is not bounded, and results depend on input order. - Results are approximate. Use
PERCENTILE_CONTwhen exact values are required and the data is small.
Statistical Summaries
Accessors such as stddev and variance take an optional 'population' or 'sample' argument (default 'sample'). In 2D, stats_agg(y, x) puts the dependent variable first. Use average_x(), average_y(), and similar accessors for per-axis stats.
Time-Weighted Averages
AVG() overweights periods with dense sampling. time_weight weights each value by how long it was in effect.
- `'Linear'` interpolates between points. Use it for continuously varying signals such as temperature.
- `'LOCF'` (last observation carried forward) holds each value until the next one. Use it for set-points, prices, and state-like values.
average()returns NULL for a bucket with a single point, because there is no time span to weight. Useinterpolated_average(below), or a larger bucket, for sparse series.integral(unit)takes'microsecond','millisecond','second'(default),'minute', or'hour'.
Bucket edges. A plain average() only sees points inside the bucket. To account for values that span bucket boundaries, pass the neighbouring buckets' summaries:
Counters and Gauges
Counters only increase and reset to zero on restart (Prometheus counters, byte totals, request totals). counter_agg detects resets and corrects for them.
rate()is per second, measured between the first and last sample in the bucket.- For Prometheus-compatible extrapolation to the bucket edges, give the aggregate bounds:
counter_agg(ts, v, tstzrange(bucket, bucket + '5 min')) -> extrapolated_rate('prometheus'). You can also set bounds afterwards withwith_bounds(summary, range). - For edge-accurate deltas across buckets, use
interpolated_delta(bucket, interval, LAG(summary) OVER w, LEAD(summary) OVER w)orinterpolated_rate(...), following theinterpolated_averagepattern above.
Gauges
Gauges go up and down: memory in use, queue depth, temperature, connection count. gauge_agg(ts, value) gives counter-style change metrics but treats every drop as a real decrease, not a reset. Requires toolkit 1.25+.
- Accessors (stable in 1.25):
delta,rate,time_delta,idelta_left/idelta_right,irate_left/irate_right,slope,intercept,corr,num_changes,num_elements,zero_time(as-> zero_time(); the function form isgauge_zero_time(summary)), andextrapolated_delta/extrapolated_rate. The extrapolated accessors take no method argument and need bounds, fromgauge_agg(ts, v, tstzrange(...))orwith_bounds(summary, range). - Edge-accurate deltas across buckets: call
interpolated_deltaorinterpolated_ratein function form, with the summary first. The arrow form is not stable for gauges.
``sql SELECT bucket, queue, interpolated_delta(ga, bucket, INTERVAL '15 minutes', LAG(ga) OVER w, LEAD(ga) OVER w) AS net_change FROM (SELECT time_bucket('15 minutes', ts) AS bucket, queue, gauge_agg(ts, depth) AS ga FROM queue_depth GROUP BY 1, 2) t WINDOW w AS (PARTITION BY queue ORDER BY bucket); ``
- Not available on gauges:
num_resets, andfirst_val/last_val. Usetime_weightif you need first/last values or a time-weighted average of the gauge. - Choosing gauge or counter: if a value only ever falls on a restart, use
counter_agg. If falls are real, such as memory being freed, usegauge_agg.stats_aggortime_weightare often better for "average level".gauge_agganswers "how much and how fast did it change".
Heartbeats and Uptime
heartbeat_agg(ts, start, range, heartbeat_interval): each heartbeat at ts marks the system live until ts + heartbeat_interval. start and range define the window that the aggregate covers.
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/timescaledb-hyperfunctions/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/timescale/pg-aiguide/timescaledb-hyperfunctions)