All Projects → citusdata → Pg_cron

citusdata / Pg_cron

Licence: postgresql
Run periodic jobs in PostgreSQL

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Pg cron

linda
Linda is a simple dispatcher library.
Stars: ✭ 12 (-98.8%)
Mutual labels:  cron, scheduler
Gocron
定时任务管理系统
Stars: ✭ 4,198 (+318.96%)
Mutual labels:  scheduler, cron
php-cron-expr
Ultra lightweight, Dependency free and Super Fast Cron Expression parser for PHP
Stars: ✭ 42 (-95.81%)
Mutual labels:  cron, scheduler
Cronicle
A simple, distributed task scheduler and runner with a web based UI.
Stars: ✭ 979 (-2.3%)
Mutual labels:  scheduler, cron
Gocron
Easy and fluent Go cron scheduling. This is a fork from https://github.com/jasonlvhit/gocron
Stars: ✭ 605 (-39.62%)
Mutual labels:  scheduler, cron
scheduler
Task Scheduler for Laravel applications. UI from scratch
Stars: ✭ 18 (-98.2%)
Mutual labels:  cron, scheduler
Deno cron
A cron Job scheduler for Deno that allows you to write human readable cron syntax with tons of flexibility
Stars: ✭ 35 (-96.51%)
Mutual labels:  scheduler, cron
rhythm
Time-based job scheduler for Apache Mesos
Stars: ✭ 30 (-97.01%)
Mutual labels:  cron, scheduler
Wecron
✔️ 微信上的定时提醒 - Cron on WeChat
Stars: ✭ 537 (-46.41%)
Mutual labels:  cron, postgresql
Quartznet
Quartz Enterprise Scheduler .NET
Stars: ✭ 4,825 (+381.54%)
Mutual labels:  scheduler, cron
nodejs-cron-job-must-know
it is an example of running node.js script with every certain period(cron job)
Stars: ✭ 35 (-96.51%)
Mutual labels:  cron, scheduler
Bree
🚥 The best job scheduler for Node.js and JavaScript with cron, dates, ms, later, and human-friendly support. Works in Node v10+ and browsers, uses workers to spawn sandboxed processes, and supports async/await, retries, throttling, concurrency, and graceful shutdown. Simple, fast, and lightweight. Made for @ForwardEmail and @ladjs.
Stars: ✭ 933 (-6.89%)
Mutual labels:  scheduler, cron
ckron
🐋 A cron-like job scheduler for docker
Stars: ✭ 37 (-96.31%)
Mutual labels:  cron, scheduler
asparagus
An easy to use task scheduler for distributed systems
Stars: ✭ 14 (-98.6%)
Mutual labels:  cron, scheduler
Automation-using-Shell-Scripts
Development Automation using Shell Scripting.
Stars: ✭ 41 (-95.91%)
Mutual labels:  cron, scheduler
josk
🏃🤖 Scheduler and manager for jobs and tasks in node.js on multi-server and clusters setup
Stars: ✭ 27 (-97.31%)
Mutual labels:  cron, scheduler
time.clj
time util for Clojure(Script)
Stars: ✭ 45 (-95.51%)
Mutual labels:  cron, scheduler
transferwisely
Batch process using transfer-wise API to automatically track, detect and book transfers for you at better rates.
Stars: ✭ 20 (-98%)
Mutual labels:  cron, scheduler
Pg timetable
pg_timetable: Advanced scheduling for PostgreSQL
Stars: ✭ 382 (-61.88%)
Mutual labels:  cron, postgresql
Crono
A time-based background job scheduler daemon (just like Cron) for Rails
Stars: ✭ 637 (-36.43%)
Mutual labels:  scheduler, cron

Citus Banner

Slack Status

What is pg_cron?

pg_cron is a simple cron-based job scheduler for PostgreSQL (9.5 or higher) that runs inside the database as an extension. It uses the same syntax as regular cron, but it allows you to schedule PostgreSQL commands directly from the database:

-- 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
(1 row)

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

pg_cron can run multiple jobs in parallel, but it runs at most one instance of a job at a time. If a second run is supposed to start before the first one finishes, then the second run is queued and started as soon as the first run completes.

The schedule uses the standard cron syntax, in which * means "run every time period", and a specific number means "but only at this time":

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

An easy way to create a cron schedule is: crontab.guru.

The code in pg_cron that handles parsing and scheduling comes directly from the cron source code by Paul Vixie, hence the same options are supported. Be aware that pg_cron always uses GMT!

Installing pg_cron

Install on Red Hat, CentOS, Fedora, Amazon Linux with PostgreSQL 12 using PGDG:

# Install the pg_cron extension
sudo yum install -y pg_cron_12

Install on Debian, Ubuntu with PostgreSQL 12 using apt.postgresql.org:

# Install the pg_cron extension
sudo apt-get -y install postgresql-12-cron

You can also install pg_cron by building it from source:

git clone https://github.com/citusdata/pg_cron.git
cd pg_cron
# Ensure pg_config is in your path, e.g.
export PATH=/usr/pgsql-12/bin:$PATH
make && sudo PATH=$PATH make install

Setting up pg_cron

To start the pg_cron background worker when PostgreSQL starts, you need to add pg_cron to shared_preload_libraries in postgresql.conf. Note that pg_cron does not run any jobs as a long a server is in hot standby mode, but it automatically starts when the server is promoted.

By default, the pg_cron background worker expects its metadata tables to be created in the "postgres" database. However, you can configure this by setting the cron.database_name configuration parameter in postgresql.conf.

# add to postgresql.conf:
shared_preload_libraries = 'pg_cron'
cron.database_name = 'postgres'

After restarting PostgreSQL, you can create the pg_cron functions and metadata tables using CREATE EXTENSION pg_cron.

-- run as superuser:
CREATE EXTENSION pg_cron;

-- optionally, grant usage to regular users:
GRANT USAGE ON SCHEMA cron TO marco;

Important: Internally, pg_cron uses libpq to open a new connection to the local database. It may be necessary to enable trust authentication for connections coming from localhost in pg_hba.conf for the user running the cron job. Alternatively, you can add the password to a .pgpass file, which libpq will use when opening a connection.

For security, jobs are executed in the database in which the cron.schedule function is called with the same permissions as the current user. In addition, users are only able to see their own jobs in the cron.job table.

Example use cases

Articles showing possible ways of using pg_cron:

Managed services

The following table keeps track of which of the major managed Postgres services support pg_cron.

Service Supported
Alibaba Cloud ✔️
Amazon RDS ✔️
Azure ✔️
Citus Cloud ✔️
Crunchy Bridge ✔️
DigitalOcean ✔️
Google Cloud
Heroku
Supabase ✔️
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].