跳转到主要内容

pg_when

PostgreSQL 自然语言时间解析扩展

pg_when · PostgreSQL 自然语言时间解析扩展

概览

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

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY0.1.1119 18 17 16 15pg_when-
RPMPIGSTY0.1.1119 18 17 16 15pg_when_$v-
DEBPIGSTY0.1.1119 18 17 16 15postgresql-$v-pg-when-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.1.10PIGSTY 0.1.10PIGSTY 0.1.10PIGSTY 0.1.10
el8.aarch64N/APIGSTY 0.1.10PIGSTY 0.1.10PIGSTY 0.1.10PIGSTY 0.1.10
el9.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
el9.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
el10.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
el10.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
d12.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
d12.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
d13.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
d13.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u22.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u22.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u24.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u24.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u26.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u26.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
软件包版本平台仓库大小下载
pg_when_190.1.11el9.x86_64pigsty1.2 MiBpg_when_19-0.1.11-1PGSTY.el9.x86_64.rpm
pg_when_190.1.11el9.aarch64pigsty1.1 MiBpg_when_19-0.1.11-1PGSTY.el9.aarch64.rpm
pg_when_190.1.11el10.x86_64pigsty1.2 MiBpg_when_19-0.1.11-1PGSTY.el10.x86_64.rpm
pg_when_190.1.11el10.aarch64pigsty1.1 MiBpg_when_19-0.1.11-1PGSTY.el10.aarch64.rpm
postgresql-19-pg-when0.1.11d12.x86_64pigsty815.1 KiBpostgresql-19-pg-when_0.1.11-1PGSTY~bookworm_amd64.deb
postgresql-19-pg-when0.1.11d12.aarch64pigsty723.4 KiBpostgresql-19-pg-when_0.1.11-1PGSTY~bookworm_arm64.deb
postgresql-19-pg-when0.1.11d13.x86_64pigsty814.5 KiBpostgresql-19-pg-when_0.1.11-1PGSTY~trixie_amd64.deb
postgresql-19-pg-when0.1.11d13.aarch64pigsty723.8 KiBpostgresql-19-pg-when_0.1.11-1PGSTY~trixie_arm64.deb
postgresql-19-pg-when0.1.11u22.x86_64pigsty904.4 KiBpostgresql-19-pg-when_0.1.11-1PGSTY~jammy_amd64.deb
postgresql-19-pg-when0.1.11u22.aarch64pigsty852.4 KiBpostgresql-19-pg-when_0.1.11-1PGSTY~jammy_arm64.deb
postgresql-19-pg-when0.1.11u24.x86_64pigsty902.9 KiBpostgresql-19-pg-when_0.1.11-1PGSTY~noble_amd64.deb
postgresql-19-pg-when0.1.11u24.aarch64pigsty843.5 KiBpostgresql-19-pg-when_0.1.11-1PGSTY~noble_arm64.deb
postgresql-19-pg-when0.1.11u26.x86_64pigsty889.3 KiBpostgresql-19-pg-when_0.1.11-1PGSTY~resolute_amd64.deb
postgresql-19-pg-when0.1.11u26.aarch64pigsty841.3 KiBpostgresql-19-pg-when_0.1.11-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_when_180.1.10el8.x86_64pigsty1.0 MiBpg_when_18-0.1.10-1PGSTY.el8.x86_64.rpm
pg_when_180.1.10el8.aarch64pigsty973.7 KiBpg_when_18-0.1.10-1PGSTY.el8.aarch64.rpm
pg_when_180.1.11el9.x86_64pigsty1.2 MiBpg_when_18-0.1.11-1PGSTY.el9.x86_64.rpm
pg_when_180.1.11el9.aarch64pigsty1.1 MiBpg_when_18-0.1.11-1PGSTY.el9.aarch64.rpm
pg_when_180.1.11el10.x86_64pigsty1.2 MiBpg_when_18-0.1.11-1PGSTY.el10.x86_64.rpm
pg_when_180.1.11el10.aarch64pigsty1.1 MiBpg_when_18-0.1.11-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-when0.1.11d12.x86_64pigsty812.2 KiBpostgresql-18-pg-when_0.1.11-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-when0.1.11d12.aarch64pigsty724.3 KiBpostgresql-18-pg-when_0.1.11-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-when0.1.11d13.x86_64pigsty813.1 KiBpostgresql-18-pg-when_0.1.11-1PGSTY~trixie_amd64.deb
postgresql-18-pg-when0.1.11d13.aarch64pigsty724.4 KiBpostgresql-18-pg-when_0.1.11-1PGSTY~trixie_arm64.deb
postgresql-18-pg-when0.1.11u22.x86_64pigsty901.2 KiBpostgresql-18-pg-when_0.1.11-1PGSTY~jammy_amd64.deb
postgresql-18-pg-when0.1.11u22.aarch64pigsty850.9 KiBpostgresql-18-pg-when_0.1.11-1PGSTY~jammy_arm64.deb
postgresql-18-pg-when0.1.11u24.x86_64pigsty895.7 KiBpostgresql-18-pg-when_0.1.11-1PGSTY~noble_amd64.deb
postgresql-18-pg-when0.1.11u24.aarch64pigsty842.9 KiBpostgresql-18-pg-when_0.1.11-1PGSTY~noble_arm64.deb
postgresql-18-pg-when0.1.11u26.x86_64pigsty885.9 KiBpostgresql-18-pg-when_0.1.11-1PGSTY~resolute_amd64.deb
postgresql-18-pg-when0.1.11u26.aarch64pigsty839.0 KiBpostgresql-18-pg-when_0.1.11-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_when_170.1.10el8.x86_64pigsty1.0 MiBpg_when_17-0.1.10-1PGSTY.el8.x86_64.rpm
pg_when_170.1.10el8.aarch64pigsty970.8 KiBpg_when_17-0.1.10-1PGSTY.el8.aarch64.rpm
pg_when_170.1.11el9.x86_64pigsty1.1 MiBpg_when_17-0.1.11-1PGSTY.el9.x86_64.rpm
pg_when_170.1.11el9.aarch64pigsty1.1 MiBpg_when_17-0.1.11-1PGSTY.el9.aarch64.rpm
pg_when_170.1.11el10.x86_64pigsty1.1 MiBpg_when_17-0.1.11-1PGSTY.el10.x86_64.rpm
pg_when_170.1.11el10.aarch64pigsty1.1 MiBpg_when_17-0.1.11-1PGSTY.el10.aarch64.rpm
postgresql-17-pg-when0.1.11d12.x86_64pigsty809.8 KiBpostgresql-17-pg-when_0.1.11-1PGSTY~bookworm_amd64.deb
postgresql-17-pg-when0.1.11d12.aarch64pigsty722.5 KiBpostgresql-17-pg-when_0.1.11-1PGSTY~bookworm_arm64.deb
postgresql-17-pg-when0.1.11d13.x86_64pigsty810.0 KiBpostgresql-17-pg-when_0.1.11-1PGSTY~trixie_amd64.deb
postgresql-17-pg-when0.1.11d13.aarch64pigsty722.5 KiBpostgresql-17-pg-when_0.1.11-1PGSTY~trixie_arm64.deb
postgresql-17-pg-when0.1.11u22.x86_64pigsty898.6 KiBpostgresql-17-pg-when_0.1.11-1PGSTY~jammy_amd64.deb
postgresql-17-pg-when0.1.11u22.aarch64pigsty849.7 KiBpostgresql-17-pg-when_0.1.11-1PGSTY~jammy_arm64.deb
postgresql-17-pg-when0.1.11u24.x86_64pigsty894.2 KiBpostgresql-17-pg-when_0.1.11-1PGSTY~noble_amd64.deb
postgresql-17-pg-when0.1.11u24.aarch64pigsty840.8 KiBpostgresql-17-pg-when_0.1.11-1PGSTY~noble_arm64.deb
postgresql-17-pg-when0.1.11u26.x86_64pigsty883.4 KiBpostgresql-17-pg-when_0.1.11-1PGSTY~resolute_amd64.deb
postgresql-17-pg-when0.1.11u26.aarch64pigsty836.5 KiBpostgresql-17-pg-when_0.1.11-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_when_160.1.10el8.x86_64pigsty1.0 MiBpg_when_16-0.1.10-1PGSTY.el8.x86_64.rpm
pg_when_160.1.10el8.aarch64pigsty969.4 KiBpg_when_16-0.1.10-1PGSTY.el8.aarch64.rpm
pg_when_160.1.11el9.x86_64pigsty1.1 MiBpg_when_16-0.1.11-1PGSTY.el9.x86_64.rpm
pg_when_160.1.11el9.aarch64pigsty1.1 MiBpg_when_16-0.1.11-1PGSTY.el9.aarch64.rpm
pg_when_160.1.11el10.x86_64pigsty1.1 MiBpg_when_16-0.1.11-1PGSTY.el10.x86_64.rpm
pg_when_160.1.11el10.aarch64pigsty1.1 MiBpg_when_16-0.1.11-1PGSTY.el10.aarch64.rpm
postgresql-16-pg-when0.1.11d12.x86_64pigsty808.8 KiBpostgresql-16-pg-when_0.1.11-1PGSTY~bookworm_amd64.deb
postgresql-16-pg-when0.1.11d12.aarch64pigsty719.5 KiBpostgresql-16-pg-when_0.1.11-1PGSTY~bookworm_arm64.deb
postgresql-16-pg-when0.1.11d13.x86_64pigsty809.6 KiBpostgresql-16-pg-when_0.1.11-1PGSTY~trixie_amd64.deb
postgresql-16-pg-when0.1.11d13.aarch64pigsty719.4 KiBpostgresql-16-pg-when_0.1.11-1PGSTY~trixie_arm64.deb
postgresql-16-pg-when0.1.11u22.x86_64pigsty897.0 KiBpostgresql-16-pg-when_0.1.11-1PGSTY~jammy_amd64.deb
postgresql-16-pg-when0.1.11u22.aarch64pigsty848.2 KiBpostgresql-16-pg-when_0.1.11-1PGSTY~jammy_arm64.deb
postgresql-16-pg-when0.1.11u24.x86_64pigsty895.9 KiBpostgresql-16-pg-when_0.1.11-1PGSTY~noble_amd64.deb
postgresql-16-pg-when0.1.11u24.aarch64pigsty837.3 KiBpostgresql-16-pg-when_0.1.11-1PGSTY~noble_arm64.deb
postgresql-16-pg-when0.1.11u26.x86_64pigsty882.0 KiBpostgresql-16-pg-when_0.1.11-1PGSTY~resolute_amd64.deb
postgresql-16-pg-when0.1.11u26.aarch64pigsty835.2 KiBpostgresql-16-pg-when_0.1.11-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_when_150.1.10el8.x86_64pigsty1.0 MiBpg_when_15-0.1.10-1PGSTY.el8.x86_64.rpm
pg_when_150.1.10el8.aarch64pigsty960.2 KiBpg_when_15-0.1.10-1PGSTY.el8.aarch64.rpm
pg_when_150.1.11el9.x86_64pigsty1.1 MiBpg_when_15-0.1.11-1PGSTY.el9.x86_64.rpm
pg_when_150.1.11el9.aarch64pigsty1.1 MiBpg_when_15-0.1.11-1PGSTY.el9.aarch64.rpm
pg_when_150.1.11el10.x86_64pigsty1.1 MiBpg_when_15-0.1.11-1PGSTY.el10.x86_64.rpm
pg_when_150.1.11el10.aarch64pigsty1.1 MiBpg_when_15-0.1.11-1PGSTY.el10.aarch64.rpm
postgresql-15-pg-when0.1.11d12.x86_64pigsty805.9 KiBpostgresql-15-pg-when_0.1.11-1PGSTY~bookworm_amd64.deb
postgresql-15-pg-when0.1.11d12.aarch64pigsty719.1 KiBpostgresql-15-pg-when_0.1.11-1PGSTY~bookworm_arm64.deb
postgresql-15-pg-when0.1.11d13.x86_64pigsty807.1 KiBpostgresql-15-pg-when_0.1.11-1PGSTY~trixie_amd64.deb
postgresql-15-pg-when0.1.11d13.aarch64pigsty719.5 KiBpostgresql-15-pg-when_0.1.11-1PGSTY~trixie_arm64.deb
postgresql-15-pg-when0.1.11u22.x86_64pigsty896.1 KiBpostgresql-15-pg-when_0.1.11-1PGSTY~jammy_amd64.deb
postgresql-15-pg-when0.1.11u22.aarch64pigsty844.0 KiBpostgresql-15-pg-when_0.1.11-1PGSTY~jammy_arm64.deb
postgresql-15-pg-when0.1.11u24.x86_64pigsty886.1 KiBpostgresql-15-pg-when_0.1.11-1PGSTY~noble_amd64.deb
postgresql-15-pg-when0.1.11u24.aarch64pigsty835.4 KiBpostgresql-15-pg-when_0.1.11-1PGSTY~noble_arm64.deb
postgresql-15-pg-when0.1.11u26.x86_64pigsty879.8 KiBpostgresql-15-pg-when_0.1.11-1PGSTY~resolute_amd64.deb
postgresql-15-pg-when0.1.11u26.aarch64pigsty832.1 KiBpostgresql-15-pg-when_0.1.11-1PGSTY~resolute_arm64.deb

源码

pig build pkg pg_when;		# build rpm/deb

安装

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

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

使用 pig 安装扩展。

pig install pg_when;		# install by package name

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

在数据库中创建扩展。

CREATE EXTENSION pg_when;

用法

来源:

pg_when 0.1.11 解析受限的自然语言日期与时间表达式,返回 PostgreSQL timestamptz,或按指定精度返回 Unix epoch 值。

CREATE EXTENSION pg_when;

SELECT when_is('next friday at 8:00 pm in America/New_York');
SELECT seconds_at('5 days ago at this hour in Asia/Tokyo');
SELECT millis_at('in 2 months at midnight in UTC-8');
SELECT micros_at('December 31, 2026 at evening');
SELECT nanos_at('last monday at 22:30');

查询结构

查询可包含日期、时间和时区,并通过 at 与 in 连接:

SELECT when_is('<date> at <time> in <timezone>');
SELECT when_is('<date>');
SELECT when_is('<date> in <timezone>');
SELECT when_is('<time>');
SELECT when_is('<time> in <timezone>');
SELECT when_is('<date> at <time>');

省略时区时,解析器使用 UTC。支持的输入包括 tomorrow、last month、5 days ago 等相对日期,常见数字及月份名称形式的准确日期,noon、midnight、next hour 等相对时间,时钟时间、IANA 时区名与 UTC 偏移量。

函数索引

  • when_is(text) 返回 timestamptz。
  • seconds_at(text) 返回 Unix epoch 秒数。
  • millis_at(text) 返回 Unix epoch 毫秒数。
  • micros_at(text) 返回 Unix epoch 微秒数。
  • nanos_at(text) 返回 Unix epoch 纳秒数。

兼容性与边界

  • 解析器实现的是文档中定义的语法,并非通用自然语言解释器。
  • 上游 0.1.11 提供 PostgreSQL 13–19 的构建特性,并固定使用 pgrx 0.19.3。具体二进制软件包支持哪些服务器版本,取决于该包的实际构建。
  • pg_when 不可重定位,其 control 文件要求超级用户执行 CREATE EXTENSION。
  • 非法文本会触发错误。这五个函数都声明为 STRICT,因此空值输入返回空值;当 epoch 纳秒数无法放入 bigint 时,nanos_at(text) 也会报错。
  • 0.1.11 的 SQL 函数声明为 IMMUTABLE,但 now、tomorrow、5 days ago 等相对表达式会读取当前时钟。不要把相对输入调用用于表达式索引或生成列,也不要假定它们会在缓存计划中重新求值;只有完整指定日期、时间与时区的输入才与当前时间无关。

表格阅读器