跳转到主要内容

pg_duration

用于表示时间段的强化数据类型

pg_duration · 用于表示时间段的强化数据类型

概览

ID扩展软件包版本类别许可证编程语言
3850pg_durationpg_duration1.0.2TYPEMITC
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--s-d-r否是否是是否

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY1.0.219 18 17 16 15pg_duration-
RPMPIGSTY1.0.219 18 17 16 15pg_duration_$v-
DEBPIGSTY1.0.219 18 17 16 15postgresql-$v-pg-duration-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
el8.aarch64N/APIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
el9.x86_64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
el9.aarch64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
el10.x86_64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
el10.aarch64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
d12.x86_64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
d12.aarch64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
d13.x86_64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
d13.aarch64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
u22.x86_64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
u22.aarch64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
u24.x86_64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
u24.aarch64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
u26.x86_64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
u26.aarch64PIGSTY 1.0.2PIGSTY 1.0.2PIGSTY 1.0.2N/AN/A
软件包版本平台仓库大小下载
pg_duration_191.0.2el9.x86_64pigsty41.7 KiBpg_duration_19-1.0.2-1PGSTY.el9.x86_64.rpm
pg_duration_191.0.2el9.aarch64pigsty40.7 KiBpg_duration_19-1.0.2-1PGSTY.el9.aarch64.rpm
pg_duration_191.0.2el10.x86_64pigsty41.6 KiBpg_duration_19-1.0.2-1PGSTY.el10.x86_64.rpm
pg_duration_191.0.2el10.aarch64pigsty41.1 KiBpg_duration_19-1.0.2-1PGSTY.el10.aarch64.rpm
postgresql-19-pg-duration1.0.2d12.x86_64pigsty29.3 KiBpostgresql-19-pg-duration_1.0.2-1PGSTY~bookworm_amd64.deb
postgresql-19-pg-duration1.0.2d12.aarch64pigsty28.9 KiBpostgresql-19-pg-duration_1.0.2-1PGSTY~bookworm_arm64.deb
postgresql-19-pg-duration1.0.2d13.x86_64pigsty29.3 KiBpostgresql-19-pg-duration_1.0.2-1PGSTY~trixie_amd64.deb
postgresql-19-pg-duration1.0.2d13.aarch64pigsty29.0 KiBpostgresql-19-pg-duration_1.0.2-1PGSTY~trixie_arm64.deb
postgresql-19-pg-duration1.0.2u22.x86_64pigsty31.6 KiBpostgresql-19-pg-duration_1.0.2-1PGSTY~jammy_amd64.deb
postgresql-19-pg-duration1.0.2u22.aarch64pigsty30.8 KiBpostgresql-19-pg-duration_1.0.2-1PGSTY~jammy_arm64.deb
postgresql-19-pg-duration1.0.2u24.x86_64pigsty30.6 KiBpostgresql-19-pg-duration_1.0.2-1PGSTY~noble_amd64.deb
postgresql-19-pg-duration1.0.2u24.aarch64pigsty30.5 KiBpostgresql-19-pg-duration_1.0.2-1PGSTY~noble_arm64.deb
postgresql-19-pg-duration1.0.2u26.x86_64pigsty30.6 KiBpostgresql-19-pg-duration_1.0.2-1PGSTY~resolute_amd64.deb
postgresql-19-pg-duration1.0.2u26.aarch64pigsty30.0 KiBpostgresql-19-pg-duration_1.0.2-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_duration_181.0.2el8.x86_64pigsty40.8 KiBpg_duration_18-1.0.2-1PGSTY.el8.x86_64.rpm
pg_duration_181.0.2el8.aarch64pigsty40.1 KiBpg_duration_18-1.0.2-1PGSTY.el8.aarch64.rpm
pg_duration_181.0.2el9.x86_64pigsty41.6 KiBpg_duration_18-1.0.2-1PGSTY.el9.x86_64.rpm
pg_duration_181.0.2el9.aarch64pigsty40.8 KiBpg_duration_18-1.0.2-1PGSTY.el9.aarch64.rpm
pg_duration_181.0.2el10.x86_64pigsty41.7 KiBpg_duration_18-1.0.2-1PGSTY.el10.x86_64.rpm
pg_duration_181.0.2el10.aarch64pigsty41.3 KiBpg_duration_18-1.0.2-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-duration1.0.2d12.x86_64pigsty29.3 KiBpostgresql-18-pg-duration_1.0.2-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-duration1.0.2d12.aarch64pigsty28.9 KiBpostgresql-18-pg-duration_1.0.2-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-duration1.0.2d13.x86_64pigsty29.3 KiBpostgresql-18-pg-duration_1.0.2-1PGSTY~trixie_amd64.deb
postgresql-18-pg-duration1.0.2d13.aarch64pigsty29.0 KiBpostgresql-18-pg-duration_1.0.2-1PGSTY~trixie_arm64.deb
postgresql-18-pg-duration1.0.2u22.x86_64pigsty31.6 KiBpostgresql-18-pg-duration_1.0.2-1PGSTY~jammy_amd64.deb
postgresql-18-pg-duration1.0.2u22.aarch64pigsty30.7 KiBpostgresql-18-pg-duration_1.0.2-1PGSTY~jammy_arm64.deb
postgresql-18-pg-duration1.0.2u24.x86_64pigsty30.6 KiBpostgresql-18-pg-duration_1.0.2-1PGSTY~noble_amd64.deb
postgresql-18-pg-duration1.0.2u24.aarch64pigsty30.4 KiBpostgresql-18-pg-duration_1.0.2-1PGSTY~noble_arm64.deb
postgresql-18-pg-duration1.0.2u26.x86_64pigsty30.6 KiBpostgresql-18-pg-duration_1.0.2-1PGSTY~resolute_amd64.deb
postgresql-18-pg-duration1.0.2u26.aarch64pigsty30.1 KiBpostgresql-18-pg-duration_1.0.2-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_duration_171.0.2el8.x86_64pigsty40.8 KiBpg_duration_17-1.0.2-1PGSTY.el8.x86_64.rpm
pg_duration_171.0.2el8.aarch64pigsty40.1 KiBpg_duration_17-1.0.2-1PGSTY.el8.aarch64.rpm
pg_duration_171.0.2el9.x86_64pigsty41.6 KiBpg_duration_17-1.0.2-1PGSTY.el9.x86_64.rpm
pg_duration_171.0.2el9.aarch64pigsty40.7 KiBpg_duration_17-1.0.2-1PGSTY.el9.aarch64.rpm
pg_duration_171.0.2el10.x86_64pigsty41.6 KiBpg_duration_17-1.0.2-1PGSTY.el10.x86_64.rpm
pg_duration_171.0.2el10.aarch64pigsty41.3 KiBpg_duration_17-1.0.2-1PGSTY.el10.aarch64.rpm
postgresql-17-pg-duration1.0.2d12.x86_64pigsty29.3 KiBpostgresql-17-pg-duration_1.0.2-1PGSTY~bookworm_amd64.deb
postgresql-17-pg-duration1.0.2d12.aarch64pigsty28.9 KiBpostgresql-17-pg-duration_1.0.2-1PGSTY~bookworm_arm64.deb
postgresql-17-pg-duration1.0.2d13.x86_64pigsty29.3 KiBpostgresql-17-pg-duration_1.0.2-1PGSTY~trixie_amd64.deb
postgresql-17-pg-duration1.0.2d13.aarch64pigsty29.0 KiBpostgresql-17-pg-duration_1.0.2-1PGSTY~trixie_arm64.deb
postgresql-17-pg-duration1.0.2u22.x86_64pigsty32.4 KiBpostgresql-17-pg-duration_1.0.2-1PGSTY~jammy_amd64.deb
postgresql-17-pg-duration1.0.2u22.aarch64pigsty31.5 KiBpostgresql-17-pg-duration_1.0.2-1PGSTY~jammy_arm64.deb
postgresql-17-pg-duration1.0.2u24.x86_64pigsty30.5 KiBpostgresql-17-pg-duration_1.0.2-1PGSTY~noble_amd64.deb
postgresql-17-pg-duration1.0.2u24.aarch64pigsty30.4 KiBpostgresql-17-pg-duration_1.0.2-1PGSTY~noble_arm64.deb
postgresql-17-pg-duration1.0.2u26.x86_64pigsty30.6 KiBpostgresql-17-pg-duration_1.0.2-1PGSTY~resolute_amd64.deb
postgresql-17-pg-duration1.0.2u26.aarch64pigsty30.1 KiBpostgresql-17-pg-duration_1.0.2-1PGSTY~resolute_arm64.deb

源码

pig build pkg pg_duration;		# build rpm/deb

安装

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

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

使用 pig 安装扩展。

pig install pg_duration;		# install by package name

pig install pg_duration -v 19;   # install for PG 19
pig install pg_duration -v 18;   # install for PG 18
pig install pg_duration -v 17;   # install for PG 17

在数据库中创建扩展。

CREATE EXTENSION pg_duration;

用法

pg_duration: PostgreSQL 的 ISO 8601 持续时间类型

pg_duration 扩展提供了一个 duration 类型,用 8 字节以微秒为单位存储经过时间,比内置的 interval 类型更简单且比较行为更一致。

CREATE EXTENSION pg_duration;

数据类型

duration 类型表示绝对经过时间,不包含日历组件(没有月或日)。有效输入接受任何不超过小时单位的 PostgreSQL interval 语法。

SELECT '01:30:00'::duration;
SELECT '2 hours 30 minutes'::duration;

运算符

  • 算术运算:duration 之间的 +、-;与 float8 的 *、/;一元 -
  • 比较运算:<、<=、>、>=、=、<>

函数

-- 从各组件构造
SELECT make_duration(hours => 2, mins => 30, secs => 15.5);

-- 检查是否有限
SELECT isfinite('01:00:00'::duration);

-- 截断到指定精度
SELECT date_trunc('hour', '02:45:30'::duration);

-- 提取子字段
SELECT date_part('minute', '02:45:30'::duration);
SELECT extract_duration('hour', '02:45:30'::duration);

类型转换

duration 可隐式转换为 interval。将 interval 转换为 duration 需要显式类型转换。

聚合与索引

支持标准聚合函数(avg、count、max、min、sum)以及 B-tree 和 Hash 索引。

表格阅读器