Skip to content

1.6 cron

1.6.1.安装pg_cron

pg12.2

cron1.2.0

bash
$ cd /opt
$ wget https://github.com/citusdata/pg_cron/archive/v1.2.0.tar.gz
$ tar zxvf v1.2.0.tar.gz
$ cd pg_cron-1.2.0/
$ make PG_CONFIG=/data/apps/pgsql/12/bin/pg_config
$ make install PG_CONFIG=/data/apps/pgsql/12/bin/pg_config

注意:
/data/apps/pgsql/12/bin/pg_config  ------> 安装在已有的上面,否则会出现权限拒绝的情况
$ cd /opt
$ wget https://github.com/citusdata/pg_cron/archive/v1.2.0.tar.gz
$ tar zxvf v1.2.0.tar.gz
$ cd pg_cron-1.2.0/
$ make PG_CONFIG=/data/apps/pgsql/12/bin/pg_config
$ make install PG_CONFIG=/data/apps/pgsql/12/bin/pg_config

注意:
/data/apps/pgsql/12/bin/pg_config  ------> 安装在已有的上面,否则会出现权限拒绝的情况

1.6.2.编辑postgresql.conf

bash
vi postgresql.conf
shared_preload_libraries = 'pg_cron'
cron.database_name = 'postgres' --pg_cron元数据存放数据库

重启数据库
vi postgresql.conf
shared_preload_libraries = 'pg_cron'
cron.database_name = 'postgres' --pg_cron元数据存放数据库

重启数据库

[!WARNING]

第二行的指定cron的元数据相关信息存放的数据库,是可以改成其他的。这里要明确一个概念,cron安装的数据库,和它要控制的数据库没有什么必然联系,并不因为说安装在了postgres库,就不能调度其他库了

1.6.3创建扩展

sql
postgres=# create extension pg_cron;
CREATE EXTENSION

-- 删除扩展
postgres=# drop extension pg_cron;

-- 查看扩展
postgres@postgres=>\dx
                 List of installed extensions
  Name   | Version |   Schema   |         Description          
---------+---------+------------+------------------------------
 pg_cron | 1.2     | public     | Job scheduler for PostgreSQL
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(2 rows)

-- 实际当中安装了pg_cron 扩展会在当前数据库生成一张cron.job表
postgres@postgres=>select * from pg_tables where schemaname = 'cron';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 cron       | job       | postgres   |            | t          | f        | t           | t
(1 row)
postgres=# create extension pg_cron;
CREATE EXTENSION

-- 删除扩展
postgres=# drop extension pg_cron;

-- 查看扩展
postgres@postgres=>\dx
                 List of installed extensions
  Name   | Version |   Schema   |         Description          
---------+---------+------------+------------------------------
 pg_cron | 1.2     | public     | Job scheduler for PostgreSQL
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(2 rows)

-- 实际当中安装了pg_cron 扩展会在当前数据库生成一张cron.job表
postgres@postgres=>select * from pg_tables where schemaname = 'cron';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 cron       | job       | postgres   |            | t          | f        | t           | t
(1 row)

1.6.4赋予普通用户权限(可选)

postgres=# GRANT USAGE ON SCHEMA cron TO username;
postgres=# GRANT USAGE ON SCHEMA cron TO username;

1.6.5普通用户创建job

sql
cron格式如下
 ┌───────────── 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)
 │ │ │ │ │
 │ │ │ │ │
 * * * * *
 
 $ /opt/pg12/bin/psql -p5555 -Utest postgres
psql (12.1)
Type "help" for help.

--每分钟插入一条随机数据
postgres@postgres=>select cron.schedule('* * * * *',$$insert into emp1 values((random()*100),'hello');$$);
 schedule 
----------
        1
(1 row)

--每天上午10点执行清理(GMT)
-- 当GMT为早上7点,那么北京时间就应该为当日7+8=15时(下午3点)
postgres=> SELECT cron.schedule('0 10 * * *', 'VACUUM');


-- 查询
postgres@postgres=> select jobid,command from cron.job;
 jobid |                     command                      
-------+--------------------------------------------------
     3 | insert into emp1 values((random()*100),'hello');
(1 row)

--删除这个任务
postgres@postgres=>select cron.unschedule(3); --cron.job.jobid
 unschedule 
------------
 t
(1 row)
cron格式如下
 ┌───────────── 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)
 │ │ │ │ │
 │ │ │ │ │
 * * * * *
 
 $ /opt/pg12/bin/psql -p5555 -Utest postgres
psql (12.1)
Type "help" for help.

--每分钟插入一条随机数据
postgres@postgres=>select cron.schedule('* * * * *',$$insert into emp1 values((random()*100),'hello');$$);
 schedule 
----------
        1
(1 row)

--每天上午10点执行清理(GMT)
-- 当GMT为早上7点,那么北京时间就应该为当日7+8=15时(下午3点)
postgres=> SELECT cron.schedule('0 10 * * *', 'VACUUM');


-- 查询
postgres@postgres=> select jobid,command from cron.job;
 jobid |                     command                      
-------+--------------------------------------------------
     3 | insert into emp1 values((random()*100),'hello');
(1 row)

--删除这个任务
postgres@postgres=>select cron.unschedule(3); --cron.job.jobid
 unschedule 
------------
 t
(1 row)

[!WARNING]

1.pg_cron不会在备库运行job,但当备库提升为主库时会自动开启job。

2.pg_cron时间为GMT