Skip to content

slope()

Calculate the slope from a counter aggregate

Since 1.3.0

Calculate the slope of the linear least-squares fit for a counter aggregate. The dependent variable is the counter value, adjusted for resets, and the independent variable is time. Time is always in seconds, so the slope estimates the per-second rate of change. This gives a result similar to rate, but it can more accurately reflect the usual counter behavior in the presence of infrequent, abnormally large changes.

Calculate the counter slope per id and per 15-minute interval.

SELECT
id,
bucket,
slope(summary)
FROM (
SELECT
id,
time_bucket('15 min'::interval, ts) AS bucket,
counter_agg(ts, val) AS summary
FROM foo
GROUP BY id, time_bucket('15 min'::interval, ts)
) t

The syntax is:

slope(
summary CounterSummary
) RETURNS DOUBLE PRECISION
NameTypeDefaultRequiredDescription
summaryCounterSummary-A counter aggregate created using counter_agg
ColumnTypeDescription
slopeDOUBLE PRECISIONThe slope of the linear least-squares fit