跳转到主要内容

cron_utils

解析 Cron 表达式并计算上一次或下一次触发时间

cron_utils · 解析 Cron 表达式并计算上一次或下一次触发时间

概览

ID扩展软件包版本类别许可证编程语言
1150cron_utilscron_utils0.1.0TIMEMITSQL
属性可执行文件动态库需要预加载需要建扩展可迁移可信
----d-r否否否是是否
The PGXN 0.1.0 distribution is marked unstable; the control file marks the extension relocatable.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY0.1.019 18 17 16 15cron_utils-
RPMPIGSTY0.1.019 18 17 16 15cron_utils_$v-
DEBPIGSTY0.1.019 18 17 16 15postgresql-$v-cron-utils-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el8.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el9.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el9.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el10.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el10.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d12.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d12.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d13.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d13.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u22.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u22.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u24.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u24.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u26.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u26.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
软件包版本平台仓库大小下载
cron_utils_190.1.0el9.x86_64pigsty11.5 KiBcron_utils_19-0.1.0-1PGSTY.el9.noarch.rpm
cron_utils_190.1.0el9.aarch64pigsty11.5 KiBcron_utils_19-0.1.0-1PGSTY.el9.noarch.rpm
cron_utils_190.1.0el10.x86_64pigsty11.6 KiBcron_utils_19-0.1.0-1PGSTY.el10.noarch.rpm
cron_utils_190.1.0el10.aarch64pigsty11.6 KiBcron_utils_19-0.1.0-1PGSTY.el10.noarch.rpm
postgresql-19-cron-utils0.1.0d12.x86_64pigsty5.6 KiBpostgresql-19-cron-utils_0.1.0-1PGSTY~bookworm_all.deb
postgresql-19-cron-utils0.1.0d12.aarch64pigsty5.6 KiBpostgresql-19-cron-utils_0.1.0-1PGSTY~bookworm_all.deb
postgresql-19-cron-utils0.1.0d13.x86_64pigsty5.6 KiBpostgresql-19-cron-utils_0.1.0-1PGSTY~trixie_all.deb
postgresql-19-cron-utils0.1.0d13.aarch64pigsty5.6 KiBpostgresql-19-cron-utils_0.1.0-1PGSTY~trixie_all.deb
postgresql-19-cron-utils0.1.0u22.x86_64pigsty5.6 KiBpostgresql-19-cron-utils_0.1.0-1PGSTY~jammy_all.deb
postgresql-19-cron-utils0.1.0u22.aarch64pigsty5.6 KiBpostgresql-19-cron-utils_0.1.0-1PGSTY~jammy_all.deb
postgresql-19-cron-utils0.1.0u24.x86_64pigsty5.6 KiBpostgresql-19-cron-utils_0.1.0-1PGSTY~noble_all.deb
postgresql-19-cron-utils0.1.0u24.aarch64pigsty5.6 KiBpostgresql-19-cron-utils_0.1.0-1PGSTY~noble_all.deb
postgresql-19-cron-utils0.1.0u26.x86_64pigsty5.6 KiBpostgresql-19-cron-utils_0.1.0-1PGSTY~resolute_all.deb
postgresql-19-cron-utils0.1.0u26.aarch64pigsty5.6 KiBpostgresql-19-cron-utils_0.1.0-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
cron_utils_180.1.0el8.x86_64pigsty11.5 KiBcron_utils_18-0.1.0-1PGSTY.el8.noarch.rpm
cron_utils_180.1.0el8.aarch64pigsty11.5 KiBcron_utils_18-0.1.0-1PGSTY.el8.noarch.rpm
cron_utils_180.1.0el9.x86_64pigsty11.5 KiBcron_utils_18-0.1.0-1PGSTY.el9.noarch.rpm
cron_utils_180.1.0el9.aarch64pigsty11.5 KiBcron_utils_18-0.1.0-1PGSTY.el9.noarch.rpm
cron_utils_180.1.0el10.x86_64pigsty11.6 KiBcron_utils_18-0.1.0-1PGSTY.el10.noarch.rpm
cron_utils_180.1.0el10.aarch64pigsty11.6 KiBcron_utils_18-0.1.0-1PGSTY.el10.noarch.rpm
postgresql-18-cron-utils0.1.0d12.x86_64pigsty5.6 KiBpostgresql-18-cron-utils_0.1.0-1PGSTY~bookworm_all.deb
postgresql-18-cron-utils0.1.0d12.aarch64pigsty5.6 KiBpostgresql-18-cron-utils_0.1.0-1PGSTY~bookworm_all.deb
postgresql-18-cron-utils0.1.0d13.x86_64pigsty5.6 KiBpostgresql-18-cron-utils_0.1.0-1PGSTY~trixie_all.deb
postgresql-18-cron-utils0.1.0d13.aarch64pigsty5.6 KiBpostgresql-18-cron-utils_0.1.0-1PGSTY~trixie_all.deb
postgresql-18-cron-utils0.1.0u22.x86_64pigsty5.6 KiBpostgresql-18-cron-utils_0.1.0-1PGSTY~jammy_all.deb
postgresql-18-cron-utils0.1.0u22.aarch64pigsty5.6 KiBpostgresql-18-cron-utils_0.1.0-1PGSTY~jammy_all.deb
postgresql-18-cron-utils0.1.0u24.x86_64pigsty5.6 KiBpostgresql-18-cron-utils_0.1.0-1PGSTY~noble_all.deb
postgresql-18-cron-utils0.1.0u24.aarch64pigsty5.6 KiBpostgresql-18-cron-utils_0.1.0-1PGSTY~noble_all.deb
postgresql-18-cron-utils0.1.0u26.x86_64pigsty5.6 KiBpostgresql-18-cron-utils_0.1.0-1PGSTY~resolute_all.deb
postgresql-18-cron-utils0.1.0u26.aarch64pigsty5.6 KiBpostgresql-18-cron-utils_0.1.0-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
cron_utils_170.1.0el8.x86_64pigsty11.5 KiBcron_utils_17-0.1.0-1PGSTY.el8.noarch.rpm
cron_utils_170.1.0el8.aarch64pigsty11.5 KiBcron_utils_17-0.1.0-1PGSTY.el8.noarch.rpm
cron_utils_170.1.0el9.x86_64pigsty11.5 KiBcron_utils_17-0.1.0-1PGSTY.el9.noarch.rpm
cron_utils_170.1.0el9.aarch64pigsty11.5 KiBcron_utils_17-0.1.0-1PGSTY.el9.noarch.rpm
cron_utils_170.1.0el10.x86_64pigsty11.6 KiBcron_utils_17-0.1.0-1PGSTY.el10.noarch.rpm
cron_utils_170.1.0el10.aarch64pigsty11.6 KiBcron_utils_17-0.1.0-1PGSTY.el10.noarch.rpm
postgresql-17-cron-utils0.1.0d12.x86_64pigsty5.6 KiBpostgresql-17-cron-utils_0.1.0-1PGSTY~bookworm_all.deb
postgresql-17-cron-utils0.1.0d12.aarch64pigsty5.6 KiBpostgresql-17-cron-utils_0.1.0-1PGSTY~bookworm_all.deb
postgresql-17-cron-utils0.1.0d13.x86_64pigsty5.6 KiBpostgresql-17-cron-utils_0.1.0-1PGSTY~trixie_all.deb
postgresql-17-cron-utils0.1.0d13.aarch64pigsty5.6 KiBpostgresql-17-cron-utils_0.1.0-1PGSTY~trixie_all.deb
postgresql-17-cron-utils0.1.0u22.x86_64pigsty5.6 KiBpostgresql-17-cron-utils_0.1.0-1PGSTY~jammy_all.deb
postgresql-17-cron-utils0.1.0u22.aarch64pigsty5.6 KiBpostgresql-17-cron-utils_0.1.0-1PGSTY~jammy_all.deb
postgresql-17-cron-utils0.1.0u24.x86_64pigsty5.6 KiBpostgresql-17-cron-utils_0.1.0-1PGSTY~noble_all.deb
postgresql-17-cron-utils0.1.0u24.aarch64pigsty5.6 KiBpostgresql-17-cron-utils_0.1.0-1PGSTY~noble_all.deb
postgresql-17-cron-utils0.1.0u26.x86_64pigsty5.6 KiBpostgresql-17-cron-utils_0.1.0-1PGSTY~resolute_all.deb
postgresql-17-cron-utils0.1.0u26.aarch64pigsty5.6 KiBpostgresql-17-cron-utils_0.1.0-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
cron_utils_160.1.0el8.x86_64pigsty11.5 KiBcron_utils_16-0.1.0-1PGSTY.el8.noarch.rpm
cron_utils_160.1.0el8.aarch64pigsty11.5 KiBcron_utils_16-0.1.0-1PGSTY.el8.noarch.rpm
cron_utils_160.1.0el9.x86_64pigsty11.5 KiBcron_utils_16-0.1.0-1PGSTY.el9.noarch.rpm
cron_utils_160.1.0el9.aarch64pigsty11.5 KiBcron_utils_16-0.1.0-1PGSTY.el9.noarch.rpm
cron_utils_160.1.0el10.x86_64pigsty11.6 KiBcron_utils_16-0.1.0-1PGSTY.el10.noarch.rpm
cron_utils_160.1.0el10.aarch64pigsty11.6 KiBcron_utils_16-0.1.0-1PGSTY.el10.noarch.rpm
postgresql-16-cron-utils0.1.0d12.x86_64pigsty5.6 KiBpostgresql-16-cron-utils_0.1.0-1PGSTY~bookworm_all.deb
postgresql-16-cron-utils0.1.0d12.aarch64pigsty5.6 KiBpostgresql-16-cron-utils_0.1.0-1PGSTY~bookworm_all.deb
postgresql-16-cron-utils0.1.0d13.x86_64pigsty5.6 KiBpostgresql-16-cron-utils_0.1.0-1PGSTY~trixie_all.deb
postgresql-16-cron-utils0.1.0d13.aarch64pigsty5.6 KiBpostgresql-16-cron-utils_0.1.0-1PGSTY~trixie_all.deb
postgresql-16-cron-utils0.1.0u22.x86_64pigsty5.6 KiBpostgresql-16-cron-utils_0.1.0-1PGSTY~jammy_all.deb
postgresql-16-cron-utils0.1.0u22.aarch64pigsty5.6 KiBpostgresql-16-cron-utils_0.1.0-1PGSTY~jammy_all.deb
postgresql-16-cron-utils0.1.0u24.x86_64pigsty5.6 KiBpostgresql-16-cron-utils_0.1.0-1PGSTY~noble_all.deb
postgresql-16-cron-utils0.1.0u24.aarch64pigsty5.6 KiBpostgresql-16-cron-utils_0.1.0-1PGSTY~noble_all.deb
postgresql-16-cron-utils0.1.0u26.x86_64pigsty5.6 KiBpostgresql-16-cron-utils_0.1.0-1PGSTY~resolute_all.deb
postgresql-16-cron-utils0.1.0u26.aarch64pigsty5.6 KiBpostgresql-16-cron-utils_0.1.0-1PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
cron_utils_150.1.0el8.x86_64pigsty11.5 KiBcron_utils_15-0.1.0-1PGSTY.el8.noarch.rpm
cron_utils_150.1.0el8.aarch64pigsty11.5 KiBcron_utils_15-0.1.0-1PGSTY.el8.noarch.rpm
cron_utils_150.1.0el9.x86_64pigsty11.5 KiBcron_utils_15-0.1.0-1PGSTY.el9.noarch.rpm
cron_utils_150.1.0el9.aarch64pigsty11.5 KiBcron_utils_15-0.1.0-1PGSTY.el9.noarch.rpm
cron_utils_150.1.0el10.x86_64pigsty11.6 KiBcron_utils_15-0.1.0-1PGSTY.el10.noarch.rpm
cron_utils_150.1.0el10.aarch64pigsty11.6 KiBcron_utils_15-0.1.0-1PGSTY.el10.noarch.rpm
postgresql-15-cron-utils0.1.0d12.x86_64pigsty5.6 KiBpostgresql-15-cron-utils_0.1.0-1PGSTY~bookworm_all.deb
postgresql-15-cron-utils0.1.0d12.aarch64pigsty5.6 KiBpostgresql-15-cron-utils_0.1.0-1PGSTY~bookworm_all.deb
postgresql-15-cron-utils0.1.0d13.x86_64pigsty5.6 KiBpostgresql-15-cron-utils_0.1.0-1PGSTY~trixie_all.deb
postgresql-15-cron-utils0.1.0d13.aarch64pigsty5.6 KiBpostgresql-15-cron-utils_0.1.0-1PGSTY~trixie_all.deb
postgresql-15-cron-utils0.1.0u22.x86_64pigsty5.6 KiBpostgresql-15-cron-utils_0.1.0-1PGSTY~jammy_all.deb
postgresql-15-cron-utils0.1.0u22.aarch64pigsty5.6 KiBpostgresql-15-cron-utils_0.1.0-1PGSTY~jammy_all.deb
postgresql-15-cron-utils0.1.0u24.x86_64pigsty5.6 KiBpostgresql-15-cron-utils_0.1.0-1PGSTY~noble_all.deb
postgresql-15-cron-utils0.1.0u24.aarch64pigsty5.6 KiBpostgresql-15-cron-utils_0.1.0-1PGSTY~noble_all.deb
postgresql-15-cron-utils0.1.0u26.x86_64pigsty5.6 KiBpostgresql-15-cron-utils_0.1.0-1PGSTY~resolute_all.deb
postgresql-15-cron-utils0.1.0u26.aarch64pigsty5.6 KiBpostgresql-15-cron-utils_0.1.0-1PGSTY~resolute_all.deb

源码

pig build pkg cron_utils;		# build rpm/deb

安装

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

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

使用 pig 安装扩展。

pig install cron_utils;		# install by package name

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

在数据库中创建扩展。

CREATE EXTENSION cron_utils;

用法

来源:

cron_utils 解析五字段 cron 表达式并在 PostgreSQL 中计算触发时间戳。它是一个调度工具,而不是作业执行器:使用它可以预览、验证或查询一个计划,并单独使用 pg_cron 等调度程序来执行工作。

核心流程

CREATE EXTENSION cron_utils;

-- First trigger at or after the supplied time.
SELECT cron_first_trigger('0 9 * * 1-5', now());

-- Last trigger before the supplied time (strict is true by default).
SELECT cron_last_trigger('0 9 * * 1-5', now());

-- Next five hourly triggers.
SELECT *
FROM cron_iterate_n('0 * * * *', now(), false, 'next', 5);

要检查一个限定窗口:

SELECT *
FROM cron_first_last_triggers(
  '0 0 * * *',
  date_trunc('month', now()),
  date_trunc('month', now()) + interval '1 month'
);

当表达式在窗口中没有触发时,返回的边界可以是 NULL。

重要对象

  • cron_parts 是分钟、小时、日、月和周字段的解析表示。
  • parse_cron(text) 解析 *、列表、范围以及步进语法。
  • cron_first_trigger(expr, base_time, strict) 向前搜索。当 strict = true 时,会跳过在 base_time 精确匹配的情况。
  • cron_last_trigger(expr, base_time, strict) 向后搜索,默认为严格匹配。
  • cron_first_last_triggers(expr, start_time, end_time) 返回窗口中的第一个和最后一个匹配项。
  • cron_iterate_n(expr, base_time, strict, direction, max_matches) 按 next 或 prev 方向返回连续的匹配项。

语义与注意事项

表达式使用标准的五个字段 minute hour day month dow;秒不被接受。周字段使用 1 = Monday 到 7 = Sunday 的表示方式。结果为 timestamptz,因此会话时区会影响显示的本地时间,并且对于本地时间计划应测试夏令时转换。

该扩展是纯 SQL/PL/pgSQL 实现的,可重定位且没有依赖于 pg_cron。其函数声明为不可变且并行安全。0.1.0 版本在控制元数据中标记为 unstable,因此在将其嵌入关键调度程序之前,请锁定行为并重新测试边缘情况。

表格阅读器