pgcron doc#
Pg_cron āĻāĻāĻāĻŋ cron-based job scheduler āĻ¯āĻž PostgreSQL(9.5 or higher) āĻāĻ° āĻā§āĻ¤āĻ° āĻāĻāĻāĻŋ Database extension āĻšāĻŋāĻ¸ā§āĻŦā§ run āĻāĻ°ā§āĨ¤ Pg_cron āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°ā§ specific āĻ¸āĻŽā§ āĻĒāĻ° āĻĒāĻ° āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ sql query āĻā§ postgresql āĻ execute āĻāĻ°āĻž āĻ¯āĻžā§āĨ¤
for an example application āĻāĻ° people suggation portion āĻĨā§āĻā§ user1 āĻ¯āĻĻāĻŋ user13 āĻā§ unmatch āĻāĻ°ā§, āĻ¤āĻŦā§ user1 āĻ¤āĻžāĻ° suggestion portion āĻāĻŦāĻ search people portion āĻ user13 āĻā§ āĻĻā§āĻāĻ¤ā§ āĻĒāĻžāĻŦā§ āĻ¨āĻž āĻāĻŦāĻ user13 āĻ āĻ¤āĻžāĻ° suggestion portion āĻāĻŦāĻ search people portion āĻ user1 āĻā§ āĻĻā§āĻāĻ¤ā§ āĻĒāĻžāĻŦā§ āĻ¨āĻžāĨ¤
According to business case, unmatched āĻāĻ°āĻž users āĻā§āĻ˛ā§ 40 days āĻāĻ° āĻŦā§āĻļāĻŋ unmatched āĻĨāĻžāĻāĻŦā§ āĻ¨āĻžāĨ¤ āĻ¤āĻžāĻ°āĻž āĻāĻŦāĻžāĻ° suggestion āĻā§āĻŦāĻŋāĻ˛ āĻāĻŦāĻ search people portion āĻ seen āĻšāĻŦā§āĨ¤
āĻ¯ā§ āĻ¸āĻŦ user āĻāĻ° unmatched date ā§Ēā§Ļ āĻĻāĻŋāĻ¨ āĻšā§ā§āĻā§ āĻ¤āĻžāĻĻā§āĻ°āĻā§ unmatch table āĻĨā§āĻā§ remove āĻāĻ°ā§ āĻĻā§ā§āĻžāĻ° opperation āĻāĻŋ postgreSQL āĻāĻ° under āĻ āĻ¸āĻŽā§āĻĒā§āĻ°ā§āĻŖ āĻšā§āĨ¤ āĻĒā§āĻ°āĻ¤āĻŋāĻĻāĻŋāĻ¨ āĻ°āĻžāĻ¤ ā§Š āĻāĻžāĻ° āĻ¸āĻŽā§ user_unmatches table āĻ PostgreSQL āĻāĻāĻāĻŋ query āĻ
āĻĒāĻžāĻ°ā§āĻļāĻžāĻ¨ execute āĻāĻ°āĻŦā§ āĻāĻŦāĻ current date-time āĻāĻ° āĻĨā§āĻā§ 40 days āĻĒā§āĻ°ā§āĻŦā§ create āĻšāĻā§āĻž row/user information āĻā§āĻ˛ā§ remove āĻāĻ°ā§ āĻĻāĻŋāĻŦā§āĨ¤
āĻāĻĒāĻ°ā§āĻā§āĻ¤ operation āĻāĻŋ āĻāĻāĻāĻŋ scheduler operation āĻ¯āĻž postgreSQL āĻāĻ° under āĻ pg_cron āĻāĻ° āĻĻā§āĻŦāĻžāĻ°āĻž execute āĻšā§āĨ¤ PostgreSQL āĻ Pg_cron configure āĻāĻ°āĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻ¨āĻŋāĻā§āĻ° instruction follow āĻāĻ°āĻ¤ā§ āĻšāĻŦā§ āĨ¤
Installing Pg_cron: Red Hat, CentOS, Fedora, and Amazon Linux āĻāĻ° āĻ¸āĻāĻā§ PostgreSQL 12 āĻ pg_cron extension install :
sudo yum install -y pg_cron_12
install on Debian, and Ubuntu with PostgreSQL 12 using apt.postgresql.org:
sudo apt-get -y install postgresql-12-cron
Setting up pg_cron: Pg_cron āĻāĻ° āĻāĻ¨ā§āĻ¯ postgreSQL āĻāĻ° background worker enable āĻāĻ°āĻžāĻ° āĻāĻ¨ā§āĻ¯ shared_preload_libraries āĻ¤ā§ pg_cron āĻā§ āĻāĻ˛ā§āĻ˛ā§āĻ āĻāĻ°āĻ¤ā§ āĻšāĻŦā§ āĨ¤ server āĻ¯āĻĻāĻŋ hot standby mode āĻ āĻĨāĻžāĻā§ āĻ¤āĻŦā§ pg_Cron run āĻšāĻŦā§ āĻ¨āĻžāĨ¤ āĻāĻŋāĻ¨ā§āĻ¤ā§ āĻ¯āĻāĻ¨ server start āĻšāĻŦā§ pg_cron automatically start āĻšāĻŦā§āĨ¤
add to postgresql.conf#
required to load pg_cron background worker on start-up#
shared_preload_libraries = 'pg_cron'
By default, pg_cron āĻāĻ° background worker āĻ¤āĻžāĻ° metadata tables āĻā§āĻ˛ā§ âPostgresâ database āĻ āĻ¤ā§āĻ°āĻŋ āĻāĻ°ā§āĨ¤ āĻ¤āĻžāĻ āĻ¯ā§ database āĻāĻ° table āĻ scheduler operation āĻāĻŋ execute āĻšāĻŦā§ āĻ¸ā§āĻ database name āĻāĻŋ postgresql.conf file āĻāĻ° cron.database_name configuration parameter āĻ āĻāĻ˛ā§āĻ˛ā§āĻ āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤
add to postgresql.conf#
optionally, specify the database in which the pg_cron background worker should run (defaults to postgres)#
cron.database_name = 'database_nameâ
Configuration part āĻļā§āĻˇ āĻšā§ā§āĻā§āĨ¤ āĻāĻāĻ¨ postgreSQL restart āĻāĻ°āĻžāĻ° āĻĒāĻ° âCREATE EXTENSION pg_cronâ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°ā§ pg_cron function āĻāĻŦāĻ metadata table āĻ¤ā§āĻ°āĻŋ āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°āĻž āĻ¯āĻžā§āĨ¤
â run as superuser: CREATE EXTENSION pg_cron;
â optionally, grant usage to regular users: GRANT USAGE ON SCHEMA cron TO postgre_user_name;
Important: by default pg_Cron, libpq āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°ā§ āĻāĻāĻāĻŋ āĻ¨āĻ¤ā§āĻ¨ connection āĻā§ local database āĻ āĻ¤ā§āĻ°āĻŋ āĻāĻ°ā§āĨ¤ āĻ¯āĻž pg_hba.conf āĻĨā§āĻā§ allowed āĻĨāĻžāĻāĻ¤ā§ āĻšā§āĨ¤ database āĻāĻ° user authentication āĻ trust enable āĻĨāĻžāĻāĻ¤ā§ āĻšāĻŦā§ āĻ¯ā§āĻ¨ localhost āĻ cron job running āĻĨāĻžāĻā§ āĨ¤ āĻāĻ āĻāĻ¨ā§āĻ¯ postgreuser āĻāĻ° password āĻāĻŋ .pgpass file āĻ āĻ°āĻžāĻāĻ¤ā§ āĻšāĻŦā§, āĻ¯āĻž libpq āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°ā§ connection open āĻāĻ°āĻŦā§āĨ¤
.pgpass āĻāĻ file āĻ postgresql āĻāĻ° credential āĻ¨āĻŋāĻŽā§āĻ¨ āĻ°ā§āĻĒā§ āĻĨāĻžāĻā§,
hostname:port:database:username:password#
localhost:5432:demo_db:postgres:postgres
pg_cron āĻāĻ° background workers āĻāĻ° number of concurrent job āĻāĻ° limit āĻā§ max_worker_processes āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°ā§ setting āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤
Schedule jobs via background workers instead of localhost connections#
cron.use_background_workers = on
Increase the number of available background workers from the default of 8#
max_worker_processes = 20
āĻāĻāĻžā§āĻž Unix domain socket directory āĻĨā§āĻā§ hostname āĻā§ trust permission āĻĻāĻŋā§ā§ pg_cron āĻāĻ° libpq āĻāĻ° connection established āĻāĻ°āĻž āĻ¯āĻžā§āĨ¤
Pg_cron āĻā§ operational āĻāĻ°āĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻ¨āĻŋāĻā§āĻ° step āĻā§āĻ˛ā§ follow āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤
Step 1: demo_db āĻāĻ database āĻ pg_cron extension āĻ¨āĻŋā§ā§ āĻāĻžāĻ āĻāĻ°āĻžāĻ° āĻāĻ¨ā§āĻ¯ at first postgreSQL āĻ CLI terminal āĻĨā§āĻā§ connect āĻšāĻ¤ā§ āĻšāĻŦā§āĨ¤
psql -h localhost -U postgres demo_db;
Step 2: step 1 āĻ connect āĻĨāĻžāĻāĻž āĻ āĻŦāĻ¸ā§āĻĨāĻžāĻ¤ā§ demo_db database āĻ Pg_cron āĻāĻ° plug-in create āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤
CREATE EXTENSION pg_cron;
GRANT USAGE ON SCHEMA cron TO postgre_user_name;
Step 1 āĻ connect āĻĨāĻžāĻāĻž āĻ
āĻŦāĻ¸ā§āĻĨāĻžāĻ¤ā§ demo_db āĻĨā§āĻā§ create āĻāĻ°āĻž extension remove āĻāĻ°āĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻ¨āĻŋāĻā§āĻ° command run āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤
DROP EXTENSION pg_cron;
Step 1 āĻ connect āĻĨāĻžāĻāĻž āĻ āĻŦāĻ¸ā§āĻĨāĻžāĻ¤ā§ pg_cron āĻĨā§āĻā§ schedule āĻāĻ°āĻž āĻ¸āĻāĻ˛ queryāĻā§āĻ˛ā§ āĻ¨āĻŋāĻā§āĻ° query āĻĻā§āĻŦāĻžāĻ°āĻž āĻĻā§āĻāĻž āĻ¯āĻžā§āĨ¤
select * from cron.job;
jobid | schedule | command | nodename | nodeport | database | username | active | jobname
-------+----------------+----------------------------------+---------------+-------------+----------------+--------------+--------+---------
Step 3: Step 1 āĻ connect āĻĨāĻžāĻāĻž āĻ āĻŦāĻ¸ā§āĻĨāĻžāĻ¤ā§ pg_cron āĻ postgreSQL āĻāĻ° query āĻāĻŋ shedule āĻāĻ°āĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻ¨āĻŋāĻā§āĻ° query execute āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤ SELECT cron.schedule(â0 3 * * *â, $$DELETE FROM public.auth_user_app_user_unmatch WHERE user_unmatch_created_at < now() - interval â40 daysâ$$);
Schedule query āĻāĻŋāĻ° structure SELECT cron.schedule(â* * * * *â, $$
ââââââââââââââ 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)
â â â â â
â â â â â
* * * * *
Pg_cron āĻāĻ° āĻā§āĻ¨ schedule āĻā§ unschedule āĻāĻ°āĻ¤ā§ āĻšāĻ˛ā§ āĻ¨āĻŋāĻā§āĻ° command āĻāĻŋ Step 1 āĻ connect āĻĨāĻžāĻāĻž āĻ
āĻŦāĻ¸ā§āĻĨāĻžāĻ¤ā§ run āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤
SELECT cron.unschedule(jobid);
Or
SELECT cron.unschedule(jobid) FROM cron.job;
SELECT \* FROM cron.job; āĻĨā§āĻā§ Jobid āĻ¨āĻŋā§ā§ āĻ¸ā§āĻ jobid user āĻāĻ°ā§ unschedule āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤
jobid | schedule | command | nodename | nodeport | database | username | active | jobname
-------+-------------+-----------------------+-----------+----------+-------------+----------+--------+---------
3 | 0 3 \* \* \* | DELETE FROM public.auth_user_app_user_unmatch WHERE user_unmatch_created_at < now() - interval '2 MINUTE' | localhost | 5432 | demo_db | postgres | t |
(1 rows)
āĻāĻĻāĻžāĻšāĻ° āĻ¸āĻ°ā§āĻĒ SELECT cron.unschedule(3); āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°ā§ schedule āĻāĻŋ unschedule āĻāĻ°āĻž āĻ¯āĻžā§āĨ¤