pg_cron

Job scheduler for PostgreSQL

Overview

PGDG 1st Party Extension: pg_cron : Job scheduler for PostgreSQL

Information

Metadata

  • Latest Version: 1.6
  • Postgres Support: 17,16,15,14,13
  • Need Load: Explicit Loading Required
  • Need DDL: Need CREATE EXTENSION DDL
  • Relocatable: Can be installed into other schemas
  • Trusted: Untrusted, Require Superuser to Create
  • Schemas: pg_catalog
  • Requires: N/A

RPM / DEB

  • RPM Repo: PGDG
  • RPM Name: pg_cron_$v*
  • RPM Ver : 1.6
  • RPM Deps: N/A
  • DEB Repo: PGDG
  • DEB Name: postgresql-$v-cron
  • DEB Ver : 1.6
  • DEB Deps: N/A

Packages

OS Arch PG17 PG16 PG15 PG14 PG13
el8 x86_64 pg_cron_17
PGDG 1.6.5
pg_cron_16
PGDG 1.6.5
pg_cron_15
PGDG 1.6.5
pg_cron_14
PGDG 1.6.5
pg_cron_13
PGDG 1.6.5
el8 aarch64 pg_cron_17
PGDG 1.6.5
pg_cron_16
PGDG 1.6.5
pg_cron_15
PGDG 1.6.5
pg_cron_14
PGDG 1.6.5
pg_cron_13
PGDG 1.6.5
el9 x86_64 pg_cron_17
PGDG 1.6.5
pg_cron_16
PGDG 1.6.5
pg_cron_15
PGDG 1.6.5
pg_cron_14
PGDG 1.6.5
pg_cron_13
PGDG 1.6.5
el9 aarch64 pg_cron_17
PGDG 1.6.5
pg_cron_16
PGDG 1.6.5
pg_cron_15
PGDG 1.6.5
pg_cron_14
PGDG 1.6.5
pg_cron_13
PGDG 1.6.5
d12 x86_64 postgresql-17-cron
PGDG 1.6.5
postgresql-16-cron
PGDG 1.6.5
postgresql-15-cron
PGDG 1.6.5
postgresql-14-cron
PGDG 1.6.5
postgresql-13-cron
PGDG 1.6.5
d12 aarch64 postgresql-17-cron
PGDG 1.6.5
postgresql-16-cron
PGDG 1.6.5
postgresql-15-cron
PGDG 1.6.5
postgresql-14-cron
PGDG 1.6.5
postgresql-13-cron
PGDG 1.6.5
u22 x86_64 postgresql-17-cron
PGDG 1.6.5
postgresql-16-cron
PGDG 1.6.5
postgresql-15-cron
PGDG 1.6.5
postgresql-14-cron
PGDG 1.6.5
postgresql-13-cron
PGDG 1.6.5
u22 aarch64 postgresql-17-cron
PGDG 1.6.5
postgresql-16-cron
PGDG 1.6.5
postgresql-15-cron
PGDG 1.6.5
postgresql-14-cron
PGDG 1.6.5
postgresql-13-cron
PGDG 1.6.5
u24 x86_64 postgresql-17-cron
PGDG 1.6.5
postgresql-16-cron
PGDG 1.6.5
postgresql-15-cron
PGDG 1.6.5
postgresql-14-cron
PGDG 1.6.5
postgresql-13-cron
PGDG 1.6.5
u24 aarch64 postgresql-17-cron
PGDG 1.6.5
postgresql-16-cron
PGDG 1.6.5
postgresql-15-cron
PGDG 1.6.5
postgresql-14-cron
PGDG 1.6.5
postgresql-13-cron
PGDG 1.6.5

Installation

Install pg_cron via the pig CLI tool:

pig ext install pg_cron

Install pg_cron via Pigsty playbook:

./pgsql.yml -t pg_extension -e '{"pg_extensions": ["pg_cron"]}' # -l <cls>

Install pg_cron RPM from YUM repo directly:

dnf install pg_cron_17*;
dnf install pg_cron_16*;
dnf install pg_cron_15*;
dnf install pg_cron_14*;
dnf install pg_cron_13*;

Install pg_cron DEB from APT repo directly:

apt install postgresql-17-cron;
apt install postgresql-16-cron;
apt install postgresql-15-cron;
apt install postgresql-14-cron;
apt install postgresql-13-cron;

Extension pg_cron has to be added to shared_preload_libraries

shared_preload_libraries = 'pg_cron'; # add to pg cluster config

Enable pg_cron extension on PostgreSQL cluster:

CREATE EXTENSION pg_cron;

Usage

beware that cron.database has to be set before adding to shared_preload_libraries

-- Delete old data on Saturday at 3:30am (GMT)
SELECT cron.schedule('30 3 * * 6', $$DELETE FROM events WHERE event_time < now() - interval '1 week'$$);
 schedule
----------
       42

-- Vacuum every day at 10:00am (GMT)
SELECT cron.schedule('nightly-vacuum', '0 10 * * *', 'VACUUM');
 schedule
----------
       43

-- Change to vacuum at 3:00am (GMT)
SELECT cron.schedule('nightly-vacuum', '0 3 * * *', 'VACUUM');
 schedule
----------
       43

-- Stop scheduling jobs
SELECT cron.unschedule('nightly-vacuum' );
 unschedule 
------------
 t

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

-- Vacuum every Sunday at 4:00am (GMT) in a database other than the one pg_cron is installed in
SELECT cron.schedule_in_database('weekly-vacuum', '0 4 * * 0', 'VACUUM', 'some_other_database');
 schedule
----------
       44

-- Call a stored procedure every 5 seconds
SELECT cron.schedule('process-updates', '5 seconds', 'CALL process_updates()');

-- Process payroll at 12:00 of the last day of each month
SELECT cron.schedule('process-payroll', '0 12 $ * *', 'CALL process_payroll()');

Crontab format:

 ┌───────────── min (0 - 59)
 │ ┌────────────── hour (0 - 23)
 │ │ ┌─────────────── day of month (1 - 31) or last day of the month ($)
 │ │ │ ┌──────────────── month (1 - 12)
 │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to
 │ │ │ │ │                  Saturday, or use names; 7 is also Sunday)
 │ │ │ │ │
 │ │ │ │ │
 * * * * *




Last modified 2025-02-17: add extension part (cfa504b)