time_bucket_gapfill()

The time_bucket_gapfill function works similar to time_bucket but also activates gap filling for the interval between start and finish. It can only be used with an aggregation query. Values outside of start and finish pass through but no gap filling is done outside of the specified range.

important

The time_bucket_gapfill function must be a top-level expression in a query or subquery, as shown in these examples. You cannot, for example, do something like round(time_bucket_gapfill(...)) or cast the result of the gapfill call. The only exception is if you use it as a subquery, where the outer query does the type cast.

For more information about gapfilling and interpolation functions, see the hyperfunctions documentation.

Required arguments

NameTypeDescription
bucket_widthINTERVALA PostgreSQL time interval for how long each bucket is
timeTIMESTAMPThe timestamp to bucket

Optional arguments

NameTypeDescription
startTIMESTAMPThe start of the gapfill period
finishTIMESTAMPThe end of the gapfill period

In TimescaleDB 1.3 and later, start and finish are optional arguments. If they are not supplied, the parameters are inferred from the WHERE clause. We recommend using a WHERE clause if possible, instead of start and finish arguments. This is because start and finish arguments do not filter input rows. If you do not provide a WHERE clause, TimescaleDB’s planner selects all data, and does not perform constraint exclusion to exclude chunks from further processing, which is less performant.

Values explicitly provided in start and stop arguments, or values derived from WHERE clause values, must be simple expressions. They should be evaluated to constants at query planning. For example, simple expressions can contain constants or call to now(), but cannot reference columns of a table.

For integer time inputs

Required arguments

NameTypeDescription
bucket_widthINTEGERinteger interval for how long each bucket is
timeINTEGERThe timestamp to bucket

Optional arguments

NameTypeDescription
startINTEGERThe start of the gapfill period
finishINTEGERThe end of the gapfill period

In TimescaleDB 1.3 and later, start and finish are optional arguments. If they are not supplied, the parameters are inferred from the WHERE clause. We recommend using a WHERE clause if possible, instead of start and finish arguments. This is because start and finish arguments do not filter input rows. If you do not provide a WHERE clause, TimescaleDB’s planner selects all data, and does not perform constraint exclusion to exclude chunks from further processing, which is less performant.

Sample usage

Get the metric value every day over the last seven days:

  1. SELECT
  2. time_bucket_gapfill('1 day', time) AS day,
  3. device_id,
  4. avg(value) AS value
  5. FROM metrics
  6. WHERE time > now() - INTERVAL '1 week' AND time < now()
  7. GROUP BY day, device_id
  8. ORDER BY day;
  9. day | device_id | value
  10. ------------------------+-----------+-------
  11. 2019-01-10 01:00:00+01 | 1 |
  12. 2019-01-11 01:00:00+01 | 1 | 5.0
  13. 2019-01-12 01:00:00+01 | 1 |
  14. 2019-01-13 01:00:00+01 | 1 | 7.0
  15. 2019-01-14 01:00:00+01 | 1 |
  16. 2019-01-15 01:00:00+01 | 1 | 8.0
  17. 2019-01-16 01:00:00+01 | 1 | 9.0
  18. (7 row)

Get the metric value every day over the last seven days, carrying forward the previous seen value if none is available in an interval:

  1. SELECT
  2. time_bucket_gapfill('1 day', time) AS day,
  3. device_id,
  4. avg(value) AS value,
  5. locf(avg(value))
  6. FROM metrics
  7. WHERE time > now() - INTERVAL '1 week' AND time < now()
  8. GROUP BY day, device_id
  9. ORDER BY day;
  10. day | device_id | value | locf
  11. ------------------------+-----------+-------+------
  12. 2019-01-10 01:00:00+01 | 1 | |
  13. 2019-01-11 01:00:00+01 | 1 | 5.0 | 5.0
  14. 2019-01-12 01:00:00+01 | 1 | | 5.0
  15. 2019-01-13 01:00:00+01 | 1 | 7.0 | 7.0
  16. 2019-01-14 01:00:00+01 | 1 | | 7.0
  17. 2019-01-15 01:00:00+01 | 1 | 8.0 | 8.0
  18. 2019-01-16 01:00:00+01 | 1 | 9.0 | 9.0

Get the metric value every day over the last seven days, interpolating missing values:

  1. SELECT
  2. time_bucket_gapfill('1 day', time) AS day,
  3. device_id,
  4. avg(value) AS value,
  5. interpolate(avg(value))
  6. FROM metrics
  7. WHERE time > now() - INTERVAL '1 week' AND time < now()
  8. GROUP BY day, device_id
  9. ORDER BY day;
  10. day | device_id | value | interpolate
  11. ------------------------+-----------+-------+-------------
  12. 2019-01-10 01:00:00+01 | 1 | |
  13. 2019-01-11 01:00:00+01 | 1 | 5.0 | 5.0
  14. 2019-01-12 01:00:00+01 | 1 | | 6.0
  15. 2019-01-13 01:00:00+01 | 1 | 7.0 | 7.0
  16. 2019-01-14 01:00:00+01 | 1 | | 7.5
  17. 2019-01-15 01:00:00+01 | 1 | 8.0 | 8.0
  18. 2019-01-16 01:00:00+01 | 1 | 9.0 | 9.0