跳转到主要内容

timeseries

时序数据API封装

pg_timeseries · 时序数据API封装

概览

ID扩展软件包版本类别许可证编程语言
1020timeseriespg_timeseries0.2.1TIMEPostgreSQLSQL
属性可执行文件动态库需要预加载需要建扩展可迁移可信
----d--否否否是否否

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY0.2.119 18 17 16 15pg_timeseriespg_cron, pg_partman
RPMPIGSTY0.2.119 18 17 16 15pg_timeseries_$vpg_cron_$v, pg_partman_$v
DEBPIGSTY0.2.119 18 17 16 15postgresql-$v-pg-timeseriespostgresql-$v-cron, postgresql-$v-partman
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
el8.aarch64N/APIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
el9.x86_64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
el9.aarch64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
el10.x86_64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
el10.aarch64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
d12.x86_64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
d12.aarch64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
d13.x86_64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
d13.aarch64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
u22.x86_64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
u22.aarch64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
u24.x86_64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
u24.aarch64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
u26.x86_64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
u26.aarch64PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1PIGSTY 0.2.1
软件包版本平台仓库大小下载
pg_timeseries_190.2.1el9.x86_64pigsty28.0 KiBpg_timeseries_19-0.2.1-1PGSTY.el9.noarch.rpm
pg_timeseries_190.2.1el9.aarch64pigsty28.0 KiBpg_timeseries_19-0.2.1-1PGSTY.el9.noarch.rpm
pg_timeseries_190.2.1el10.x86_64pigsty28.2 KiBpg_timeseries_19-0.2.1-1PGSTY.el10.noarch.rpm
pg_timeseries_190.2.1el10.aarch64pigsty28.2 KiBpg_timeseries_19-0.2.1-1PGSTY.el10.noarch.rpm
postgresql-19-pg-timeseries0.2.1d12.x86_64pigsty12.9 KiBpostgresql-19-pg-timeseries_0.2.1-1PGSTY~bookworm_all.deb
postgresql-19-pg-timeseries0.2.1d12.aarch64pigsty12.9 KiBpostgresql-19-pg-timeseries_0.2.1-1PGSTY~bookworm_all.deb
postgresql-19-pg-timeseries0.2.1d13.x86_64pigsty12.8 KiBpostgresql-19-pg-timeseries_0.2.1-1PGSTY~trixie_all.deb
postgresql-19-pg-timeseries0.2.1d13.aarch64pigsty12.8 KiBpostgresql-19-pg-timeseries_0.2.1-1PGSTY~trixie_all.deb
postgresql-19-pg-timeseries0.2.1u22.x86_64pigsty13.2 KiBpostgresql-19-pg-timeseries_0.2.1-1PGSTY~jammy_all.deb
postgresql-19-pg-timeseries0.2.1u22.aarch64pigsty13.2 KiBpostgresql-19-pg-timeseries_0.2.1-1PGSTY~jammy_all.deb
postgresql-19-pg-timeseries0.2.1u24.x86_64pigsty13.2 KiBpostgresql-19-pg-timeseries_0.2.1-1PGSTY~noble_all.deb
postgresql-19-pg-timeseries0.2.1u24.aarch64pigsty13.2 KiBpostgresql-19-pg-timeseries_0.2.1-1PGSTY~noble_all.deb
postgresql-19-pg-timeseries0.2.1u26.x86_64pigsty13.2 KiBpostgresql-19-pg-timeseries_0.2.1-1PGSTY~resolute_all.deb
postgresql-19-pg-timeseries0.2.1u26.aarch64pigsty13.2 KiBpostgresql-19-pg-timeseries_0.2.1-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
pg_timeseries_180.2.1el8.x86_64pigsty28.4 KiBpg_timeseries_18-0.2.1-1PGSTY.el8.noarch.rpm
pg_timeseries_180.2.1el8.aarch64pigsty28.4 KiBpg_timeseries_18-0.2.1-1PGSTY.el8.noarch.rpm
pg_timeseries_180.2.1el9.x86_64pigsty28.0 KiBpg_timeseries_18-0.2.1-1PGSTY.el9.noarch.rpm
pg_timeseries_180.2.1el9.aarch64pigsty28.0 KiBpg_timeseries_18-0.2.1-1PGSTY.el9.noarch.rpm
pg_timeseries_180.2.1el10.x86_64pigsty28.2 KiBpg_timeseries_18-0.2.1-1PGSTY.el10.noarch.rpm
pg_timeseries_180.2.1el10.aarch64pigsty28.2 KiBpg_timeseries_18-0.2.1-1PGSTY.el10.noarch.rpm
postgresql-18-pg-timeseries0.2.1d12.x86_64pigsty12.9 KiBpostgresql-18-pg-timeseries_0.2.1-1PGSTY~bookworm_all.deb
postgresql-18-pg-timeseries0.2.1d12.aarch64pigsty12.9 KiBpostgresql-18-pg-timeseries_0.2.1-1PGSTY~bookworm_all.deb
postgresql-18-pg-timeseries0.2.1d13.x86_64pigsty12.8 KiBpostgresql-18-pg-timeseries_0.2.1-1PGSTY~trixie_all.deb
postgresql-18-pg-timeseries0.2.1d13.aarch64pigsty12.8 KiBpostgresql-18-pg-timeseries_0.2.1-1PGSTY~trixie_all.deb
postgresql-18-pg-timeseries0.2.1u22.x86_64pigsty13.2 KiBpostgresql-18-pg-timeseries_0.2.1-1PGSTY~jammy_all.deb
postgresql-18-pg-timeseries0.2.1u22.aarch64pigsty13.2 KiBpostgresql-18-pg-timeseries_0.2.1-1PGSTY~jammy_all.deb
postgresql-18-pg-timeseries0.2.1u24.x86_64pigsty13.2 KiBpostgresql-18-pg-timeseries_0.2.1-1PGSTY~noble_all.deb
postgresql-18-pg-timeseries0.2.1u24.aarch64pigsty13.2 KiBpostgresql-18-pg-timeseries_0.2.1-1PGSTY~noble_all.deb
postgresql-18-pg-timeseries0.2.1u26.x86_64pigsty13.2 KiBpostgresql-18-pg-timeseries_0.2.1-1PGSTY~resolute_all.deb
postgresql-18-pg-timeseries0.2.1u26.aarch64pigsty13.2 KiBpostgresql-18-pg-timeseries_0.2.1-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
pg_timeseries_170.2.1el8.x86_64pigsty28.4 KiBpg_timeseries_17-0.2.1-1PGSTY.el8.noarch.rpm
pg_timeseries_170.2.1el8.aarch64pigsty28.4 KiBpg_timeseries_17-0.2.1-1PGSTY.el8.noarch.rpm
pg_timeseries_170.2.1el9.x86_64pigsty28.0 KiBpg_timeseries_17-0.2.1-1PGSTY.el9.noarch.rpm
pg_timeseries_170.2.1el9.aarch64pigsty28.0 KiBpg_timeseries_17-0.2.1-1PGSTY.el9.noarch.rpm
pg_timeseries_170.2.1el10.x86_64pigsty28.2 KiBpg_timeseries_17-0.2.1-1PGSTY.el10.noarch.rpm
pg_timeseries_170.2.1el10.aarch64pigsty28.2 KiBpg_timeseries_17-0.2.1-1PGSTY.el10.noarch.rpm
postgresql-17-pg-timeseries0.2.1d12.x86_64pigsty12.9 KiBpostgresql-17-pg-timeseries_0.2.1-1PGSTY~bookworm_all.deb
postgresql-17-pg-timeseries0.2.1d12.aarch64pigsty12.9 KiBpostgresql-17-pg-timeseries_0.2.1-1PGSTY~bookworm_all.deb
postgresql-17-pg-timeseries0.2.1d13.x86_64pigsty12.8 KiBpostgresql-17-pg-timeseries_0.2.1-1PGSTY~trixie_all.deb
postgresql-17-pg-timeseries0.2.1d13.aarch64pigsty12.8 KiBpostgresql-17-pg-timeseries_0.2.1-1PGSTY~trixie_all.deb
postgresql-17-pg-timeseries0.2.1u22.x86_64pigsty13.2 KiBpostgresql-17-pg-timeseries_0.2.1-1PGSTY~jammy_all.deb
postgresql-17-pg-timeseries0.2.1u22.aarch64pigsty13.2 KiBpostgresql-17-pg-timeseries_0.2.1-1PGSTY~jammy_all.deb
postgresql-17-pg-timeseries0.2.1u24.x86_64pigsty13.2 KiBpostgresql-17-pg-timeseries_0.2.1-1PGSTY~noble_all.deb
postgresql-17-pg-timeseries0.2.1u24.aarch64pigsty13.2 KiBpostgresql-17-pg-timeseries_0.2.1-1PGSTY~noble_all.deb
postgresql-17-pg-timeseries0.2.1u26.x86_64pigsty13.2 KiBpostgresql-17-pg-timeseries_0.2.1-1PGSTY~resolute_all.deb
postgresql-17-pg-timeseries0.2.1u26.aarch64pigsty13.2 KiBpostgresql-17-pg-timeseries_0.2.1-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
pg_timeseries_160.2.1el8.x86_64pigsty28.4 KiBpg_timeseries_16-0.2.1-1PGSTY.el8.noarch.rpm
pg_timeseries_160.2.1el8.aarch64pigsty28.4 KiBpg_timeseries_16-0.2.1-1PGSTY.el8.noarch.rpm
pg_timeseries_160.2.1el9.x86_64pigsty28.0 KiBpg_timeseries_16-0.2.1-1PGSTY.el9.noarch.rpm
pg_timeseries_160.2.1el9.aarch64pigsty28.0 KiBpg_timeseries_16-0.2.1-1PGSTY.el9.noarch.rpm
pg_timeseries_160.2.1el10.x86_64pigsty28.2 KiBpg_timeseries_16-0.2.1-1PGSTY.el10.noarch.rpm
pg_timeseries_160.2.1el10.aarch64pigsty28.2 KiBpg_timeseries_16-0.2.1-1PGSTY.el10.noarch.rpm
postgresql-16-pg-timeseries0.2.1d12.x86_64pigsty12.9 KiBpostgresql-16-pg-timeseries_0.2.1-1PGSTY~bookworm_all.deb
postgresql-16-pg-timeseries0.2.1d12.aarch64pigsty12.9 KiBpostgresql-16-pg-timeseries_0.2.1-1PGSTY~bookworm_all.deb
postgresql-16-pg-timeseries0.2.1d13.x86_64pigsty12.8 KiBpostgresql-16-pg-timeseries_0.2.1-1PGSTY~trixie_all.deb
postgresql-16-pg-timeseries0.2.1d13.aarch64pigsty12.8 KiBpostgresql-16-pg-timeseries_0.2.1-1PGSTY~trixie_all.deb
postgresql-16-pg-timeseries0.2.1u22.x86_64pigsty13.2 KiBpostgresql-16-pg-timeseries_0.2.1-1PGSTY~jammy_all.deb
postgresql-16-pg-timeseries0.2.1u22.aarch64pigsty13.2 KiBpostgresql-16-pg-timeseries_0.2.1-1PGSTY~jammy_all.deb
postgresql-16-pg-timeseries0.2.1u24.x86_64pigsty13.2 KiBpostgresql-16-pg-timeseries_0.2.1-1PGSTY~noble_all.deb
postgresql-16-pg-timeseries0.2.1u24.aarch64pigsty13.2 KiBpostgresql-16-pg-timeseries_0.2.1-1PGSTY~noble_all.deb
postgresql-16-pg-timeseries0.2.1u26.x86_64pigsty13.2 KiBpostgresql-16-pg-timeseries_0.2.1-1PGSTY~resolute_all.deb
postgresql-16-pg-timeseries0.2.1u26.aarch64pigsty13.2 KiBpostgresql-16-pg-timeseries_0.2.1-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
pg_timeseries_150.2.1el8.x86_64pigsty28.4 KiBpg_timeseries_15-0.2.1-1PGSTY.el8.noarch.rpm
pg_timeseries_150.2.1el8.aarch64pigsty28.4 KiBpg_timeseries_15-0.2.1-1PGSTY.el8.noarch.rpm
pg_timeseries_150.2.1el9.x86_64pigsty28.0 KiBpg_timeseries_15-0.2.1-1PGSTY.el9.noarch.rpm
pg_timeseries_150.2.1el9.aarch64pigsty28.0 KiBpg_timeseries_15-0.2.1-1PGSTY.el9.noarch.rpm
pg_timeseries_150.2.1el10.x86_64pigsty28.2 KiBpg_timeseries_15-0.2.1-1PGSTY.el10.noarch.rpm
pg_timeseries_150.2.1el10.aarch64pigsty28.2 KiBpg_timeseries_15-0.2.1-1PGSTY.el10.noarch.rpm
postgresql-15-pg-timeseries0.2.1d12.x86_64pigsty12.9 KiBpostgresql-15-pg-timeseries_0.2.1-1PGSTY~bookworm_all.deb
postgresql-15-pg-timeseries0.2.1d12.aarch64pigsty12.9 KiBpostgresql-15-pg-timeseries_0.2.1-1PGSTY~bookworm_all.deb
postgresql-15-pg-timeseries0.2.1d13.x86_64pigsty12.8 KiBpostgresql-15-pg-timeseries_0.2.1-1PGSTY~trixie_all.deb
postgresql-15-pg-timeseries0.2.1d13.aarch64pigsty12.8 KiBpostgresql-15-pg-timeseries_0.2.1-1PGSTY~trixie_all.deb
postgresql-15-pg-timeseries0.2.1u22.x86_64pigsty13.2 KiBpostgresql-15-pg-timeseries_0.2.1-1PGSTY~jammy_all.deb
postgresql-15-pg-timeseries0.2.1u22.aarch64pigsty13.2 KiBpostgresql-15-pg-timeseries_0.2.1-1PGSTY~jammy_all.deb
postgresql-15-pg-timeseries0.2.1u24.x86_64pigsty13.2 KiBpostgresql-15-pg-timeseries_0.2.1-1PGSTY~noble_all.deb
postgresql-15-pg-timeseries0.2.1u24.aarch64pigsty13.2 KiBpostgresql-15-pg-timeseries_0.2.1-1PGSTY~noble_all.deb
postgresql-15-pg-timeseries0.2.1u26.x86_64pigsty13.2 KiBpostgresql-15-pg-timeseries_0.2.1-1PGSTY~resolute_all.deb
postgresql-15-pg-timeseries0.2.1u26.aarch64pigsty13.2 KiBpostgresql-15-pg-timeseries_0.2.1-1PGSTY~resolute_all.deb

源码

pig build pkg pg_timeseries;		# build rpm/deb

安装

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

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

使用 pig 安装扩展。

pig install pg_timeseries;		# install by package name
pig install timeseries;		# install by extension name

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

在数据库中创建扩展。

CREATE EXTENSION timeseries CASCADE; -- requires pg_cron, pg_partman

用法

来源:README、v0.2.1 release、PGXN

本扩展为时间序列表的创建、维护和使用提供了一致的用户体验。

快速开始

假设你已经有一张分区表,只需调用 enable_ts_table 函数即可启用时间序列功能:

CREATE EXTENSION timeseries CASCADE;

SELECT enable_ts_table('sensor_readings');

这一调用会自动完成以下操作:

  • 使用 PostgreSQL 原生分区功能将表重构为一系列分区
  • 每个分区覆盖一段时间范围(默认一周)
  • 自动为未来创建新分区(默认提前一个月)
  • 每小时运行一次维护任务,创建缺失的分区和未来所需的分区

使用表

索引

时间序列表本质上就是标准的 PostgreSQL 分区表,所有 PostgreSQL 的现有功能都能正常使用。

传统 B-Tree 索引适用于时间序列数据,但在某些查询场景下(通常是返回大量结果的查询),BRIN 索引可能表现更优。如果每个分区不超过一百万条记录,建议先用 B-Tree。

分区大小

扩展提供了 ts_part_info 视图,可方便查看各分区的数据大小、索引大小和总大小。一般建议每个分区能容纳在可用内存的约四分之一以内。

数据保留

调用 set_ts_retention_policy 设置保留策略,超出保留窗口的分区每小时会被自动删除。使用 clear_ts_retention_policy 恢复默认行为(无限保留)。

压缩

调用 set_ts_compression_policy 可对超过指定时间间隔的分区使用列式存储进行压缩。压缩功能依赖 citus 和 citus_columnar 扩展:

CREATE EXTENSION citus;
CREATE EXTENSION citus_columnar;

分析辅助函数

first 和 last

按一个维度分组,但按另一个维度取首条/末条记录:

SELECT machine_id,
       last(cpu_util, recorded_at)
FROM events
GROUP BY machine_id;

date_bin_table

自动将时间序列值对齐到指定步长,并为无数据的时间段补充 NULL 行:

SELECT * FROM date_bin_table(NULL::target_table, '1 hour', '[2024-02-01 00:00, 2024-02-02 15:00]');

输出结果与直接查表相比有三点不同:

  • 按时间升序排列
  • 时间列的值对齐到指定步长
  • 无数据的时间段会补充 NULL 行

make_view_incremental

将普通视图转换为自动保持更新的增量物化视图,无需手动 REFRESH。该功能基于 pg_ivm 实现:

CREATE EXTENSION pg_ivm;

依赖

可选依赖

版本说明

timeseries 0.2.1 主要是打包和兼容性工作:PG18 文档、RPM spec 修复、@extschema@ 泄漏修复,以及 runtime image dependency cleanup。SQL 使用面仍是上面的 README workflow。

表格阅读器