name: timescaledb
Manage time-series data in PostgreSQL using TimescaleDB, extending PostgreSQL for high-performance time-series workloads.
Use this skill when you need to:
This skill is NOT for:
Install TimescaleDB Extension (Debian/Ubuntu):
sudo apt install -y postgresql-{{pg_version}}-timescaledb
sudo pg_createcluster {{pg_version}} main --start
sudo pg_ctlcluster {{pg_version}} main start
sudo -u postgres psql -c "CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;"
(Replace {{pg_version}} with your PostgreSQL version, e.g., 16)
Configuration (postgresql.conf):
# Add to postgresql.conf
shared_preload_libraries = 'timescaledb'
timescaledb.max_background_workers = 8 # Adjust based on CPU cores
max_connections = 100 # Adjust based on workload
(After changes, restart PostgreSQL: sudo systemctl restart postgresql)
Create Hypertables:
CREATE TABLE sensor_data (
time TIMESTAMPTZ NOT NULL,
device_id INT,
temperature DOUBLE PRECISION,
humidity DOUBLE PRECISION
);
SELECT create_hypertable('sensor_data', 'time');
Convert Existing Table to Hypertable:
SELECT create_hypertable('your_existing_table', 'time_column', migrate_data => true);
Show Hypertables:
\d+
SELECT * FROM timescaledb_information.hypertables;
Create Continuous Aggregate:
CREATE MATERIALIZED VIEW device_hourly_summary
WITH (timescaledb.continuous) AS
SELECT
time_bucket('1 hour', time) AS bucket,
device_id,
AVG(temperature) AS avg_temp,
MAX(temperature) AS max_temp
FROM sensor_data
GROUP BY time_bucket('1 hour', time), device_id
WITH NO DATA; -- Initially create without data
-- Refresh the continuous aggregate
CALL refresh_continuous_aggregate('device_hourly_summary', NULL, NULL);
Get Continuous Aggregates Info:
SELECT * FROM timescaledb_information.continuous_aggregates;
Set Data Retention Policy (Drop data older than 3 months):
SELECT add_retention_policy('sensor_data', INTERVAL '3 months');
Enable Compression (Compress data older than 7 days):
ALTER TABLE sensor_data SET (timescaledb.compress = TRUE);
SELECT add_compression_policy('sensor_data', INTERVAL '7 days');
Show Compression Status:
SELECT * FROM timescaledb_information.compression_settings;
Basic Time-Range Query:
SELECT * FROM sensor_data
WHERE time >= NOW() - INTERVAL '1 day'
AND time < NOW()
ORDER BY time DESC;
Gapfilling and Interpolation:
SELECT
time_bucket('1 hour', time) AS bucket,
AVG(temperature) AS avg_temp,
locf(AVG(temperature)) OVER (ORDER BY time_bucket('1 hour', time)) AS avg_temp_locf
FROM sensor_data
GROUP BY bucket
ORDER BY bucket;
Approximate Count:
SELECT COUNT(*) FROM sensor_data TABLESAMPLE BERNOULLI (1);
Top-N Queries:
SELECT time, device_id, temperature
FROM sensor_data
WHERE time >= NOW() - INTERVAL '1 day'
ORDER BY temperature DESC
LIMIT 10;
iot_readings.tick_data with proper chunk sizing for high ingest rate.tick_data.tick_data to calculate 5-minute VWAP (Volume Weighted Average Price) for a specific symbol.system_metrics partitioned by time and host_id.time_bucket_gapfill query to find CPU usage for all hosts over the last 24 hours, filling in missing data points.MAX(cpu_usage) exceeding a threshold using a continuous aggregate.references/installation.md: Detailed installation and setupreferences/hypertables.md: Deep dive into hypertable managementreferences/continuous_aggregates.md: Advanced continuous aggregate techniquesreferences/compression.md: Comprehensive guide to data compressionreferences/api.md: TimescaleDB SQL functions and commands referencereferences/performance.md: Performance tuning and best practicesreferences/getting_started.md: Official TimescaleDB Getting Started Guidereferences/llms.md: Using TimescaleDB with LLMs (e.g., storing embeddings, RAG)references/llms-full.md: Full LLM integration scenariosreferences/tutorials.md: Official TimescaleDB Tutorials and Use Casesreferences/time_buckets.md: Guide to time_bucket and gapfilling functionsreferences/hyperfunctions.md: Advanced analytical functions for time-seriestime and other frequently queried columns.EXPLAIN ANALYZE to identify bottlenecks.VACUUM ANALYZE on your tables.time column is TIMESTAMPTZ or a supported integer type.migrate_data => true.This skill provides a robust foundation for managing time-series data with TimescaleDB!