← Back to the chapter · Next detail →

Technical detail · Time-Series Query Optimization

Read a captured aggregation plan

This query uses TimescaleDB’s time_bucket function. ANALYZE in EXPLAIN (ANALYZE, BUFFERS) executes the statement to collect runtime evidence. This capture used PostgreSQL 16.16 and TimescaleDB 2.29.2 with 4,320 synthetic rows and an index on site_id. The hypertable has three one-day UTC chunks beginning May 22, May 23, and May 24, 2026. The fixture inserts one room-temperature row per minute from May 22 00:00 UTC. Number the minutes from zero. Every 30th minute belongs to building-1, and the rest belong to building-2. The first hour of each two-hour cycle is assigned to sensor-a and the second to sensor-b. In each hour, minute 30 is invalid and all other minutes are valid. The value is 20 plus the remainder when the minute number is divided by four. Table statistics were collected after loading the fixture.

GroupAggregate  (cost=18.03..19.55 rows=47 width=121) (actual time=0.124..0.146 rows=24 loops=1)
  Group Key: (time_bucket('00:15:00'::interval, _hyper_1_2_chunk.observed_at)), _hyper_1_2_chunk.device_id
  Buffers: shared hit=23
  ->  Sort  (cost=18.03..18.14 rows=47 width=22) (actual time=0.106..0.107 rows=24 loops=1)
        Sort Key: (time_bucket('00:15:00'::interval, _hyper_1_2_chunk.observed_at)), _hyper_1_2_chunk.device_id
        Sort Method: quicksort  Memory: 26kB
        Buffers: shared hit=23
        ->  Result  (cost=0.28..16.72 rows=47 width=22) (actual time=0.023..0.073 rows=24 loops=1)
              Buffers: shared hit=17
              ->  Index Scan using _hyper_1_2_chunk_readings_site_idx on _hyper_1_2_chunk  (cost=0.28..16.13 rows=47 width=22) (actual time=0.020..0.068 rows=24 loops=1)
                    Index Cond: (site_id = 'building-1'::text)
                    Filter: ((metric_name = 'room_temperature'::text) AND (quality_status = 'valid'::text))
                    Rows Removed by Filter: 24
                    Buffers: shared hit=17
Planning:
  Buffers: shared hit=816 read=3
Planning Time: 3.068 ms
Execution Time: 0.275 ms

Read the plan from the innermost scan outward:

  1. The chunk inventory shows _hyper_1_1_chunk, _hyper_1_2_chunk, and _hyper_1_3_chunk for May 22, 23, and 24 respectively. Only the May 23 chunk appears under the scan for the query’s time window. Comparing the two records shows that the other days were pruned.
  2. Index Cond selects building-1 through the site index. The residual Filter checks metric and quality after index access. It removes 24 rows in this run. The scan estimated 47 returned rows and actually returned 24. The actual row number is an average per loop, so with one loop the total is 24. When a node loops, multiply the average by the loop count for an approximate total.
  3. The scan reports 17 shared-buffer hits. These are accesses satisfied by buffers already in memory. The top node reports 23 shared hits including child work, so do not add its count to the scan’s. The planning section reports its own buffer activity.
  4. The sort puts the 24 scan results in bucket and device order. GroupAggregate reads them in that order to compute the requested summaries. The run returns 24 groups because the fixture has one valid building-1 reading per hour, each in a different 15-minute bucket.

The timings and buffer counts belong to this capture. Use the plan and workload you measure when judging another deployment.

← Back to the chapter · Next detail →