Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

Sensor readings land in a TimescaleDB hypertable created with a 1-hour chunk interval and kept for 2 years. Simple dashboard queries over the last 30 days now take 2 seconds, and EXPLAIN shows most of the time in planning, not execution.

Explain why chunk sizing caused this and how you would fix it on a live system.

Two years of 1-hour chunks is over 17,000 chunks, each a child table with its own indexes, so planning a query and excluding chunks means examining thousands of relations, and catalog and lock overhead grow with them. Chunks should be sized so the ones being written, with their indexes, fit in about a quarter of memory; the default is 7 days, and 1 day or 1 week suits this load. On a live system we change future chunks with set_chunk_time_interval, let old chunks age out or rewrite them in background batches, add compression, retention and continuous aggregates, and make sure queries filter on the time column in a way the planner can use.

Why this question is asked

TimescaleDB makes PostgreSQL good at time-series by splitting one big table into many small ones, and that is exactly how it can go wrong: too many small ones. The overview gives a precise symptom, planning time dominating execution time, which points straight at the number of relations involved. Interviewers ask this to see whether we know how hypertables work and can fix a sizing mistake on a live system without downtime. In this answer, we will recap hypertables and chunk exclusion, count what 1-hour chunks cost, choose a better interval, change it safely, add compression, retention and continuous aggregates, and check the queries themselves, ending with sizing for uneven write rates.

1A Quick Recap of Hypertables and Chunks

A hypertable looks like one table to the application, but TimescaleDB stores it as many chunks, each a regular PostgreSQL child table holding rows for one time interval, with its own copy of every index. When we insert, the row goes to the chunk for its timestamp. When we query with a time condition, the planner excludes chunks whose range cannot match, called chunk exclusion, and scans only the rest.

This keeps recent chunks and their indexes small enough to stay in memory, which keeps inserts fast, and lets retention drop whole chunks instead of deleting rows. But every chunk is a relation the planner has to know about.

2What 1-Hour Chunks Cost Over Two Years

Two years is about 17,520 hours, so the hypertable has over 17,000 chunks, and with three indexes per chunk, more than 70,000 relations. When a dashboard query arrives, the planner must look at the hypertable's chunks to decide which ones overlap the last 30 days, then plan scans over the 720 that do, each with its own index choices. Opening, locking and planning hundreds of relations takes time that has nothing to do with the data, and the catalog, the lock table and memory per backend all grow with the chunk count. So a query that executes in 200 ms can spend 2 seconds in planning, exactly what EXPLAIN shows.

The interval was probably chosen to keep chunks small, which is the right instinct for inserts, taken much too far.

SQL
EXPLAIN (ANALYZE, SUMMARY) SELECT time_bucket('1 hour', ts), avg(value)
FROM readings WHERE ts > now() - interval '30 days' GROUP BY 1;
-- Planning Time: 1984.3 ms
-- Execution Time: 212.6 ms
SELECT count(*) FROM timescaledb_information.chunks
WHERE hypertable_name = 'readings'; -- 17520
Chunk interval over two years of data
IntervalChunksChunks for a 30-day query
1 hourabout 17,500720
1 dayabout 73030 or 31
7 days (default)about 1055

3Choosing a Better Interval

The guidance from TimescaleDB is to size chunks so that the chunks currently being written, including their indexes, fit in about 25 percent of the memory available to the database. If the sensors write 2 GB of data and indexes per day and the server has 64 GB, a 1-day chunk is comfortably inside that, and even a 7-day chunk at 14 GB fits within the guidance. The default interval is 7 days. For this load, 1 day is a sensible choice: it keeps recent chunks small and hot, gives about 730 chunks over two years, and lets compression and retention work at a daily granularity.

4Changing It on a Live System

The interval is a property for new chunks, so changing it is instant and safe. set_chunk_time_interval affects only chunks created after the call, and existing 1-hour chunks keep their size. That means the problem shrinks gradually: as the retention policy drops chunks older than two years, the 1-hour chunks disappear over time.

If we cannot wait two years, we rewrite old data. A common approach is to create a new hypertable with the 1-day interval, copy history into it in background batches, one day at a time and throttled, while new writes go to both or a view unions them, then switch names in a short transaction. Recent TimescaleDB versions also add functions to merge chunks; check your version before relying on one.

SQL
SELECT set_chunk_time_interval('readings', INTERVAL '1 day'); -- applies to new chunks only
-- compress chunks older than 7 days, drop data older than 2 years
ALTER TABLE readings SET (timescaledb.compress, timescaledb.compress_segmentby = 'sensor_id');
SELECT add_compression_policy('readings', INTERVAL '7 days');
SELECT add_retention_policy('readings', INTERVAL '2 years');
-- newer versions call compression 'columnstore' (add_columnstore_policy); check your version

5Compression, Retention and Continuous Aggregates

Compression turns older chunks into a columnar format, often 90 percent smaller, segmented by sensor id so per-sensor queries stay fast. A retention policy drops chunks past two years automatically. And dashboards that cover 30 days should not read raw readings at all: a continuous aggregate, a materialised view that TimescaleDB keeps up to date incrementally, can store hourly averages, minimums, maximums and counts per sensor. A 30-day chart then reads 720 rows per sensor from the aggregate instead of millions of raw rows, and planning touches only the aggregate's own few chunks.

SQL
CREATE MATERIALIZED VIEW readings_hourly WITH (timescaledb.continuous) AS
SELECT sensor_id, time_bucket('1 hour', ts) AS bucket,
avg(value) AS avg_v, min(value) AS min_v, max(value) AS max_v, count(*) AS n
FROM readings GROUP BY sensor_id, bucket;
SELECT add_continuous_aggregate_policy('readings_hourly',
start_offset => INTERVAL '3 days', end_offset => INTERVAL '1 hour',
schedule_interval => INTERVAL '30 minutes');

6Making Sure the Planner Can Exclude Chunks

Finally, we check the queries themselves. Chunk exclusion works when the time filter is on the hypertable's time column and compares it to a constant, a parameter, or an expression such as now() minus an interval that TimescaleDB can evaluate. A filter wrapped in a function on the column, such as date(ts) = current_date, or a time range that comes from a join, can prevent exclusion, so every chunk is planned or scanned. EXPLAIN shows how many chunks appear in the plan, which is the quickest way to confirm.

7Follow-up questions

How would you size chunks when write rates differ tenfold between day and night?

Size for the peak, not the average, because the chunk being written during the busiest hours must fit in memory with its indexes. A daily chunk contains both peak and quiet hours, so its total size is what matters for the memory guidance, and it stays the same each day. If the peak is so high that even a day is too large, a smaller interval such as 12 hours is fine; the aim is a few hundred to a few thousand chunks over the retention period, not tens of thousands.

Why does the planner spend so much time even when execution excludes chunks?

Some exclusion happens at planning time and some at execution time. Either way, each candidate chunk is a relation with metadata to load and locks to take, so thousands of chunks cost planning time even if most are pruned later.

Would PostgreSQL native partitioning have the same problem?

Yes. Thousands of partitions slow planning in plain PostgreSQL as well, although recent versions improved pruning. The rule of keeping partitions to a reasonable count applies to both.

8Conclusion

In this answer, we saw that 1-hour chunks over two years create more than 17,000 chunks, each a table with indexes, and planning a 30-day query over 720 of them explains the 2 seconds of planning time. We chose a 1-day interval based on the guidance that active chunks with indexes should fit in about 25 percent of memory, changed it live with set_chunk_time_interval for new chunks, let old chunks age out or rewrote them in batches, and added compression, retention and continuous aggregates. We also checked that queries filter on the time column in a way the planner can use.

What to say in the interviewAbout 80 seconds aloud

Open with

Each chunk of a hypertable is a real PostgreSQL table with its own indexes, and with a one-hour interval over two years we have about 17,500 chunks, so a 30-day dashboard query has to plan over 720 chunks and the planner has to deal with thousands of relations, locks and catalog entries.

Then, if they want more

That is why EXPLAIN shows two seconds of planning and a fraction of that in execution. The guidance is to size chunks so that the chunks being written, with indexes, fit in about a quarter of memory; the default is seven days, and for this load I would use one day, which gives about 730 chunks over two years.

On the live system I would call set_chunk_time_interval, which only affects new chunks, so the old hourly chunks age out through retention, or if we cannot wait, I would copy history into a new hypertable in throttled background batches and swap.

I would also add compression for chunks older than a week, a two-year retention policy, and a continuous aggregate with hourly averages so the 30-day dashboards read a small pre-aggregated table. And I would check that queries filter directly on the time column, so chunk exclusion can work.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain why chunk sizing caused this and how you would fix it on a live system.": https://scrivio-interview-library.netlify.app/architecture/storage-and-pipelines/38125313a6/