ESC
Type to search guides, tutorials, and reference documentation.
Verified by Garnet Grid

Time-Series Databases

Choose and optimize time-series databases for metrics, IoT, and financial data. Covers data models, compression, retention policies, downsampling, query optimization, and the patterns that handle billions of data points efficiently.

Time-series databases are optimized for data that arrives in chronological order — metrics, sensor readings, financial ticks, log entries. While you can store time-series data in PostgreSQL, a dedicated TSDB handles billions of data points with:compression ratios of 10-20x, query speeds 100x faster, and automated data lifecycle management.


Why Time-Series Databases

Regular database characteristics:
  - Optimized for random reads/writes
  - Row-level updates common
  - Indexes on arbitrary columns
  
Time-series characteristics:
  - Write-heavy (high ingestion rate)
  - Reads are time-range scans
  - Updates are rare (append-only)
  - Recent data queried more than old data
  - Data has natural expiration (retention)

TSDB optimizations:
  ✓ Columnar storage (compress similar values)
  ✓ Time-based partitioning (efficient range queries)
  ✓ Automatic downsampling (1-second → 1-minute → 1-hour)
  ✓ Built-in retention policies (auto-delete old data)
  ✓ Specialized query language for time operations

TSDB Comparison

DatabaseTypeBest ForScale
TimescaleDBPostgreSQL extensionSQL familiarity, relational + time-seriesModerate (TB)
InfluxDBPurpose-builtDevOps metrics, IoTHigh (PB)
PrometheusPull-based metricsKubernetes monitoringModerate
ClickHouseColumn-oriented analyticalAnalytics + time-seriesVery high (PB)
QuestDBPurpose-built (SQL)Low-latency financial dataHigh
VictoriaMetricsPrometheus-compatibleLong-term Prometheus storageVery high (PB)

Data Model

-- TimescaleDB: Create hypertable (auto-partitioned by time)
CREATE TABLE metrics (
    time        TIMESTAMPTZ NOT NULL,
    host        TEXT NOT NULL,
    metric_name TEXT NOT NULL,
    value       DOUBLE PRECISION,
    tags        JSONB
);

SELECT create_hypertable('metrics', 'time');

-- Compression policy: Compress chunks older than 7 days
ALTER TABLE metrics SET (
    timescaledb.compress,
    timescaledb.compress_segmentby = 'host,metric_name'
);

SELECT add_compression_policy('metrics', INTERVAL '7 days');

-- Retention policy: Drop data older than 90 days
SELECT add_retention_policy('metrics', INTERVAL '90 days');

-- Continuous aggregate (materialized rollup)
CREATE MATERIALIZED VIEW metrics_hourly
WITH (timescaledb.continuous) AS
SELECT
    time_bucket('1 hour', time) AS bucket,
    host,
    metric_name,
    AVG(value) AS avg_value,
    MAX(value) AS max_value,
    MIN(value) AS min_value,
    COUNT(*) AS sample_count
FROM metrics
GROUP BY bucket, host, metric_name;

-- Auto-refresh every hour
SELECT add_continuous_aggregate_policy('metrics_hourly',
    start_offset => INTERVAL '3 hours',
    end_offset   => INTERVAL '1 hour',
    schedule_interval => INTERVAL '1 hour'
);

Anti-Patterns

Anti-PatternConsequenceFix
PostgreSQL for high-cardinality metricsSlow queries, massive storagePurpose-built TSDB or TimescaleDB
No retention policyStorage grows foreverAutomated retention + tiered storage
No downsamplingQuerying years of 1-second data = slowContinuous aggregates at multiple granularities
String tags with high cardinalityIndex explosion, slow writesBounded tag values, tag caching
No pre-aggregationDashboard queries scan raw dataMaterialized views for common queries

Time-series databases trade flexibility for performance. They are not general-purpose databases — they are specialized engines for a specific data pattern. Use the right tool for the right job.

Jakub Dimitri Rezayev
Jakub Dimitri Rezayev
Founder & Chief Architect • Garnet Grid Consulting

Jakub holds an M.S. in Customer Intelligence & Analytics and a B.S. in Finance & Computer Science from Pace University. With deep expertise spanning D365 F&O, Azure, Power BI, and AI/ML systems, he architects enterprise solutions that bridge legacy systems and modern technology — and has led multi-million dollar ERP implementations for Fortune 500 supply chains.

View Full Profile →