Skip to content

time_bucket_gapfill()

Bucket rows by time interval while filling gaps in data

Since 1.2.0

Group data into buckets based on time interval, while filling in gaps of missing data. If you don’t provide a gapfilling algorithm, such as locf or interpolate, gaps are left as NULL in the returned data.

  1. Create a hypertable

    CREATE TABLE sensor_data (
    time TIMESTAMPTZ NOT NULL,
    sensor_id INTEGER,
    temperature DOUBLE PRECISION
    ) WITH (tsdb.hypertable);
  2. Fill missing hourly sensor data with gapfilling and interpolation

    SELECT
    time_bucket_gapfill('1 hour', time) AS hour,
    sensor_id,
    AVG(temperature) AS avg_temp,
    interpolate(AVG(temperature)) AS interpolated_temp,
    locf(AVG(temperature)) AS locf_temp
    FROM sensor_data
    WHERE time >= NOW() - INTERVAL '1 day'
    AND time < NOW() -- Must have upper bound for gapfill to work
    AND sensor_id IN (1, 2, 3)
    GROUP BY hour, sensor_id
    ORDER BY hour;

    Returns something like:

    hoursensor_idavg_tempinterpolated_templocf_temp
    2025-12-10 09:00:00+00123.5418378847098323.5418378847098323.54183788470983
    2025-12-10 09:00:00+00323.50142810525842723.50142810525842723.501428105258427
    2025-12-10 09:00:00+00223.56157455613824723.56157455613824723.561574556138247
    2025-12-10 10:00:00+00122.5128000401948322.5128000401948322.51280004019483
    2025-12-10 10:00:00+00222.4959338997513322.4959338997513322.49593389975133
    2025-12-10 10:00:00+00322.51233929427958522.51233929427958522.512339294279585
    2025-12-10 11:00:00+00121.25875353711134621.25875353711134621.258753537111346
    2025-12-10 11:00:00+00221.32057319875898221.32057319875898221.320573198758982
    2025-12-10 11:00:00+00321.2939527311615521.2939527311615521.29395273116155
    2025-12-10 12:00:00+00120.0172260362809720.0172260362809720.01722603628097
    2025-12-10 12:00:00+00219.98515355597492519.98515355597492519.985153555974925
    2025-12-10 12:00:00+00320.00254151015822820.00254151015822820.002541510158228

The syntax is:

SELECT time_bucket_gapfill(
bucket_width = <interval>,
ts = <timestamp>,
start = <timestamp>,
finish = <timestamp>,
timezone = '<timezone>'
) AS bucket
FROM <table_name>
WHERE <timestamp> >= <start>
AND <timestamp> < <finish>
GROUP BY bucket;
NameTypeDefaultRequiredDescription
bucket_widthINTERVALINTEGER-
tsTIMESTAMPTZTIMESTAMPDATESMALLINT
timezoneTEXT--The timezone to use for bucketing. For example, Europe/Berlin. Available in TimescaleDB 2.9 or later. Does not work for integer-based time. If you have an untyped start or finish argument and a timezone argument, you might run into a problem where you are not passing your arguments for the parameter that you expect. To solve this, either name your arguments or explicitly type cast them.
startTIMESTAMPTZINTEGER--
finishTIMESTAMPTZINTEGER--
ColumnTypeDescription
time_bucket_gapfillTIMESTAMPTZ or INTEGERThe start time of the time bucket. The return type matches the input time type.