Gauge aggregation overview
Functions for analyzing gauge metrics that can increase or decrease
Analyze data coming from gauges. Unlike counters, gauges can decrease as well as increase.
If your value can only increase, use counter_agg instead to appropriately account for resets.
Two-step aggregation
Section titled “Two-step aggregation”This group of functions uses the two-step aggregation pattern.
Rather than calculating the final result in one step, you first create an intermediate aggregate by using the aggregate function.
Then, use any of the accessors on the intermediate aggregate to calculate a final result. You can also roll up multiple intermediate aggregates with the rollup functions.
The two-step aggregation pattern has several advantages:
- More efficient because multiple accessors can reuse the same aggregate
- Easier to reason about performance, because aggregation is separate from final computation
- Easier to understand when calculations can be rolled up into larger intervals, especially in window functions and continuous aggregates
- Perform retrospective analysis even when underlying data is dropped, because the intermediate aggregate stores extra information not available in the final result
To learn more, see the blog post on two-step aggregates.
Samples
Section titled “Samples”Analyze gauge metrics
Section titled “Analyze gauge metrics”Create hourly gauge aggregates and calculate changes:
SELECT time_bucket('1 hour'::interval, ts) AS hour, gauge_agg(ts, temperature) AS gauge_summaryFROM sensorsWHERE location = 'warehouse'GROUP BY hour;Calculate the delta and rate of change for gauge values:
WITH hourly AS ( SELECT time_bucket('1 hour'::interval, ts) AS hour, gauge_agg(ts, temperature) AS gauge_summary FROM sensors WHERE location = 'warehouse' GROUP BY hour)SELECT hour, delta(gauge_summary) AS temp_change, rate(gauge_summary) AS temp_change_rateFROM hourlyORDER BY hour;Available functions
Section titled “Available functions”Aggregate
Section titled “Aggregate”gauge_agg(): aggregate gauge data into an intermediate form for further analysis
Accessors
Section titled “Accessors”corr(): calculate the correlation coefficient from a gauge aggregatedelta(): calculate the change in a gauge’s valueextrapolated_delta(): estimate the total change in a gauge over a time periodextrapolated_rate(): estimate the average rate of change over a time periodgauge_zero_time(): calculate the time when a gauge value was zeroidelta_left(): calculate the instantaneous change at the left boundaryidelta_right(): calculate the instantaneous change at the right boundaryintercept(): calculate the y-intercept from a gauge aggregateinterpolated_delta(): calculate the change over a specific time range with interpolationinterpolated_rate(): calculate the rate of change over a specific time range with interpolationirate_left(): calculate the instantaneous rate at the left boundaryirate_right(): calculate the instantaneous rate at the right boundarynum_changes(): get the number of times the gauge changed valuenum_elements(): get the number of points in a gauge aggregaterate(): calculate the average rate of changeslope(): calculate the slope from a gauge aggregatetime_delta(): calculate the elapsed time in a gauge aggregate
Rollup
Section titled “Rollup”rollup(): combine multiple gauge aggregates
Mutator
Section titled “Mutator”with_bounds(): add time bounds to a gauge aggregate for extrapolation