Q: How do you design chunk intervals and automated compression policies for TimescaleDB hypertables?
TimescaleDB interview questions explained: pick a chunk interval, configure compress_segmentby and compress_orderby, and automate compression and retention.
Want to master this scenario in a live sandbox? KodeKloud's PostgreSQL Database Administration & High Availability Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: Most timescaledb interview questions on storage come down to two decisions: how big a chunk should be, and when and how to compress old chunks. I answer them in that order. A hypertable is split into chunks by time, and every write, index build and compression job operates on one chunk at a time. If chunks are too small you pay planning overhead across thousands of tables. If they are too large, the active chunk and its indexes stop fitting in memory and ingest slows down. Below is the runbook I follow on a metrics table receiving a few thousand rows per second, using current TimescaleDB 2.x functions.
Create the Hypertable with an Explicit chunk_time_interval
The common guidance is to size chunks so the indexes of the most recent chunks fit comfortably in memory, leaving headroom for the rest of the workload. Treat that as a starting rule, not a measured guarantee: you validate it against your own ingest rate and shared_buffers. I start with one day for a mid-volume table and move to a few hours when a single day of data and indexes gets large. The interval only applies to chunks created after you change it, so adjusting it later is cheap and does not rewrite history.
CREATE TABLE metrics (
time timestamptz NOT NULL,
device_id bigint NOT NULL,
cpu double precision,
mem double precision
);
SELECT create_hypertable('metrics', 'time',
chunk_time_interval => INTERVAL '1 day');
CREATE INDEX ON metrics (device_id, time DESC);
-- Later, if chunks are too big: affects only NEW chunks
SELECT set_chunk_time_interval('metrics', INTERVAL '6 hours');
Inspect Chunks and Measure Real Sizes
Before tuning anything I look at what the table actually produced. timescaledb_information.chunks lists each chunk with its time range and compression state, and chunks_detailed_size shows table, index and toast bytes per chunk. If the newest chunk's index bytes are a large fraction of available memory, the interval is too long for that host. If you see hundreds of tiny chunks for a single day, it is too short.
SELECT chunk_name, range_start, range_end, is_compressed
FROM timescaledb_information.chunks
WHERE hypertable_name = 'metrics'
ORDER BY range_start DESC LIMIT 3;
-- chunk_name | range_start | range_end | is_compressed
-- _hyper_1_42_chunk | 2026-11-29 00:00:00 | 2026-11-30 00:00:00 | f
SELECT chunk_name,
pg_size_pretty(table_bytes) AS tbl,
pg_size_pretty(index_bytes) AS idx
FROM chunks_detailed_size('metrics')
ORDER BY chunk_name DESC LIMIT 3;
Enable Compression with segmentby and orderby, Then Add a Policy
Compression converts rows into column-oriented arrays. compress_segmentby picks the column whose values stay separate, so queries that filter on device_id only decompress matching segments. compress_orderby sets the sort inside each segment, normally time DESC. I choose a segmentby column that appears in most WHERE clauses and has enough rows per chunk to form meaningful batches. add_compression_policy then runs as a background job and compresses chunks older than the interval you give it. Keep that interval longer than your typical late-arriving data window, because inserting into compressed chunks costs more.
ALTER TABLE metrics SET (
timescaledb.compress,
timescaledb.compress_segmentby = 'device_id',
timescaledb.compress_orderby = 'time DESC'
);
SELECT add_compression_policy('metrics', INTERVAL '7 days');
-- returns the job id, e.g. 1000
-- Compress one chunk by hand to test
SELECT compress_chunk(c) FROM show_chunks('metrics', older_than => INTERVAL '7 days') c LIMIT 1;
Verify Compression Results and Job Health
A policy that silently fails is worse than none, so I check both the savings and the job. chunk_compression_stats reports bytes before and after for each compressed chunk. timescaledb_information.job_stats shows the last run status and the next scheduled start. If last_run_status is Failed, I read the PostgreSQL log for that job id before touching the policy.
SELECT chunk_name,
pg_size_pretty(before_compression_total_bytes) AS before,
pg_size_pretty(after_compression_total_bytes) AS after
FROM chunk_compression_stats('metrics')
ORDER BY chunk_name LIMIT 3;
SELECT job_id, last_run_status, last_successful_finish, next_start
FROM timescaledb_information.job_stats
WHERE hypertable_name = 'metrics';
-- job_id | last_run_status | last_successful_finish | next_start
-- 1000 | Success | 2026-11-29 02:00:11+00 | 2026-11-30 02:00:00+00
Add a Retention Policy to Drop Expired Chunks
Retention is where hypertables pay off. add_retention_policy drops whole chunks that fall entirely outside the window, which is a metadata operation and avoids the bloat and vacuum cost of a large DELETE. I set retention longer than the compression interval so chunks are compressed first and dropped later. Always confirm the interval against your data retention requirements before enabling it, because dropped chunks are gone unless you have backups.
SELECT add_retention_policy('metrics', INTERVAL '90 days');
SELECT job_id, proc_name, schedule_interval, config
FROM timescaledb_information.jobs
WHERE hypertable_name = 'metrics';
-- Remove it if requirements change
SELECT remove_retention_policy('metrics');
- I create the hypertable with a chunk interval sized so the newest chunks and their indexes fit in memory, and I validate that with chunks_detailed_size.
- I enable compression with segmentby on the column queries filter by and orderby on time DESC, then add a compression policy older than the late-data window.
- I confirm savings with chunk_compression_stats and watch job_stats so a failing policy does not go unnoticed.
- I finish with add_retention_policy so expired data leaves as dropped chunks rather than slow deletes.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Choosing a huge chunk interval to reduce chunk count The active chunk and its indexes outgrow memory, so inserts and index maintenance slow down.
- Picking a high-cardinality or unused column for compress_segmentby Segments end up with too few rows to compress well, and queries that filter on other columns decompress more data than needed.
- Compressing chunks that still receive late data Writes into compressed chunks are costlier, so keep the policy interval longer than your late-arrival window.
- Using DELETE FROM for old data instead of a retention policy Row deletes create dead tuples and vacuum load, while dropping a chunk is a metadata operation.