跳转到主要内容

timescaledb_toolkit

分析型超函数库、时间序列管道处理及其他 SQL 工具

timescaledb_toolkit · 分析型超函数库、时间序列管道处理及其他 SQL 工具

概览

ID扩展软件包版本类别许可证编程语言
1010timescaledb_toolkittimescaledb_toolkit1.30.0TIMETimescaleRust
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--s-dt-否是否是否是
PG15 packages use 1.27.0.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY1.30.019 18 17 16 15timescaledb_toolkit-
RPMPIGSTY1.30.019 18 17 16 15timescaledb-toolkit_$v-
DEBPIGSTY1.30.019 18 17 16 15postgresql-$v-timescaledb-toolkit-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
el8.aarch64N/APIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
el9.x86_64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
el9.aarch64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
el10.x86_64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
el10.aarch64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
d12.x86_64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
d12.aarch64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
d13.x86_64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
d13.aarch64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
u22.x86_64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
u22.aarch64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
u24.x86_64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
u24.aarch64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
u26.x86_64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
u26.aarch64PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.30.0PIGSTY 1.27.0
软件包版本平台仓库大小下载
timescaledb-toolkit_191.30.0el9.x86_64pigsty3.1 MiBtimescaledb-toolkit_19-1.30.0-1PGSTY.el9.x86_64.rpm
timescaledb-toolkit_191.30.0el9.aarch64pigsty2.9 MiBtimescaledb-toolkit_19-1.30.0-1PGSTY.el9.aarch64.rpm
timescaledb-toolkit_191.30.0el10.x86_64pigsty3.1 MiBtimescaledb-toolkit_19-1.30.0-1PGSTY.el10.x86_64.rpm
timescaledb-toolkit_191.30.0el10.aarch64pigsty2.9 MiBtimescaledb-toolkit_19-1.30.0-1PGSTY.el10.aarch64.rpm
postgresql-19-timescaledb-toolkit1.30.0d12.x86_64pigsty2.7 MiBpostgresql-19-timescaledb-toolkit_1.30.0-1PGSTY~bookworm_amd64.deb
postgresql-19-timescaledb-toolkit1.30.0d12.aarch64pigsty2.3 MiBpostgresql-19-timescaledb-toolkit_1.30.0-1PGSTY~bookworm_arm64.deb
postgresql-19-timescaledb-toolkit1.30.0d13.x86_64pigsty2.7 MiBpostgresql-19-timescaledb-toolkit_1.30.0-1PGSTY~trixie_amd64.deb
postgresql-19-timescaledb-toolkit1.30.0d13.aarch64pigsty2.3 MiBpostgresql-19-timescaledb-toolkit_1.30.0-1PGSTY~trixie_arm64.deb
postgresql-19-timescaledb-toolkit1.30.0u22.x86_64pigsty3.0 MiBpostgresql-19-timescaledb-toolkit_1.30.0-1PGSTY~jammy_amd64.deb
postgresql-19-timescaledb-toolkit1.30.0u22.aarch64pigsty2.7 MiBpostgresql-19-timescaledb-toolkit_1.30.0-1PGSTY~jammy_arm64.deb
postgresql-19-timescaledb-toolkit1.30.0u24.x86_64pigsty2.9 MiBpostgresql-19-timescaledb-toolkit_1.30.0-1PGSTY~noble_amd64.deb
postgresql-19-timescaledb-toolkit1.30.0u24.aarch64pigsty2.7 MiBpostgresql-19-timescaledb-toolkit_1.30.0-1PGSTY~noble_arm64.deb
postgresql-19-timescaledb-toolkit1.30.0u26.x86_64pigsty2.9 MiBpostgresql-19-timescaledb-toolkit_1.30.0-1PGSTY~resolute_amd64.deb
postgresql-19-timescaledb-toolkit1.30.0u26.aarch64pigsty2.7 MiBpostgresql-19-timescaledb-toolkit_1.30.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
timescaledb-toolkit_181.30.0el8.x86_64pigsty3.2 MiBtimescaledb-toolkit_18-1.30.0-1PGSTY.el8.x86_64.rpm
timescaledb-toolkit_181.30.0el8.aarch64pigsty2.8 MiBtimescaledb-toolkit_18-1.30.0-1PGSTY.el8.aarch64.rpm
timescaledb-toolkit_181.30.0el9.x86_64pigsty3.1 MiBtimescaledb-toolkit_18-1.30.0-1PGSTY.el9.x86_64.rpm
timescaledb-toolkit_181.30.0el9.aarch64pigsty2.9 MiBtimescaledb-toolkit_18-1.30.0-1PGSTY.el9.aarch64.rpm
timescaledb-toolkit_181.30.0el10.x86_64pigsty3.1 MiBtimescaledb-toolkit_18-1.30.0-1PGSTY.el10.x86_64.rpm
timescaledb-toolkit_181.30.0el10.aarch64pigsty3.0 MiBtimescaledb-toolkit_18-1.30.0-1PGSTY.el10.aarch64.rpm
postgresql-18-timescaledb-toolkit1.30.0d12.x86_64pigsty2.7 MiBpostgresql-18-timescaledb-toolkit_1.30.0-1PGSTY~bookworm_amd64.deb
postgresql-18-timescaledb-toolkit1.30.0d12.aarch64pigsty2.3 MiBpostgresql-18-timescaledb-toolkit_1.30.0-1PGSTY~bookworm_arm64.deb
postgresql-18-timescaledb-toolkit1.30.0d13.x86_64pigsty2.7 MiBpostgresql-18-timescaledb-toolkit_1.30.0-1PGSTY~trixie_amd64.deb
postgresql-18-timescaledb-toolkit1.30.0d13.aarch64pigsty2.3 MiBpostgresql-18-timescaledb-toolkit_1.30.0-1PGSTY~trixie_arm64.deb
postgresql-18-timescaledb-toolkit1.30.0u22.x86_64pigsty2.9 MiBpostgresql-18-timescaledb-toolkit_1.30.0-1PGSTY~jammy_amd64.deb
postgresql-18-timescaledb-toolkit1.30.0u22.aarch64pigsty2.7 MiBpostgresql-18-timescaledb-toolkit_1.30.0-1PGSTY~jammy_arm64.deb
postgresql-18-timescaledb-toolkit1.30.0u24.x86_64pigsty2.9 MiBpostgresql-18-timescaledb-toolkit_1.30.0-1PGSTY~noble_amd64.deb
postgresql-18-timescaledb-toolkit1.30.0u24.aarch64pigsty2.7 MiBpostgresql-18-timescaledb-toolkit_1.30.0-1PGSTY~noble_arm64.deb
postgresql-18-timescaledb-toolkit1.30.0u26.x86_64pigsty2.9 MiBpostgresql-18-timescaledb-toolkit_1.30.0-1PGSTY~resolute_amd64.deb
postgresql-18-timescaledb-toolkit1.30.0u26.aarch64pigsty2.7 MiBpostgresql-18-timescaledb-toolkit_1.30.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
timescaledb-toolkit_171.30.0el8.x86_64pigsty3.2 MiBtimescaledb-toolkit_17-1.30.0-1PGSTY.el8.x86_64.rpm
timescaledb-toolkit_171.30.0el8.aarch64pigsty2.8 MiBtimescaledb-toolkit_17-1.30.0-1PGSTY.el8.aarch64.rpm
timescaledb-toolkit_171.30.0el9.x86_64pigsty3.1 MiBtimescaledb-toolkit_17-1.30.0-1PGSTY.el9.x86_64.rpm
timescaledb-toolkit_171.30.0el9.aarch64pigsty2.9 MiBtimescaledb-toolkit_17-1.30.0-1PGSTY.el9.aarch64.rpm
timescaledb-toolkit_171.30.0el10.x86_64pigsty3.1 MiBtimescaledb-toolkit_17-1.30.0-1PGSTY.el10.x86_64.rpm
timescaledb-toolkit_171.30.0el10.aarch64pigsty2.9 MiBtimescaledb-toolkit_17-1.30.0-1PGSTY.el10.aarch64.rpm
postgresql-17-timescaledb-toolkit1.30.0d12.x86_64pigsty2.7 MiBpostgresql-17-timescaledb-toolkit_1.30.0-1PGSTY~bookworm_amd64.deb
postgresql-17-timescaledb-toolkit1.30.0d12.aarch64pigsty2.3 MiBpostgresql-17-timescaledb-toolkit_1.30.0-1PGSTY~bookworm_arm64.deb
postgresql-17-timescaledb-toolkit1.30.0d13.x86_64pigsty2.7 MiBpostgresql-17-timescaledb-toolkit_1.30.0-1PGSTY~trixie_amd64.deb
postgresql-17-timescaledb-toolkit1.30.0d13.aarch64pigsty2.3 MiBpostgresql-17-timescaledb-toolkit_1.30.0-1PGSTY~trixie_arm64.deb
postgresql-17-timescaledb-toolkit1.30.0u22.x86_64pigsty2.9 MiBpostgresql-17-timescaledb-toolkit_1.30.0-1PGSTY~jammy_amd64.deb
postgresql-17-timescaledb-toolkit1.30.0u22.aarch64pigsty2.7 MiBpostgresql-17-timescaledb-toolkit_1.30.0-1PGSTY~jammy_arm64.deb
postgresql-17-timescaledb-toolkit1.30.0u24.x86_64pigsty2.9 MiBpostgresql-17-timescaledb-toolkit_1.30.0-1PGSTY~noble_amd64.deb
postgresql-17-timescaledb-toolkit1.30.0u24.aarch64pigsty2.7 MiBpostgresql-17-timescaledb-toolkit_1.30.0-1PGSTY~noble_arm64.deb
postgresql-17-timescaledb-toolkit1.30.0u26.x86_64pigsty2.9 MiBpostgresql-17-timescaledb-toolkit_1.30.0-1PGSTY~resolute_amd64.deb
postgresql-17-timescaledb-toolkit1.30.0u26.aarch64pigsty2.6 MiBpostgresql-17-timescaledb-toolkit_1.30.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
timescaledb-toolkit_161.30.0el8.x86_64pigsty3.2 MiBtimescaledb-toolkit_16-1.30.0-1PGSTY.el8.x86_64.rpm
timescaledb-toolkit_161.30.0el8.aarch64pigsty2.8 MiBtimescaledb-toolkit_16-1.30.0-1PGSTY.el8.aarch64.rpm
timescaledb-toolkit_161.30.0el9.x86_64pigsty3.1 MiBtimescaledb-toolkit_16-1.30.0-1PGSTY.el9.x86_64.rpm
timescaledb-toolkit_161.30.0el9.aarch64pigsty2.9 MiBtimescaledb-toolkit_16-1.30.0-1PGSTY.el9.aarch64.rpm
timescaledb-toolkit_161.30.0el10.x86_64pigsty3.1 MiBtimescaledb-toolkit_16-1.30.0-1PGSTY.el10.x86_64.rpm
timescaledb-toolkit_161.30.0el10.aarch64pigsty2.9 MiBtimescaledb-toolkit_16-1.30.0-1PGSTY.el10.aarch64.rpm
postgresql-16-timescaledb-toolkit1.30.0d12.x86_64pigsty2.7 MiBpostgresql-16-timescaledb-toolkit_1.30.0-1PGSTY~bookworm_amd64.deb
postgresql-16-timescaledb-toolkit1.30.0d12.aarch64pigsty2.3 MiBpostgresql-16-timescaledb-toolkit_1.30.0-1PGSTY~bookworm_arm64.deb
postgresql-16-timescaledb-toolkit1.30.0d13.x86_64pigsty2.7 MiBpostgresql-16-timescaledb-toolkit_1.30.0-1PGSTY~trixie_amd64.deb
postgresql-16-timescaledb-toolkit1.30.0d13.aarch64pigsty2.3 MiBpostgresql-16-timescaledb-toolkit_1.30.0-1PGSTY~trixie_arm64.deb
postgresql-16-timescaledb-toolkit1.30.0u22.x86_64pigsty2.9 MiBpostgresql-16-timescaledb-toolkit_1.30.0-1PGSTY~jammy_amd64.deb
postgresql-16-timescaledb-toolkit1.30.0u22.aarch64pigsty2.7 MiBpostgresql-16-timescaledb-toolkit_1.30.0-1PGSTY~jammy_arm64.deb
postgresql-16-timescaledb-toolkit1.30.0u24.x86_64pigsty2.9 MiBpostgresql-16-timescaledb-toolkit_1.30.0-1PGSTY~noble_amd64.deb
postgresql-16-timescaledb-toolkit1.30.0u24.aarch64pigsty2.7 MiBpostgresql-16-timescaledb-toolkit_1.30.0-1PGSTY~noble_arm64.deb
postgresql-16-timescaledb-toolkit1.30.0u26.x86_64pigsty2.9 MiBpostgresql-16-timescaledb-toolkit_1.30.0-1PGSTY~resolute_amd64.deb
postgresql-16-timescaledb-toolkit1.30.0u26.aarch64pigsty2.6 MiBpostgresql-16-timescaledb-toolkit_1.30.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
timescaledb-toolkit_151.27.0el8.x86_64pigsty4.7 MiBtimescaledb-toolkit_15-1.27.0-1PGSTY.el8.x86_64.rpm
timescaledb-toolkit_151.27.0el8.aarch64pigsty3.1 MiBtimescaledb-toolkit_15-1.27.0-1PGSTY.el8.aarch64.rpm
timescaledb-toolkit_151.27.0el9.x86_64pigsty4.1 MiBtimescaledb-toolkit_15-1.27.0-1PGSTY.el9.x86_64.rpm
timescaledb-toolkit_151.27.0el9.aarch64pigsty3.3 MiBtimescaledb-toolkit_15-1.27.0-1PGSTY.el9.aarch64.rpm
timescaledb-toolkit_151.27.0el10.x86_64pigsty4.1 MiBtimescaledb-toolkit_15-1.27.0-1PGSTY.el10.x86_64.rpm
timescaledb-toolkit_151.27.0el10.aarch64pigsty3.3 MiBtimescaledb-toolkit_15-1.27.0-1PGSTY.el10.aarch64.rpm
postgresql-15-timescaledb-toolkit1.27.0d12.x86_64pigsty3.6 MiBpostgresql-15-timescaledb-toolkit_1.27.0-1PGSTY~bookworm_amd64.deb
postgresql-15-timescaledb-toolkit1.27.0d12.aarch64pigsty2.6 MiBpostgresql-15-timescaledb-toolkit_1.27.0-1PGSTY~bookworm_arm64.deb
postgresql-15-timescaledb-toolkit1.27.0d13.x86_64pigsty3.6 MiBpostgresql-15-timescaledb-toolkit_1.27.0-1PGSTY~trixie_amd64.deb
postgresql-15-timescaledb-toolkit1.27.0d13.aarch64pigsty2.6 MiBpostgresql-15-timescaledb-toolkit_1.27.0-1PGSTY~trixie_arm64.deb
postgresql-15-timescaledb-toolkit1.27.0u22.x86_64pigsty3.9 MiBpostgresql-15-timescaledb-toolkit_1.27.0-1PGSTY~jammy_amd64.deb
postgresql-15-timescaledb-toolkit1.27.0u22.aarch64pigsty3.1 MiBpostgresql-15-timescaledb-toolkit_1.27.0-1PGSTY~jammy_arm64.deb
postgresql-15-timescaledb-toolkit1.27.0u24.x86_64pigsty3.9 MiBpostgresql-15-timescaledb-toolkit_1.27.0-1PGSTY~noble_amd64.deb
postgresql-15-timescaledb-toolkit1.27.0u24.aarch64pigsty3.0 MiBpostgresql-15-timescaledb-toolkit_1.27.0-1PGSTY~noble_arm64.deb
postgresql-15-timescaledb-toolkit1.27.0u26.x86_64pigsty3.9 MiBpostgresql-15-timescaledb-toolkit_1.27.0-1PGSTY~resolute_amd64.deb
postgresql-15-timescaledb-toolkit1.27.0u26.aarch64pigsty3.0 MiBpostgresql-15-timescaledb-toolkit_1.27.0-1PGSTY~resolute_arm64.deb

源码

pig build pkg timescaledb_toolkit;		# build rpm/deb

安装

启用 PGDG 和 PIGSTY 仓库并更新缓存。

pig repo add pgsql -u   # add both repo and update cache

使用 pig 安装扩展。

pig install timescaledb_toolkit;		# install by package name

pig install timescaledb_toolkit -v 19;   # install for PG 19
pig install timescaledb_toolkit -v 18;   # install for PG 18
pig install timescaledb_toolkit -v 17;   # install for PG 17
pig install timescaledb_toolkit -v 16;   # install for PG 16

在数据库中创建扩展。

CREATE EXTENSION timescaledb_toolkit;

用法

来源:

TimescaleDB Toolkit 提供了一系列专用于时序数据分析的函数,采用 两步聚合模式。大多数函数会先创建中间表示,再通过访问器函数进行查询,从而实现高效复用和多维分析。

启用与兼容性

timescaledb_toolkit 1.30.0 支持 PostgreSQL 16–19。安装匹配库后,在使用它的数据库执行:

CREATE EXTENSION timescaledb_toolkit;

控制文件标记为 trusted,具有数据库 CREATE 权限的角色可安装;无需共享预加载。下方使用 time_bucket 的示例还假定已启用 TimescaleDB。升级已有安装时使用匹配的库和提供的 SQL 升级路径;实验模式中的对象不保证升级兼容。

近似分析

HyperLogLog - 基数估算

基于概率的去重计数,支持可配置精度,适用于高基数数据集。

-- Estimate unique users per day
SELECT
    date_trunc('day', timestamp) as day,
    distinct_count(hyperloglog(64, user_id)) as unique_users
FROM events
GROUP BY day;

-- Combine counts across partitions
SELECT distinct_count(rollup(hll))
FROM (SELECT hyperloglog(32, session_id) as hll FROM events_2023
      UNION ALL
      SELECT hyperloglog(32, session_id) FROM events_2024) t;

T-Digest - 分位数近似

高精度百分位估算,针对尾部分位数(P95、P99)进行了优化。

-- Track response time percentiles
SELECT
    service_name,
    approx_percentile(0.50, tdigest(100, response_time)) as p50,
    approx_percentile(0.95, tdigest(100, response_time)) as p95,
    approx_percentile(0.99, tdigest(100, response_time)) as p99
FROM api_metrics
GROUP BY service_name;

-- Store hourly percentiles in a materialized view
CREATE MATERIALIZED VIEW hourly_percentiles AS
SELECT
    time_bucket('1 hour', timestamp) as hour,
    tdigest(200, response_time) as digest
FROM requests GROUP BY hour;

UddSketch - 有界误差分位数

分位数估算使用指定的初始误差上界;桶数不足时实际误差上界可能增大,应通过结果的 error 检查。

-- CPU percentiles with a starting 1% error target
SELECT
    host_id,
    approx_percentile(0.95, uddsketch(100, 0.01, cpu_percent)) as p95_cpu,
    error(uddsketch(100, 0.01, cpu_percent)) as actual_error
FROM system_metrics
GROUP BY host_id;

计数器分析

计数器聚合 - 单调递增指标

处理单调递增的计数器,自动检测重置。

-- Request rate calculation
SELECT
    time_bucket('5 min', timestamp) as bucket,
    rate(counter_agg(timestamp, request_count)) as requests_per_sec,
    delta(counter_agg(timestamp, request_count)) as total_requests
FROM metrics
GROUP BY bucket;

-- Extrapolated rate for each hourly bucket
SELECT bucket, extrapolated_rate(summary, method => 'prometheus') AS bytes_per_second
FROM (
    SELECT time_bucket('1 hour', timestamp) AS bucket,
           counter_agg(timestamp, bytes_sent,
                       bounds => time_bucket_range('1 hour', timestamp)) AS summary
    FROM network_stats
    GROUP BY time_bucket('1 hour', timestamp)
) hourly;

仪表聚合 - 波动指标

用于分析上下波动的指标(如温度、内存使用量)。

-- Temperature change analysis
SELECT
    sensor_id,
    delta(gauge_agg(timestamp, temperature)) as temp_delta,
    rate(gauge_agg(timestamp, temperature)) as temp_rate_per_sec
FROM weather_data
GROUP BY sensor_id;

时间加权分析

时间加权平均

处理不规则采样数据,支持插值方法(LOCF 末次观测值保持、Linear 线性插值)。

-- Weighted average for irregular sensor readings
SELECT
    device_id,
    average(time_weight('LOCF', timestamp, sensor_value)) as weighted_avg,
    average(time_weight('Linear', timestamp, sensor_value)) as linear_avg
FROM iot_readings
GROUP BY device_id;

-- Combining multiple time ranges
SELECT average(rollup(tw))
FROM (SELECT time_weight('LOCF', ts, val) as tw FROM readings_2023
      UNION ALL
      SELECT time_weight('LOCF', ts, val) FROM readings_2024) t;

数据可视化

LTTB 降采样

在保持视觉相似性的前提下对时序数据进行降采样,适用于图表展示。

-- Reduce 100K points to 1K for visualization
SELECT time, value
FROM unnest((
    SELECT lttb(timestamp, price, 1000)
    FROM stock_prices
    WHERE symbol = 'AAPL'
));

ASAP 平滑

通过降噪生成易于阅读的图表,同时保留趋势特征。

-- Smooth daily data to weekly resolution
SELECT time, value
FROM unnest((
    SELECT asap_smooth(date, daily_sales, 52)
    FROM sales_data
    WHERE date >= '2023-01-01'
));

统计分析

统计聚合

提供全面的统计分析能力,支持一维和二维回归分析。

-- Multi-variable analysis
SELECT
    -- Basic statistics
    average(stats_agg(response_time)) as avg_response,
    stddev(stats_agg(response_time)) as response_stddev,

    -- Regression analysis
    slope(stats_agg(response_time, request_size)) as size_impact,
    corr(stats_agg(response_time, request_size)) as correlation,
    determination_coeff(stats_agg(response_time, request_size)) as r_squared
FROM performance_data;

Timevector 数据类型

Timevector 保存时序数据的中间值。toolkit_experimental.lttb 等流水线操作符仍属实验功能;降采样前应排序。

-- Create and manipulate timevector
CREATE VIEW cpu_series AS
SELECT host_id, timevector(timestamp, cpu_percent) as ts
FROM system_metrics GROUP BY host_id;

-- Chain operations on timevector
SELECT host_id, unnest(ts -> toolkit_experimental.sort()
                         -> toolkit_experimental.lttb(100))
FROM cpu_series;

集成模式

物化聚合

下面创建的是普通物化视图,源数据变化后须显式刷新。TimescaleDB 连续聚合还需要超表、连续聚合选项与合适的刷新策略。

CREATE MATERIALIZED VIEW hourly_analytics AS
SELECT
    time_bucket('1 hour', timestamp) as hour,
    service_name,
    tdigest(100, response_time) as response_digest,
    counter_agg(timestamp, request_count) as request_counter,
    hyperloglog(64, user_id) as unique_users
FROM api_events
GROUP BY hour, service_name;

-- Query pre-computed aggregates
SELECT
    hour,
    approx_percentile(0.95, response_digest) as p95_response,
    rate(request_counter) as req_per_sec,
    distinct_count(unique_users) as unique_users
FROM hourly_analytics
WHERE hour >= NOW() - INTERVAL '24 hours';

两步分析模式

存储中间聚合结果,支持多维度分析复用:

-- Step 1: Create aggregates
CREATE TABLE daily_summaries AS
SELECT
    date_trunc('day', timestamp) as day,
    tdigest(200, response_time) as response_digest,
    stats_agg(response_time, request_size) as stats
FROM requests GROUP BY day;

-- Step 2: Multiple analyses from same data
SELECT
    day,
    approx_percentile(0.50, response_digest) as median,
    approx_percentile(0.99, response_digest) as p99,
    average_y(stats) as avg_response,
    slope(stats) as size_correlation
FROM daily_summaries;

所有位于 experimental 模式(toolkit_experimental)中的函数可能会在版本间发生变化。生产环境中如需 API 稳定性保证,请使用稳定版函数。

版本说明

1.23 中,max_n、max_n_by、min_n 和 min_n_by 对空输入返回空结果;stats_agg(bigint) 拒绝超出双精度精确整数范围的值。这些行为在后续版本仍需纳入应用测试。

1.24 为 heartbeat_agg 添加相等操作符,并修复空频率聚合与统计溢出处理;1.25 稳定了 gauge_agg,修复空输入的 t-digest 和若干类型处理问题;1.26 移除 time_weight 及其 rollup 的组合、序列化与反序列化支持,保持其非并行聚合边界。

1.27 为 statssummary1d 和 statssummary2d 添加相等操作符,修复稀疏 HyperLogLog 合并及相同时间戳计数器点的误判重置。它拒绝无效 UTF-8 状态名、超过 1024 字节或括号嵌套超过 256 层的 lambda、不同元素类型的频率聚合合并,以及 topn 中类型不匹配的 NULL 参数;还补上流水线 map 的权限检查。

版本 1.28.0

1.28.0 为 timeweightsummary 增加相等运算符,并修复 interpolated_delta 和 interpolated_rate 遇到 NULL 计数器摘要时的错误。此版本停止构建和发布 PostgreSQL 15 二进制,源码声明 PostgreSQL 16–18 支持。选择该版本前应先升级 PG15 服务器。已有包支持字段反映独立发布的包,不能据此认定所有坐标均已提供 1.28.0。

安装匹配文件,使用发行版提供的路径更新扩展,并复核依赖 NULL 计数器输入或时间加权摘要相等性的查询:

ALTER EXTENSION timescaledb_toolkit UPDATE TO '1.28.0';

版本 1.29.0

1.29.0 支持 PostgreSQL 16–19,新增 PostgreSQL 19 发行构建,并停止 Debian 11 构建。前面的版本段落描述历史兼容性。安装与服务器主版本匹配的文件,然后使用发行版提供的升级路径:

ALTER EXTENSION timescaledb_toolkit UPDATE TO '1.29.0';

版本 1.30.0

本版新增 toolkit_experimental.get_toolkit_git_commit(),用于检查编译时嵌入的源码修订,并将上游 pgrx 依赖更新到 0.19.5。继续支持 PostgreSQL 16–19,实验函数不属于稳定 API 契约。安装匹配文件后,使用发行版提供的升级路径:

ALTER EXTENSION timescaledb_toolkit UPDATE TO '1.30.0';
SELECT toolkit_experimental.get_toolkit_git_commit();

表格阅读器