pg_cron

定时任务调度器

概览

扩展包名版本分类许可证语言
pg_cron1.6.7TIMEPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
1070pg_cronpg_catalog
相关扩展timescaledb_toolkit timescaledb periods temporal_tables pg_task pg_later emaj table_version
下游依赖documentdb pg_incremental timeseries vectorize pgmb

require cron.database_name

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG1.6.71817161514pg_cron-
RPMPGDG1.6.71817161514pg_cron_$v-
DEBPGDG1.6.71817161514postgresql-$v-cron-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
d12.aarch64
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
d13.x86_64
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
d13.aarch64
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
PGDG 1.6.7
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64

安装

您可以直接安装 pg_cron 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:

pig repo add pgdg -u          # 添加 PGDG 仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install pg_cron;          # 当前活跃 PG 版本安装
pig ext install -y pg_cron -v 18  # PG 18
pig ext install -y pg_cron -v 17  # PG 17
pig ext install -y pg_cron -v 16  # PG 16
pig ext install -y pg_cron -v 15  # PG 15
pig ext install -y pg_cron -v 14  # PG 14
dnf install -y pg_cron_18       # PG 18
dnf install -y pg_cron_17       # PG 17
dnf install -y pg_cron_16       # PG 16
dnf install -y pg_cron_15       # PG 15
dnf install -y pg_cron_14       # PG 14
apt install -y postgresql-18-cron   # PG 18
apt install -y postgresql-17-cron   # PG 17
apt install -y postgresql-16-cron   # PG 16
apt install -y postgresql-15-cron   # PG 15
apt install -y postgresql-14-cron   # PG 14

预加载配置

shared_preload_libraries = 'pg_cron';

创建扩展

CREATE EXTENSION pg_cron;

用法

请注意,cron.database 必须在将 pg_cron 添加到 shared_preload_libraries 之前设置好。

-- 每周六凌晨 3:30(GMT)删除过期数据
SELECT cron.schedule('30 3 * * 6', $$DELETE FROM events WHERE event_time < now() - interval '1 week'$$);
 schedule
----------
       42

-- 每天上午 10:00(GMT)执行 VACUUM
SELECT cron.schedule('nightly-vacuum', '0 10 * * *', 'VACUUM');
 schedule
----------
       43

-- 改为每天凌晨 3:00(GMT)执行 VACUUM
SELECT cron.schedule('nightly-vacuum', '0 3 * * *', 'VACUUM');
 schedule
----------
       43

-- 取消定时任务
SELECT cron.unschedule('nightly-vacuum' );
 unschedule
------------
 t

SELECT cron.unschedule(42);
 unschedule
------------
          t

-- 每周日凌晨 4:00(GMT)在 pg_cron 所在数据库之外的其他数据库中执行 VACUUM
SELECT cron.schedule_in_database('weekly-vacuum', '0 4 * * 0', 'VACUUM', 'some_other_database');
 schedule
----------
       44

-- 每 5 秒调用一次存储过程
SELECT cron.schedule('process-updates', '5 seconds', 'CALL process_updates()');

-- 每月最后一天中午 12:00 执行工资处理
SELECT cron.schedule('process-payroll', '0 12 $ * *', 'CALL process_payroll()');

Crontab 格式说明:

 ┌───────────── 分钟 (0 - 59)
 │ ┌────────────── 小时 (0 - 23)
 │ │ ┌─────────────── 日期 (1 - 31) 或月末最后一天 ($)
 │ │ │ ┌──────────────── 月份 (1 - 12)
 │ │ │ │ ┌───────────────── 星期 (0 - 6)(0 到 6 表示周日到
 │ │ │ │ │                  周六,也可使用英文名称;7 同样表示周日)
 │ │ │ │ │
 │ │ │ │ │
 * * * * *

最后修改 2026-03-08: add extension catalog (baacba6)