interpolated_rate()

Calculate the rate of change in a counter over a time period. Data points at the exact boundaries of the time period aren’t needed. The function linerally interpolates the counter values at the boundaries from adjacent CounterSummaries if they are unknown.

This is the same value as an interpolated_delta divided by the duration in seconds.

  1. interpolated_rate(
  2. summary CounterSummary,
  3. start TIMESTAMPTZ,
  4. interval INTERVAL,
  5. prev CounterSummary,
  6. next CounterSummary
  7. ) RETURNS DOUBLE PRECISION

For more information about counter aggregation functions, see the hyperfunctions documentation.

Required arguments

NameTypeDescription
summaryCounterSummaryThe input CounterSummary from a counter_agg call
startTIMESTAMPTZThe start of the interval which the rate should be computed over (if there is a preceeding point)
intervalINTERVALThe length of the interval which the rate should cover

Optional arguments

NameTypeDescription
prevCounterSummaryThe CounterSummary from the prior interval, used to interpolate the value at start. If NULL, the first timestamp in summary is used as the start of the interval.
nextCounterSummaryThe CounterSummary from the following interval, used to interpolate the value at start + interval. If NULL, the last timestamp in summary will be used as the end of the interval.

Returns

NameTypeDescription
interpolated_rateDOUBLE PRECISIONThe per-second rate of change of the counter between the specified bounds. If the raw data contains no points calculated at those bounds, the bounding values are linearly interpolated from neighboring CounterSummaries.

Sample usage

  1. SELECT
  2. id,
  3. bucket,
  4. interpolated_rate(
  5. summary,
  6. bucket,
  7. '15 min',
  8. LAG(summary) OVER (PARTITION BY id ORDER by bucket),
  9. LEAD(summary) OVER (PARTITION BY id ORDER by bucket)
  10. )
  11. FROM (
  12. SELECT
  13. id,
  14. time_bucket('15 min'::interval, ts) AS bucket,
  15. counter_agg(ts, val) AS summary
  16. FROM foo
  17. GROUP BY id, time_bucket('15 min'::interval, ts)
  18. ) t