1---2name: timescaledb3description: Store and query time-series data with hypertables, compression, and continuous aggregates.4---5
6## Hypertables
7
8- Convert table to hypertable: `SELECT create_hypertable('metrics', 'time')`
9- Must have time column (TIMESTAMPTZ recommended)—partition key for chunks
10- Call BEFORE inserting data—converting large tables is expensive
11- Can't undo easily—plan schema before converting
12
13## Chunk Interval
14
15- Default 7 days per chunk—tune based on data volume
16- `SELECT set_chunk_time_interval('metrics', INTERVAL '1 day')` for high-volume
17- Chunks should be 25% of memory—too small = overhead, too large = slow queries
18- Check chunk sizes: `SELECT * FROM chunks_detailed_size('metrics')`
19
20## time_bucket
21
22- `time_bucket('1 hour', time)` groups timestamps—like date_trunc but with arbitrary intervals
23- Use in GROUP BY for aggregation: `GROUP BY time_bucket('5 minutes', time)`
24- Origin parameter for offset: `time_bucket('1 day', time, '2024-01-01'::timestamptz)`
25- Beats date_trunc for non-standard intervals—15min, 4h, etc.
26
27## Continuous Aggregates
28
29- Materialized views that auto-refresh—pre-compute expensive aggregations
30- `CREATE MATERIALIZED VIEW hourly_stats WITH (timescaledb.continuous) AS SELECT ...`
31- Add refresh policy: `SELECT add_continuous_aggregate_policy('hourly_stats', ...)`
32- Query aggregate view instead of raw hypertable—orders of magnitude faster
33
34## Real-Time Aggregates
35
36- Continuous aggregates include recent data automatically—no stale reads
37- `WITH (timescaledb.continuous, timescaledb.materialized_only = false)` for real-time
38- Combines materialized historical + live recent—transparent to queries
39- Small performance cost for real-time—disable if batch-only acceptable
40
41## Compression
42
43- Compress old chunks to save 90%+ storage: `ALTER TABLE metrics SET (timescaledb.compress)`
44- Add compression policy: `SELECT add_compression_policy('metrics', INTERVAL '7 days')`
45- Compressed chunks are read-only—can't update/delete individual rows
46- Decompress for modifications: `SELECT decompress_chunk('chunk_name')`
47
48## Retention
49
50- Auto-delete old data: `SELECT add_retention_policy('metrics', INTERVAL '90 days')`
51- Drops entire chunks—efficient, no row-by-row delete
52- Retention runs on scheduler—data persists slightly past interval
53- Combine with compression: compress at 7d, drop at 90d
54
55## Indexing
56
57- Time column auto-indexed in hypertable—don't add redundant index
58- Add indexes on filter columns: `CREATE INDEX ON metrics (device_id, time DESC)`
59- Composite indexes with time last—enables chunk exclusion
60- Skip indexes on rarely-filtered columns—each index slows writes
61
62## Insert Performance
63
64- Batch inserts critical—single-row inserts are slow
65- Use COPY or multi-value INSERT: `INSERT INTO metrics VALUES (...), (...), ...`
66- Parallel COPY with `timescaledb-parallel-copy` tool—saturates I/O
67- Out-of-order inserts work but slower—prefer time-ordered writes
68
69## Query Patterns
70
71- Always include time range in WHERE—enables chunk exclusion
72- `WHERE time > now() - INTERVAL '1 day'` skips old chunks entirely
73- ORDER BY time DESC with LIMIT for "latest N"—index scan, fast
74- Avoid SELECT * on wide tables—fetch only needed columns
75
76## Distributed Hypertables
77
78- Multi-node for horizontal scale—data sharded across nodes
79- Create access node + data nodes—access node coordinates queries
80- More operational complexity—start single-node, distribute when needed
81- Not needed for most workloads—single node handles millions of rows/sec