Giter Club home page Giter Club logo

pg_cron's Introduction

Citus Banner

What is pg_cron?

pg_cron is a simple cron-based job scheduler for PostgreSQL (10 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. You can also use '[1-59] seconds' to schedule a job based on an interval.

pg_cron also allows you using '$' to indicate last day of the month.

-- 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()');

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) 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)
 │ │ │ │ │
 │ │ │ │ │
 * * * * *

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.

Installing pg_cron

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

# Install the pg_cron extension
sudo yum install -y pg_cron_16

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

# Install the pg_cron extension
sudo apt-get -y install postgresql-16-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-16/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.

# add to postgresql.conf

# required to load pg_cron background worker on start-up
shared_preload_libraries = 'pg_cron'

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

# optionally, specify the database in which the pg_cron background worker should run (defaults to postgres)
cron.database_name = 'postgres'

pg_cron may only be installed to one database in a cluster. If you need to run jobs in multiple databases, use cron.schedule_in_database().

Previously pg_cron could only use GMT time, but now you can adapt your time by setting cron.timezone in postgresql.conf.

# add to postgresql.conf

# optionally, specify the timezone in which the pg_cron background worker should run (defaults to GMT). E.g:
cron.timezone = 'PRC'

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;

Ensuring pg_cron can start jobs

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

You can also use a unix domain socket directory as the hostname and enable trust authentication for local connections in pg_hba.conf, which is normally safe:

# Connect via a unix domain socket:
cron.host = '/tmp'

# Can also be an empty string to look for the default directory:
cron.host = ''

Alternatively, pg_cron can be configured to use background workers. In that case, the number of concurrent jobs is limited by the max_worker_processes setting, so you may need to raise that.

# 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

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.

Viewing job run details

You can view the status of running and recently completed job runs in the cron.job_run_details:

select * from cron.job_run_details order by start_time desc limit 5;
┌───────┬───────┬─────────┬──────────┬──────────┬───────────────────┬───────────┬──────────────────┬───────────────────────────────┬───────────────────────────────┐
│ jobid │ runid │ job_pid │ database │ username │      command      │  status   │  return_message  │          start_time           │           end_time            │
├───────┼───────┼─────────┼──────────┼──────────┼───────────────────┼───────────┼──────────────────┼───────────────────────────────┼───────────────────────────────┤
│    1043282610 │ postgres │ marco    │ select process()  │ succeeded │ SELECT 12023-02-07 09:30:00.098164+012023-02-07 09:30:00.130729+01 │
│    1043272609 │ postgres │ marco    │ select process()  │ succeeded │ SELECT 12023-02-07 09:29:00.015168+012023-02-07 09:29:00.832308+01 │
│    1043212603 │ postgres │ marco    │ select process()  │ succeeded │ SELECT 12023-02-07 09:28:00.011965+012023-02-07 09:28:01.420901+01 │
│    1043202602 │ postgres │ marco    │ select process()  │ failed    │ server restarted │ 2023-02-07 09:27:00.011833+012023-02-07 09:27:00.72121+01  │
│     943202602 │ postgres │ marco    │ select do_stuff() │ failed    │ job canceled     │ 2023-02-07 09:26:00.011833+012023-02-07 09:26:00.22121+01  │
└───────┴───────┴─────────┴──────────┴──────────┴───────────────────┴───────────┴──────────────────┴───────────────────────────────┴───────────────────────────────┘
(10 rows)

The records in cron.job_run_details are not cleaned automatically, but every user that can schedule cron jobs also has permission to delete their own cron.job_run_details records.

Especially when you have jobs that run every few seconds, it can be a good idea to clean up regularly, which can easily be done using pg_cron itself:

-- Delete old cron.job_run_details records of the current user every day at noon
SELECT  cron.schedule('delete-job-run-details', '0 12 * * *', $$DELETE FROM cron.job_run_details WHERE end_time < now() - interval '7 days'$$);

If you do not want to use cron.job_run_details at all, then you can add cron.log_run = off to postgresql.conf.

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
Aiven ✔️
Alibaba Cloud ✔️
Amazon RDS ✔️
Azure ✔️
Crunchy Bridge ✔️
DigitalOcean ✔️
Google Cloud ✔️
Heroku
ScaleGrid ✔️
Scaleway ✔️
Supabase ✔️
Tembo ✔️

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

pg_cron's People

Contributors

alexeyklyukin avatar aykut-bozkurt avatar bchrobot avatar bdrouvotaws avatar bookvik avatar df7cb avatar dverite avatar fluca1978 avatar hughcapet avatar italomg avatar japinli avatar lornajane avatar marcocitus avatar marcoslot avatar mizhka avatar mrdrivingduck avatar mtuncer avatar nathan-bossart avatar ngalstyan4 avatar nmisch avatar nuno-faria avatar nxz91 avatar robert-pang avatar ryw avatar sebastianwebber avatar serprex avatar timgates42 avatar tsinghualucky912 avatar viliuss avatar zhjwpku avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pg_cron's Issues

cron job 1 connection failed : could not receive data from client: Connection reset by peer

Hi.

I'm running postgres 9.6 and I have configured pg_cron in accordance with your guide.
I also have set a cron job to run by the minute, so I could test it and remove my Linux cron.

But I am stuck with this error :

< 2017-03-15 11:58:00.000 UTC > LOG: cron job 1 starting: SELECT unilever_data_merge('unl_client')
< 2017-03-15 11:58:00.016 UTC > LOG: cron job 1 connection failed
< 2017-03-15 11:58:00.016 UTC > LOG: could not receive data from client: Connection reset by peer

This is my cron.job table content :

unilever=# SELECT * FROM cron.job ;
jobid | schedule | command | nodename | nodeport | database | username
-------+-----------+------------------------------------------+-----------+----------+----------+----------
1 | * * * * * | SELECT unilever_data_merge('unl_client') | localhost | 5432 | unilever | unilever

If I run this function with this user from localhost, via psql client, it goes smooth :

[root@unilever-teste1 ~]# psql -U unilever -h localhost -d unilever -c "SELECT unilever_data_merge('unl_client')"
NOTICE: 2017-03-15 12:00:19.833+00 Starting execution.
NOTICE: Table: unl_client, query type: Insert, query order: 1
NOTICE: Execution time : 1 seconds. Affected row(s): 0.
NOTICE: Table: unl_client, query type: Update, query order: 2
NOTICE: Execution time : 0 seconds. Affected row(s): 0.
NOTICE: Table: unl_client, query type: Update, query order: 3
NOTICE: Execution time : 0 seconds. Affected row(s): 0.
NOTICE: 2017-03-15 12:00:21.715+00 Finished execution.

What could be causing this issue ?

Thanks !

Added pg_cron to Alpine Linux

I've added pg_cron to the Alpine Linux distribution; this because alpine is heavily used within docker environments, so pg_cron can be used within a docker container which runs postgresql on Alpine.

Currently it's only available for the latest 'edge' branch under testing.
https://pkgs.alpinelinux.org/packages?name=pg_cron&branch=&repo=&arch=&maintainer=

Architectures:

  • aarch64
  • armhf
  • x86
  • x86_64

Question:
Currently the latest version is a release candidate, is there any information when the final 1.0.0 will be released ?

Add 'database' parameter to cron.schedule

I think it could be useful for providing containers with pg_cron installed to set the cron.database_name to a certain fixed database (even 'postgres') and allow scheduling actual tasks against any other database on the same cluster. Everything is already there, except for the cron.schedule function support. Does it make sense to make another version of this function with an additional 'database' parameter?

pg_cron is cannot work

configure

shared_preload_libraries= 'pg_cron,others extension'
cron.database_name='postgres'
postgres=# select * from cron.job;
 jobid |  schedule   |           command           | nodename  | nodeport | database | username
-------+-------------+-----------------------------+-----------+----------+----------+----------
     1 | */1 * * * * | insert into t1 select now() | localhost |     5432 | postgres | postgres
(1 row)

backend ground is OK
bgworker: pg_cron_scheduler
Why job is cant work? PG version is 9.5.3

Not able to make pg_cron work

This seems to be a very useful project and appreciate Citusdata opensourcing it.
I have tried this in both 9.6 and 10 ( single node default installation ) and did exactly as the instructions have said, but after several hours I cannot seem to make it work in both cases.

Environment:
Plain vanilla installation of PG 9.6 and PG 10 on Ubuntu

  1. I create a testcron table with id int , insert_time timestampz default now()
  2. I create a cron job using the syntax with
    SELECT cron.schedule('1 * * * *', $$insert into testcron values(1)$$);
    or
    SELECT cron.schedule('1 * * * *', $$delete from testcron where id = 1$$);
  3. It creates the job but doesn't do anything after 1 minute.

Unable to install in a container based on PostgreSQL official Docker

Hi I'm trying to install your extension on a container based on PostgerSQL official docker for version 9.5.5 and I'm facing an error I can not resolve myself.

These are the steps I've taken, after starting a vanilla PostrgeSQL 9.5.5:

$ apt-get update
$ apt-get install -y git make gcc postgresql-server-dev-9.5
$ git clone https://github.com/citusdata/pg_cron.git
$ cd ./pg_cron
$ PATH=/usr/lib/postgresql/9.5/bin/:$PATH make

The path mentioned in the readme, /usr/local/pgsql/bin/, does not exist in this container and I assumed it should be pointing to the directory where the postgres executable is located, so I changed it to /usr/lib/postgresql/9.5/bin/. At this point I'm facing the following error:

gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -I/usr/include/mit-krb5 -fPIC -pie -fno-omit-frame-pointer -fpic -std=c99 -Wall -Wextra -Werror -Wno-unused-parameter -Iinclude -I/usr/include/postgresql -I. -I./ -I/usr/include/postgresql/9.5/server -I/usr/include/postgresql/internal -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2  -I/usr/include/tcl8.6  -c -o src/job_metadata.o src/job_metadata.c
src/job_metadata.c:232:1: error: ‘NextJobId’ was used with no prototype before its definition [-Werror=missing-prototypes]
 NextJobId(void)
 ^
cc1: all warnings being treated as errors
<builtin>: recipe for target 'src/job_metadata.o' failed
make: *** [src/job_metadata.o] Error 1

It would be great if you can help me install your extension on a docker. Thanks.

docs suggest wrong location for .pgpass

The README says: "Alternatively, you can create a .pgpass file in the working directory of the database server." But that's not where libpq looks for .pgpass - by default it it looks in the home directory of the user, in this case the user that's running Postgres (or %APPDATA%\postgresql\pgpass.conf on Windows). Unless I'm misunderstanding the code, that's what the docs should say.

Looks like pg_cron does not recognize ranges

INSERT INTO cron.job (schedule, command, nodename, nodeport, database, username)
VALUES ('*/1 * * * *', $$SELECT auc_status_monitor()$$, '', 5432, 'mp', 'postgres');

INSERT INTO cron.job (schedule, command, nodename, nodeport, database, username)
VALUES ('*/1 08-19 * * *', $$SELECT auction_status_monitor2_ysh()$$, '', 5432, 'mp', 'postgres');

as I can see hour range 08-19 does not work

root@psql:/etc/postgresql/9.6/main# date
Fri Sep 15 10:41:31 +06 2017
root@psql:/etc/postgresql/9.6/main# uname -a
Linux psql 4.9.0-3-amd64 #1 SMP Debian 4.9.30-2+deb9u2 (2017-06-26) x86_64 GNU/Linux
root@psql:/etc/postgresql/9.6/main# tail -f /var/log/postgresql/postgresql-9.6-main.log
2017-09-15 10:40:00.000 +06 [15420] LOG:  cron job 3 starting: SELECT auc_status_monitor()
2017-09-15 10:40:00.038 +06 [15420] LOG:  cron job 3 completed: 1 row
2017-09-15 10:41:00.000 +06 [15420] LOG:  cron job 3 starting: SELECT auc_status_monitor()
2017-09-15 10:41:00.022 +06 [15420] LOG:  cron job 3 completed: 1 row
2017-09-15 10:42:00.000 +06 [15420] LOG:  cron job 3 starting: SELECT auc_status_monitor()
2017-09-15 10:42:00.022 +06 [15420] LOG:  cron job 3 completed: 1 row

if I remove hour range - everything is ok

root@psql:/etc/postgresql/9.6/main# psql -h localhost -U mp mp -W mp
psql: warning: extra command-line argument "mp" ignored
Password for user mp:
psql (9.6.3)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

mp=# select * from cron.job;
 jobid |    schedule     |               command                | nodename | nodeport | database | username
-------+-----------------+--------------------------------------+----------+----------+----------+----------
     4 | */1 08-19 * * * | SELECT auction_status_monitor2_ysh() |          |     5432 | mp       | postgres
     3 | */1 * * * *     | SELECT auc_status_monitor()          |          |     5432 | mp       | postgres
(2 rows)

mp=# SELECT cron.unschedule(4);
 unschedule
------------
 t
(1 row)

mp=# select * from cron.job;
 jobid |  schedule   |           command           | nodename | nodeport | database | username
-------+-------------+-----------------------------+----------+----------+----------+----------
     3 | */1 * * * * | SELECT auc_status_monitor() |          |     5432 | mp       | postgres
(1 row)

mp=# INSERT INTO cron.job (schedule, command, nodename, nodeport, database, username)
mp-# VALUES ('*/1 * * * *', $$SELECT auction_status_monitor2_ysh()$$, '', 5432, 'mp', 'postgres');
INSERT 0 1
mp=#
mp=#
mp=# select * from cron.job;
 jobid |  schedule   |               command                | nodename | nodeport | database | username
-------+-------------+--------------------------------------+----------+----------+----------+----------
     3 | */1 * * * * | SELECT auc_status_monitor()          |          |     5432 | mp       | postgres
     5 | */1 * * * * | SELECT auction_status_monitor2_ysh() |          |     5432 | mp       | postgres
(2 rows)

mp=# \q
root@psql:/etc/postgresql/9.6/main# tail -f /var/log/postgresql/postgresql-9.6-main.log
2017-09-15 10:40:00.000 +06 [15420] LOG:  cron job 3 starting: SELECT auc_status_monitor()
2017-09-15 10:40:00.038 +06 [15420] LOG:  cron job 3 completed: 1 row
2017-09-15 10:41:00.000 +06 [15420] LOG:  cron job 3 starting: SELECT auc_status_monitor()
2017-09-15 10:41:00.022 +06 [15420] LOG:  cron job 3 completed: 1 row
2017-09-15 10:42:00.000 +06 [15420] LOG:  cron job 3 starting: SELECT auc_status_monitor()
2017-09-15 10:42:00.022 +06 [15420] LOG:  cron job 3 completed: 1 row
2017-09-15 10:43:00.000 +06 [15420] LOG:  cron job 3 starting: SELECT auc_status_monitor()
2017-09-15 10:43:00.022 +06 [15420] LOG:  cron job 3 completed: 1 row
2017-09-15 10:44:00.000 +06 [15420] LOG:  cron job 3 starting: SELECT auc_status_monitor()
2017-09-15 10:44:01.141 +06 [15420] LOG:  cron job 3 completed: 1 row
2017-09-15 10:45:00.000 +06 [15420] LOG:  cron job 5 starting: SELECT auction_status_monitor2_ysh()
2017-09-15 10:45:00.001 +06 [15420] LOG:  cron job 3 starting: SELECT auc_status_monitor()
2017-09-15 10:45:00.050 +06 [15420] LOG:  cron job 3 completed: 1 row

Call a function via pg_cron

Hi

I added a new record in cron.job table via this sql command:

SELECT cron.schedule('* * * * *', 'select "sample_func"(200::INTEGER)');

The pg_cron extension is installed on a db except postgres(sample_db). So cron.database_name in postgresql.conf is set to sample_db

cron.database_name = 'sample_db'

The cron is run every minute, but the problem is that calling this cron caused an error:

< 2017-07-10 22:37:00.001 EDT > LOG: cron job 10 starting: select "sample_func"(200::INTEGER)
< 2017-07-10 22:37:00.033 EDT > LOG: cron job 10 connection failed

This is necessary to say that if exist another records in cron.job table in my case, this error is repeated for them. (commands like VACUUM or select 1)

Warnings being treated as errors when building from source

I am trying to build pg_cron from within a docker container. I run into the following issue;

src/pg_cron.c: In function 'ManageCronTask':
src/pg_cron.c:776:16: error: this statement may fall through [-Werror=implicit-fallthrough=]
    task->state = CRON_TASK_START;
    ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
src/pg_cron.c:779:3: note: here
   case CRON_TASK_START:
   ^~~~
src/pg_cron.c:1118:16: error: this statement may fall through [-Werror=implicit-fallthrough=]
    task->state = CRON_TASK_DONE;
    ~~~~~~~~~~~~^~~~~~~~~~~~~~~~
src/pg_cron.c:1123:3: note: here
   case CRON_TASK_DONE:
   ^~~~
cc1: all warnings being treated as errors
make: *** [src/pg_cron.o] Error 1

It has been suggested that I add -Wimplicit-fallthrough=0 to the PG_CPPFLAGS in the Makefile. Why is this happening in the first place? I don't really want to have to manipulate the Makefile from within the dockerfile...

background worker huge memory allocation

We're seeing this behavior which is more perceptible when using many jobs scheduled on pg_cron.

This is the situation after one week with 400 jobs:

  PID USER      PR  NI    VIRT    RES    SHR   USED S  %CPU %MEM     TIME+ COMMAND
 1786 postgres  20   0 9766.3m 4.181g   3228 4.181g S   0.0 22.0   0:38.14 postgres: bgworker: pg_cron_scheduler

As shown the shared memory is aroung 3.2MB and the process memory is 4.1GB.

The version where the issue was detected is PostgreSQL 9.5.7 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11), 64-bit, but also occurs on PostgreSQL 9.5.7 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit.

Bellow there are the SQL commands creating jobs and the top measurements showing the memory increase:

postgres=# select now(); insert into cron.job (schedule, command, nodename, nodeport, database, username) values ('* * * * *', 'select 1', 'localhost', '5432', 'postgres', 'postgres');
              now              
-------------------------------
 2017-06-01 10:58:28.591112-03
(1 row)
INSERT 0 1
postgres=# select now(); insert into cron.job (schedule, command, nodename, nodeport, database, username) select schedule, command, nodename, nodeport, database, username from cron.job;
             now              
------------------------------
 2017-06-01 10:58:48.90418-03
(1 row)
INSERT 0 1
postgres=# select now(); insert into cron.job (schedule, command, nodename, nodeport, database, username) select schedule, command, nodename, nodeport, database, username from cron.job;
              now              
-------------------------------
 2017-06-01 10:59:43.526128-03
(1 row)
INSERT 0 2
postgres=# select now(); insert into cron.job (schedule, command, nodename, nodeport, database, username) select schedule, command, nodename, nodeport, database, username from cron.job;
              now              
-------------------------------
 2017-06-01 11:00:04.406233-03
(1 row)
INSERT 0 4
postgres=# select now(); insert into cron.job (schedule, command, nodename, nodeport, database, username) select schedule, command, nodename, nodeport, database, username from cron.job;
              now              
-------------------------------
 2017-06-01 11:00:20.862736-03
(1 row)
INSERT 0 8
postgres=# select now(); insert into cron.job (schedule, command, nodename, nodeport, database, username) select schedule, command, nodename, nodeport, database, username from cron.job;
              now              
-------------------------------
 2017-06-01 11:00:49.438208-03
(1 row)
INSERT 0 16
postgres=# select now(); insert into cron.job (schedule, command, nodename, nodeport, database, username) select schedule, command, nodename, nodeport, database, username from cron.job;
              now              
-------------------------------
 2017-06-01 11:01:27.310265-03
(1 row)
INSERT 0 32
postgres=# select now(); insert into cron.job (schedule, command, nodename, nodeport, database, username) select schedule, command, nodename, nodeport, database, username from cron.job;
              now              
-------------------------------
 2017-06-01 11:01:56.518833-03
(1 row)
INSERT 0 64
postgres=# select now(); insert into cron.job (schedule, command, nodename, nodeport, database, username) select schedule, command, nodename, nodeport, database, username from cron.job;
              now              
-------------------------------
 2017-06-01 11:02:23.070821-03
(1 row)
INSERT 0 128
postgres=# select now(); insert into cron.job (schedule, command, nodename, nodeport, database, username) select schedule, command, nodename, nodeport, database, username from cron.job;
              now              
-------------------------------
 2017-06-01 11:02:51.662401-03
(1 row)
INSERT 0 256
postgres=# select version();
                                         version                                          
------------------------------------------------------------------------------------------
 PostgreSQL 9.5.7 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
(1 row)
pinogy@workstation:~ $ top -b -d 30 -p 3988 | egrep "(load|pg_cron|USED)"
top - 10:57:44 up 37 min,  6 users,  load average: 0.25, 0.20, 0.13
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  232452   8700   6624   8700      0 S   0.0  0.0   0:00.00 postgres: bgworker: pg_cron_scheduler
top - 10:58:14 up 38 min,  6 users,  load average: 0.29, 0.22, 0.13
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  232452   8700   6624   8700      0 S   0.0  0.0   0:00.00 postgres: bgworker: pg_cron_scheduler
top - 10:58:44 up 38 min,  6 users,  load average: 0.25, 0.21, 0.13
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  232452   8700   6624   8700      0 S   0.0  0.0   0:00.00 postgres: bgworker: pg_cron_scheduler
top - 10:59:14 up 39 min,  6 users,  load average: 0.22, 0.21, 0.13
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  239028  11208   8896  11208      0 S   0.0  0.0   0:00.00 postgres: bgworker: pg_cron_scheduler
top - 10:59:44 up 39 min,  6 users,  load average: 0.13, 0.18, 0.13
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  239028  11208   8896  11208      0 S   0.0  0.0   0:00.00 postgres: bgworker: pg_cron_scheduler
top - 11:00:14 up 40 min,  6 users,  load average: 0.08, 0.17, 0.12
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  239028  11272   8896  11272      0 S   0.0  0.0   0:00.01 postgres: bgworker: pg_cron_scheduler
top - 11:00:44 up 40 min,  6 users,  load average: 0.05, 0.15, 0.11
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  239028  11272   8896  11272      0 S   0.0  0.0   0:00.01 postgres: bgworker: pg_cron_scheduler
top - 11:01:14 up 41 min,  6 users,  load average: 0.03, 0.13, 0.11
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  240120  11596   8896  11596      0 S   0.0  0.0   0:00.02 postgres: bgworker: pg_cron_scheduler
top - 11:01:44 up 41 min,  6 users,  load average: 0.02, 0.12, 0.10
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  240120  11596   8896  11596      0 S   0.0  0.0   0:00.02 postgres: bgworker: pg_cron_scheduler
top - 11:01:45 up 41 min,  6 users,  load average: 0.02, 0.12, 0.10
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  240120  11596   8896  11596      0 S   0.0  0.0   0:00.02 postgres: bgworker: pg_cron_scheduler
top - 11:02:15 up 42 min,  6 users,  load average: 0.01, 0.11, 0.10
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  243640  12836   8928  12836      0 S   0.1  0.0   0:00.05 postgres: bgworker: pg_cron_scheduler
top - 11:02:45 up 42 min,  6 users,  load average: 0.00, 0.09, 0.09
  PID USER      PR  NI    VIRT    RES    SHR   USED   SWAP S  %CPU %MEM     TIME+ COMMAND
 3988 pinogy    20   0  243640  13100   8936  13100      0 S   0.0  0.0   0:00.06 postgres: bgworker: pg_cron_scheduler
top - 11:03:15 up 43 min,  6 users,  load average: 0.00, 0.08, 0.09

Can not create extension

When I try to create extension on the database this is the error that I get

CREATE EXTENSION pg_cron;

ERROR: unrecognized configuration parameter "cron.database_name"
CONTEXT: PL/pgSQL function inline_code_block line 3 at IF

Can pg_cron be used with streaming replication?

I have a primary database that is streaming to a hot-standby replica. I'd like to use pg_cron, but I'm thinking about the issues related to failover. It seems like the right behavior would be for pg_cron's worker to be running on the primary, but either absent entirely from, or 'paused' on the replica until triggered by the failover event. Is this possible?

Building pg_cron results in a gcc error

Following the instructions in the README, gcc complains about return values and exits with the following errors:

src/misc.c:306:10: error: ignoring return value of ?fscanf?, declared with attribute warn_unused_result [-Werror=unused-result]
    fscanf(fp, "%d", &otherpid);
          ^
src/misc.c:321:2: error: ignoring return value of ?ftruncate?, declared with attribute warn_unused_result [-Werror=unused-result]
  (void) ftruncate(fileno(fp), ftell(fp));
  ^
cc1: all warnings being treated as errors
make: *** [src/misc.o] Error 1

Removing the -Werror flag from PG_CPPFLAGS in the Makefile appears to resolve this issue.

Add a way to run a job every x seconds

It would be useful to be able to run a job every few seconds. Pg_cron currently relies on battle-tested cron code, which works at a per-minute level. I'm a bit nervous about simply adding another column to the schedule without battle-tested logic for dealing with clock jumps etc. It doesn't necessarily make sense to schedule a job at a particular second, given that a process can easily skip past that second. However, it may be sufficient to just have the ability to schedule a job every x seconds from the start of the process without following a particular cron schedule.

A possible interface could be:

SELECT cron.schedule(interval '1 second', 'SELECT do_job()');

There are workarounds, but they are rather ugly, e.g.:

CREATE EXTENSION IF NOT EXISTS dblink;

CREATE OR REPLACE FUNCTION do_every_second(run_time interval default '60 seconds')
RETURNS bool AS $do_every_second$
DECLARE
    end_time timestamptz := now() + run_time;
BEGIN
    WHILE now() < end_time LOOP
        PERFORM FROM dblink('host=localhost port=5432 dbname=postgres',
                            'SELECT do_job()') AS (num_rows bigint);

        PERFORM pg_sleep(1);  
    END LOOP;

    RETURN true;
END;
$do_every_second$ LANGUAGE plpgsql;

SELECT cron.schedule('* * * * *', 'SELECT do_every_second()');

Clean up unused cron code

There are still some unused functions from cron in pg_cron that should be removed to avoid build problems.

Fedora Packages

Hi Folks,
I am wondering if we can expect pg_cron packages for another later than Fedora 26?

Based on the information in the Readme, this should work, although, I can see there are only Fedora 26 packages for pg_cron.

After adding the citusdata repo, and taking a peek at what is available on Fedora 28:

~/ansible on  devel! ⌚ 11:47:26
$ yum --disablerepo="*" --enablerepo="citusdata_community" list available                           
Last metadata expiration check: 0:17:29 ago on Wed 20 Jun 2018 11:29:59 AM PDT.
Available Packages
citus72_10.x86_64                                                                                     7.2.2.citus-1.fc28                                                                          citusdata_community
citus72_10-debuginfo.x86_64                                                                           7.2.2.citus-1.fc28                                                                          citusdata_community
citus72_10-debugsource.x86_64                                                                         7.2.2.citus-1.fc28                                                                          citusdata_community
citus72_96.x86_64                                                                                     7.2.2.citus-1.fc28                                                                          citusdata_community
citus72_96-debuginfo.x86_64                                                                           7.2.2.citus-1.fc28                                                                          citusdata_community
citus72_96-debugsource.x86_64                                                                         7.2.2.citus-1.fc28                                                                          citusdata_community
citus73_10.x86_64                                                                                     7.3.0.citus-1.fc28                                                                          citusdata_community
citus73_10-debuginfo.x86_64                                                                           7.3.0.citus-1.fc28                                                                          citusdata_community
citus73_10-debugsource.x86_64                                                                         7.3.0.citus-1.fc28                                                                          citusdata_community
citus73_96.x86_64                                                                                     7.3.0.citus-1.fc28                                                                          citusdata_community
citus73_96-debuginfo.x86_64                                                                           7.3.0.citus-1.fc28                                                                          citusdata_community
citus73_96-debugsource.x86_64                                                                         7.3.0.citus-1.fc28                                                                          citusdata_community
citus74_10.x86_64                                                                                     7.4.1.citus-1.fc28                                                                          citusdata_community
citus74_10-debuginfo.x86_64                                                                           7.4.1.citus-1.fc28                                                                          citusdata_community
citus74_10-debugsource.x86_64                                                                         7.4.1.citus-1.fc28                                                                          citusdata_community
citus74_96.x86_64                                                                                     7.4.1.citus-1.fc28                                                                          citusdata_community
citus74_96-debuginfo.x86_64                                                                           7.4.1.citus-1.fc28                                                                          citusdata_community
citus74_96-debugsource.x86_64                                                                         7.4.1.citus-1.fc28                                                                          citusdata_community

Running the following:

sudo sed -i.bak 's/fedora\/28/fedora\/26/g' /etc/yum.repos.d/citusdata_community.repo

I can now install pg_cron_10 happily enough.
i.e. here's what a list available looks like afterward:

/etc/yum.repos.d ⌚ 11:55:09
$ yum --disablerepo="*" --enablerepo="citusdata_community" list available              
Last metadata expiration check: 0:07:44 ago on Wed 20 Jun 2018 11:51:24 AM PDT.
Available Packages
citus61_95.x86_64                                                                                    6.1.3.citus-1.fc26                                                                           citusdata_community
citus61_95-debuginfo.x86_64                                                                          6.1.3.citus-1.fc26                                                                           citusdata_community
citus61_96.x86_64                                                                                    6.1.3.citus-1.fc26                                                                           citusdata_community
citus61_96-debuginfo.x86_64                                                                          6.1.3.citus-1.fc26                                                                           citusdata_community
citus62_95.x86_64                                                                                    6.2.5.citus-1.fc26                                                                           citusdata_community
citus62_95-debuginfo.x86_64                                                                          6.2.5.citus-1.fc26                                                                           citusdata_community
citus62_96.x86_64                                                                                    6.2.5.citus-1.fc26                                                                           citusdata_community
citus62_96-debuginfo.x86_64                                                                          6.2.5.citus-1.fc26                                                                           citusdata_community
citus70_10.x86_64                                                                                    7.0.3.citus-1.fc26                                                                           citusdata_community
citus70_10-debuginfo.x86_64                                                                          7.0.3.citus-1.fc26                                                                           citusdata_community
citus70_96.x86_64                                                                                    7.0.3.citus-1.fc26                                                                           citusdata_community
citus70_96-debuginfo.x86_64                                                                          7.0.3.citus-1.fc26                                                                           citusdata_community
citus71_10.x86_64                                                                                    7.1.2.citus-1.fc26                                                                           citusdata_community
citus71_10-debuginfo.x86_64                                                                          7.1.2.citus-1.fc26                                                                           citusdata_community
citus71_96.x86_64                                                                                    7.1.2.citus-1.fc26                                                                           citusdata_community
citus71_96-debuginfo.x86_64                                                                          7.1.2.citus-1.fc26                                                                           citusdata_community
citus72_10.x86_64                                                                                    7.2.2.citus-1.fc26                                                                           citusdata_community
citus72_10-debuginfo.x86_64                                                                          7.2.2.citus-1.fc26                                                                           citusdata_community
citus72_96.x86_64                                                                                    7.2.2.citus-1.fc26                                                                           citusdata_community
citus72_96-debuginfo.x86_64                                                                          7.2.2.citus-1.fc26                                                                           citusdata_community
citus73_10.x86_64                                                                                    7.3.0.citus-1.fc26                                                                           citusdata_community
citus73_10-debuginfo.x86_64                                                                          7.3.0.citus-1.fc26                                                                           citusdata_community
citus73_96.x86_64                                                                                    7.3.0.citus-1.fc26                                                                           citusdata_community
citus73_96-debuginfo.x86_64                                                                          7.3.0.citus-1.fc26                                                                           citusdata_community
citus74_10.x86_64                                                                                    7.4.1.citus-1.fc26                                                                           citusdata_community
citus74_10-debuginfo.x86_64                                                                          7.4.1.citus-1.fc26                                                                           citusdata_community
citus74_96.x86_64                                                                                    7.4.1.citus-1.fc26                                                                           citusdata_community
citus74_96-debuginfo.x86_64                                                                          7.4.1.citus-1.fc26                                                                           citusdata_community
hll_10.x86_64                                                                                        2.10.2.citus-1.fc26                                                                          citusdata_community
hll_10-debuginfo.x86_64                                                                              2.10.2.citus-1.fc26                                                                          citusdata_community
hll_95.x86_64                                                                                        2.10.2.citus-1.fc26                                                                          citusdata_community
hll_95-debuginfo.x86_64                                                                              2.10.2.citus-1.fc26                                                                          citusdata_community
hll_96.x86_64                                                                                        2.10.2.citus-1.fc26                                                                          citusdata_community
hll_96-debuginfo.x86_64                                                                              2.10.2.citus-1.fc26                                                                          citusdata_community
pg_cron_10.x86_64                                                                                    1.0.2-1.fc26                                                                                 citusdata_community
pg_cron_10-debuginfo.x86_64                                                                          1.0.2-1.fc26                                                                                 citusdata_community
pg_cron_95.x86_64                                                                                    1.0.2-1.fc26                                                                                 citusdata_community
pg_cron_95-debuginfo.x86_64                                                                          1.0.2-1.fc26                                                                                 citusdata_community
pg_cron_96.x86_64                                                                                    1.0.2-1.fc26                                                                                 citusdata_community
pg_cron_96-debuginfo.x86_64                                                                          1.0.2-1.fc26                                                                                 citusdata_community
topn_10.x86_64                                                                                       2.0.2.citus-1.fc26                                                                           citusdata_community
topn_10-debuginfo.x86_64                                                                             2.0.2.citus-1.fc26                                                                           citusdata_community
topn_95.x86_64                                                                                       2.0.2.citus-1.fc26                                                                           citusdata_community
topn_95-debuginfo.x86_64                                                                             2.0.2.citus-1.fc26                                                                           citusdata_community
topn_96.x86_64                                                                                       2.0.2.citus-1.fc26                                                                           citusdata_community
topn_96-debuginfo.x86_64                                                                             2.0.2.citus-1.fc26                                                                           citusdata_community

I'd love to see a README.md update or perhaps just pg_cron packages available for Fedora 27 and 28.

how to turn of verbosity

from a user conversation

is there a way to turn off verbosity? if it runs every minute, there is a log every minute. 
For some we really don’t care if it succeeds

Any Reason why there can only be one job table inside schema cron on postgres or configured?

Hello I'm actually quite loving the pg_cron extensions, it allows for small users a really simple mechanism to create High Available Cron Jobs.

However managing these are quite a bit painful.
Is there any reason why the metadata tables is expected to be a single one?

Isn't there a way to actually do CREATE EXTENSION pg_cron; and it would be possible to select all the jobs of the current user, schedule/unschedule them?

Currently we use jdbc and if we don't want to open up a second connection to the postgres database, we would need to reconfigure the cron extension everywhere (well sadly we need to configure shared_preload_libraries anyway...).
this looks a little bit of an overhead, especially since we want to still use our flyway migrations to insert new cron jobs.
Is there any "best practice"/no-go on what's the best way to integrate pg_cron inside an application?

(At the moment we actually have a single cron.schedule that calls
SELECT pg_notify('our_channel', '{"type": "nightly"}'). Which we registerted with cron.schedule and then updated the cron.job database column.

pg_cron issues when a postgres process crashes

When postmaster reinitializes postgres processes because of the crash of one of them, pg_cron is terminated and is not subsequently restarted. The reason is that it supplies the BGW_NEVER_RESTART flag to the RegisterBackgroundWorker. I wonder why?

The other issue is that pg_cron continues to run when postmaster dies (i.e. killed by -9). The docs actually deal with that:

Backends which need to suspend execution only temporarily should use an interruptible sleep rather than exiting; this can be achieved by calling WaitLatch(). Make sure the WL_POSTMASTER_DEATH flag is set when calling that function, and verify the return code for a prompt exit in the emergency case that postgres itself has terminated.

That's quite easy to fix:

$ git diff
diff --git a/src/pg_cron.c b/src/pg_cron.c
index a7e42c5..8d4ab05 100644
--- a/src/pg_cron.c
+++ b/src/pg_cron.c
@@ -152,7 +152,7 @@ _PG_init(void)
    /* set up common data for all our workers */
    worker.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
    worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
-   worker.bgw_restart_time = BGW_NEVER_RESTART;
+   worker.bgw_restart_time = 1;
    worker.bgw_main = PgCronWorkerMain;
    worker.bgw_main_arg = Int32GetDatum(0);
    worker.bgw_notify_pid = 0;
@@ -544,16 +544,23 @@ ShouldRunTask(entry *schedule, TimestampTz currentTime, bool doWild,
 static void
 WaitForCronTasks(List *taskList)
 {
+   int     rc;
    int taskCount = list_length(taskList);

    if (taskCount > 0)
    {
        PollForTasks(taskList);
    }
-   else
+
+   /* wait for new jobs */
+   rc = WaitLatch(MyLatch,
+                  WL_LATCH_SET | WL_POSTMASTER_DEATH,
+                  taskCount > 0 ? 0: MaxWait, PG_WAIT_EXTENSION);
+   ResetLatch(MyLatch);
+   if (rc & WL_POSTMASTER_DEATH)
    {
-       /* wait for new jobs */
-       pg_usleep(MaxWait*1000L);
+       /* Postmaster died and we should bail out immediately */
+       proc_exit(1);
    }
 }

I think there should be a few additional changes, namely WaitLatch should go to the main loop and the signals should SetLatch as well. What do you think?

Add a way to run a job upon reception of a notification

While I'm not sure if this would still fit in the scope of this nice extension, I'd like to propose extending the existing infrastructure to allow scheduling of jobs on LISTEN channels that would run upon reception of an notification raised by an NOTIFY command. That way (and in combination with something like the tcn contrib module) one could design asynchronous triggers or similar constructs.

Log cron job results in a table

Currently, pg_cron does not give much feedback on the outcome of cron jobs other than what is available from the PostgreSQL log. Add logic to update the cron.result table as tasks progress. Optionally, add a cron job to clean up the table.

pg_cron causes segfault when trying to log error message

We're using a PostgreSQL 9.6.2-514.10.2.2 with pg_cron 1.0.0 (everything compiled from sources) on CentOS7. After some recent SQL changes the PostgreSQL server process started crashing quite often.

Enabling core dumps and opening the core file in GDB shows the following backtrace:

Program terminated with signal 11, Segmentation fault. 
#0  0x00007fecadb36694 in vfprintf () from /lib64/libc.so.6 
Missing separate debuginfos, use: debuginfo-install glibc-2.17-157.el7_3.1.x86_64 keyutils-libs-1.5.8-3.el7.x86_64 krb5-libs-1.14.1-27.el7_3.x86_64 libcom_err
-1.42.9-9.el7.x86_64 libselinux-2.5-6.el7.x86_64 libxml2-2.9.1-6.el7_2.3.x86_64 openssl-libs-1.0.1e-60.el7_3.1.x86_64 pcre-8.32-15.el7_2.1.x86_64 xz-libs-5.2.
2-1.el7.x86_64 zlib-1.2.7-17.el7.x86_64 
(gdb) bt 
#0  0x00007fecadb36694 in vfprintf () from /lib64/libc.so.6 
#1  0x00007fecadb62179 in vsnprintf () from /lib64/libc.so.6 
#2  0x00000000007e2243 in pvsnprintf (buf=0xd63930 "cron job 19  %s", len=len@entry=1024, fmt=fmt@entry=0xd63520 "cron job %ld %s",  
   args=args@entry=0x7fffcc92fea8) at psprintf.c:121 
#3  0x00000000005e16c4 in appendStringInfoVA (str=str@entry=0x7fffcc92fe90, fmt=fmt@entry=0xd63520 "cron job %ld %s", args=args@entry=0x7fffcc92fea8) 
   at stringinfo.c:130 
#4  0x00000000007ac1d1 in errmsg (fmt=fmt@entry=0x7feca57416c5 "cron job %ld %s") at elog.c:807 
#5  0x00007feca573f2ad in ManageCronTask (currentTime=552128823095161, task=0xe2d440) at src/pg_cron.c:1099 
#6  ManageCronTasks (currentTime=552128823095161, taskList=<optimized out>) at src/pg_cron.c:739 
#7  PgCronWorkerMain (arg=<optimized out>) at src/pg_cron.c:250 
#8  0x0000000000656f5e in StartBackgroundWorker () at bgworker.c:725 
#9  0x0000000000661d7d in do_start_bgworker (rw=0xd86850) at postmaster.c:5537 
#10 maybe_start_bgworker () at postmaster.c:5712 
#11 0x00000000006621fe in reaper (postgres_signal_arg=<optimized out>) at postmaster.c:2812 
#12 <signal handler called> 
#13 0x00007fecadbdcb83 in __select_nocancel () from /lib64/libc.so.6 
#14 0x000000000046d75a in ServerLoop () at postmaster.c:1665 
#15 0x0000000000663a06 in PostmasterMain (argc=argc@entry=5, argv=argv@entry=0xd612d0) at postmaster.c:1309 
#16 0x000000000046e8ea in main (argc=5, argv=0xd612d0) at main.c:228

We suspect the segfault is triggered by a use-after-free caused by this code block:

                                        case PGRES_FATAL_ERROR:
                                        {
                                                task->errorMessage = PQresultErrorMessage(result);
                                                task->pollingStatus = 0;
                                                task->state = CRON_TASK_ERROR;

                                                PQclear(result);

                                                return;
                                        }

Since PQclear() releases all memory associated with the result, task.errorMessage points to released memory after exiting this block.

Latest pg_cron on APT (1.0.2) does not work as expected in Postgres 9.6

I found apt.postgresql.org is still with 1.0.2-1.pgdg16.04+1 (BTW, my OS is Ubuntu 16.04 LTS), and I just noticed that even if I have installed it as instructed ( except promoting the server because I do not know what is "promote"), and all jobs will not be executed by time automatically.

So it is about the version of pg_cron or is it about the way I set it up?

PS: I am sure I have set up trust security level to local DB connections

segmentation fault without job run

We watched a single pg_cron crashing. Half an hour before all jobs have were stopped silently.
At crash time, there was a high load due to autovacuum processes.

2017-01-24 18:24:03 MSK 12541 LOG: worker process: pg_cron_scheduler (PID 12551) was terminated by signal 11: Segmentation fault

PostgreSQL 9.6.1 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, 64-bit
pg_cron downloaded 22 dec 2016

_usr_lib_postgresql_9.6_bin_postgres.103.crash.gz
https://drive.google.com/file/d/0Bz8C5tvsnKrETWdKbmhDcENTTUE/view?usp=sharing

Jobs don't run at specified time

I just installed pg_cron and it all seems ok but I can't set specified times to run my jobs.

If I set:
select cron.schedule ('*/1 * * * *', $$insert into test (text) values (current_timestamp)$$);
It works

If i set a fixed time:
select cron.schedule ('30 15 * * *', $$insert into test (text) values (current_timestamp)$$);
Nothing happens.

Am I missing something?

Clarification required pg_cron scheduling for long running jobs

Say there is a materialized view that is built on top of mysql_fdw which gets refreshed every minute,
If the refresh takes more than a minute to execute what is the default behaviour of pg_cron?

  • The job gets silently killed (since the previous op didn't finish.) The job will get rescheduled anyway.
  • The job get queued => this saturated all available postgres connections on my machine when I tried with manual crontab not pg_cron?

Allow enable/disable a job

I would like to have an extra column in cron.job like enabled as boolean.

Such feature will allow to disable a job without alter the schedule or something else.

Thanks you!

Does not run the task when specifying a schedule.

Install and compile the module without problems, but when creating a task, I only run those I program every 1 minute (* * * * *), but if the same task I put a specific time to run a task every day For example: (30 17 * * *) does not run, does not even leave a trace in the log, is this normal? Thank you.

Users can unschedule other user's jobs

Users can have their own cron jobs and are unable to see other user's cron jobs. However, SELECT cron.unschedule(..) currently does not check whether the user is the owner of the cron job.

Documentation Clarification

I'm trying to understand if pg_cron is suitable for my needs and so want to clarify some items from documentation.

In particular

By default, the pg_cron background worker expects its metadata tables to be created in the "postgres" database. Does this really mean database and not schema?

My setup is as follows

  1. Hot-Warm Cluster
  2. N databases on the nodes
  3. Each databases has 2 schemas (public and app)

Is pg_cron suitable for my setup e.g. multi-tenant nothing shared ?

If it is suitable I'm assuming that I'll end up with N pg_cron schemas and N cron.job tables? Is that correct?

Thank you.

Identify scheduler process in pg_stat_activity using application_name

Right now pg_stat_activity contains the following information about the scheduler process, which makes it a bit hard to understand that this is pg_cron:

datid            | 16400
datname          | citus
pid              | 2341
usesysid         | 10
usename          | postgres
application_name | 
client_addr      | 
client_hostname  | 
client_port      | 
backend_start    | 2017-11-24 22:23:03.661549+00
xact_start       | 
query_start      | 
state_change     | 2017-11-24 22:23:03.664681+00
wait_event_type  | Extension
wait_event       | Extension
state            | idle
backend_xid      | 
backend_xmin     | 
query            | 
backend_type     | background worker
# ps aux | grep 2341
postgres  2341  0.0  0.3 857308  7516 ?        Ss   22:23   0:00 postgres: bgworker: pg_cron_scheduler 

Note that we set application_name to Citus Maintenance Daemon for the citus background worker, so I think it'd be appropriate to do the same for pg_cron, e.g. set it to pg_cron Scheduler

how to install pg_cron in Windows 10

Hello,

could I ask if there is any option to install pg_cron in PostgreSQL 9.6 which runs on my Windows 10 PC (operating system) ?

Thank you in advance!

Regards,
Vasilis

Add a way to "name" a job and then update the schedule for that specific job

We have a use case where we want to be able to update a job's schedule based on a job's name (i.e., a fixed attribute that is set from a static config rather than a dynamic ID value).

I think it would be really useful to add an optional unique "job_name" column that would allow jobs to be referenced by a static string.

So something like:

SELECT cron.schedule('updateStats', '0 * * * *', 'REFRESH MATERIALIZED VIEW CONCURRENTLY user_stats');

Then have an ability to do something like:

SELECT cron.updateSchedule('updateStats', '0 1 * * *');

Thanks!

Installing pg_cron

Hello:

I need some clarity about installing pg_cron on AWS Aurora Postgresql cluster (multi node).

Not sure if there is any other way to connect to instance using PUTTY and i am not able to do that.

Installation instruction are simple of here but not is respect to AWS Aurora Postgresql cluster and how to do that.

Please help if you can.

Thanking in advance.

Error on creating extension in pipeline db

Hi,

I have installed pipeline (9.5.3) in my mac.

$ pipeline-ctl -V
pg_ctl (PostgreSQL) 9.5.3
 $ 

Also added the below in /usr/local/lib/pipelinedb/data/pipelinedb.conf which is an equivalent of postgres.conf.

shared_preload_libraries = 'pg_cron'            # (change requires restart)
cron.database_name = 'pipeline'

Did restart the pipelinedb using below

pipeline-ctl -D pipelinedb-data/ -l logfile restart

Still getting below error

 $ psql -p 5432 -h localhost pipeline
psql (9.5.3)
Type "help" for help.

pipeline=# CREATE EXTENSION pg_cron;
ERROR:  unrecognized configuration parameter "cron.database_name"
CONTEXT:  PL/pgSQL function inline_code_block line 3 at IF
pipeline=# 

Pg_cron crontab log

We're trying to configure periodic jobs in PostgreSQL.
To do this, we installed on Linux machine, with Postgres 9.6 running, the pg_cron project.

System information:

OS: Linux pg 4.4.0-72-generic #93-Ubuntu SMP
PG: Postgres 9.6.3 installed from repo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main'

Following the instructions in the pg_cron repository, we set in postgresql.conf the configuration below:

shared_preload_libraries = 'pg_cron'
cron.database_name = 'our db_name'

Then, on db_name, we created the EXTENSION pg_cron

CREATE EXTENSION pg_cron;
and we scheduled our first Postgres job:

SELECT cron.schedule('45 12 * * *', $$CREATE TABLE schema.testCron AS Select 'Test Cron' as Cron$$);
So, jobid 1 was created and listed in table cron.job.

We expected that at 12:45 the command of the scheduled job would have been launched. But nothing happened.

The testCron table was not created and we had no trace in any logs.

We also defined LOG_FILE in /usr/src/pg_cron/include/pathnames.h to enable logging.

But, after re-compiling the project and restarting the Postgres service, we did not track log for pg_cron.

How can we enable logs for pg_cron to check scheduling result?

Thanks in advance!

Killing pg_cron process will restart other background processes

[yangjie@young-1 bin]$ ps x
  PID TTY      STAT   TIME COMMAND
15907 pts/3    S+     0:00 ./psql
16091 pts/4    S      0:00 /work/hgdb/hgdb-pure/bin/postgres -D ../data
16092 ?        Ss     0:00 postgres: logger process   
16094 ?        Ss     0:00 postgres: checkpointer process   
16095 ?        Ss     0:00 postgres: writer process   
16096 ?        Ss     0:00 postgres: wal writer process   
16097 ?        Ss     0:00 postgres: autovacuum launcher process   
16098 ?        Ss     0:00 postgres: stats collector process   
16099 ?        Ss     0:00 postgres: bgworker: pg_cron_scheduler   
16101 ?        Ss     0:00 postgres: bgworker: logical replication launcher  
16105 ?        Ss     0:00 postgres: yangjie yangjie [local] idle
16106 pts/4    R+     0:00 ps x
[yangjie@young-1 bin]$ kill -9 16099
[yangjie@young-1 bin]$ ps x
  PID TTY      STAT   TIME COMMAND
15907 pts/3    S+     0:00 ./psql
16091 pts/4    S      0:00 /work/hgdb/hgdb-pure/bin/postgres -D ../data
16092 ?        Ss     0:00 postgres: logger process   
16119 ?        Ss     0:00 postgres: checkpointer process   
16120 ?        Ss     0:00 postgres: writer process   
16121 ?        Ss     0:00 postgres: wal writer process   
16122 ?        Ss     0:00 postgres: autovacuum launcher process   
16123 ?        Ss     0:00 postgres: stats collector process   
16124 ?        Ss     0:00 postgres: bgworker: pg_cron_scheduler   
16126 ?        Ss     0:00 postgres: bgworker: logical replication launcher  
16130 pts/4    R+     0:00 ps x
[yangjie@young-1 bin]$ 

Killing pg_cron process will restart other background processes.

Unable to compile

I'm trying to include pg_cron within my Alpine Linux Postgresql image.
Tried compiling on alpine:3.4 and alpine:edge.

Both cases same result.

Anyone any suggestion why I'm unable to compile ?

I'm not adding a path to the build command like within the README, because within Alpine all postgres commands are installed within /usr/bin

bash-4.3# make
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Os -fomit-frame-pointer -fpic -std=c99 -Wall -Wextra -Werror -Wno-unused-parameter -Iinclude -I/usr/include -I. -I./ -I/usr/include/postgresql/server -I/usr/include/postgresql/internal -Os -fomit-frame-pointer -D_GNU_SOURCE -I/usr/include/libxml2   -c -o src/entry.o src/entry.c
In file included from src/entry.c:30:0:
include/cron.h:222:17: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__P'
 void unget_char __P((int, FILE *)),
                 ^~~
include/cron.h:226:15: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__P'
 int  get_char __P((FILE *)),
               ^~~
src/entry.c:38:22: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__P'
 static char get_list __P((bitstr_t *, int, int, char *[], int, FILE *)),
                      ^~~
src/entry.c:41:24: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__P'
 static int set_element __P((bitstr_t *, int, int, int));
                        ^~~
src/entry.c:45:1: error: no previous prototype for 'free_entry' [-Werror=missing-prototypes]
 free_entry(e)
 ^~~~~~~~~~
src/entry.c: In function 'parse_cron_entry':
src/entry.c:96:2: error: implicit declaration of function 'skip_comments' [-Werror=implicit-function-declaration]
  skip_comments(file);
  ^~~~~~~~~~~~~
src/entry.c:98:7: error: implicit declaration of function 'get_char' [-Werror=implicit-function-declaration]
  ch = get_char(file);
       ^~~~~~~~
src/entry.c:122:8: error: implicit declaration of function 'get_string' [-Werror=implicit-function-declaration]
   ch = get_string(cmd, MAX_COMMAND, file, " \t\n");
        ^~~~~~~~~~
src/entry.c:168:8: error: implicit declaration of function 'get_list' [-Werror=implicit-function-declaration]
   ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
        ^~~~~~~~
src/entry.c: At top level:
src/entry.c:244:1: error: conflicting types for 'get_list'
 get_list(bits, low, high, names, ch, file)
 ^~~~~~~~
src/entry.c:168:8: note: previous implicit declaration of 'get_list' was here
   ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
        ^~~~~~~~
src/entry.c: In function 'get_list':
src/entry.c:272:8: error: implicit declaration of function 'get_range' [-Werror=implicit-function-declaration]
   ch = get_range(bits, low, high, names, ch, file);
        ^~~~~~~~~
src/entry.c: At top level:
src/entry.c:291:1: error: conflicting types for 'get_range'
 get_range(bits, low, high, names, ch, file)
 ^~~~~~~~~
src/entry.c:272:8: note: previous implicit declaration of 'get_range' was here
   ch = get_range(bits, low, high, names, ch, file);
        ^~~~~~~~~
src/entry.c: In function 'get_range':
src/entry.c:315:20: error: implicit declaration of function 'get_number' [-Werror=implicit-function-declaration]
   if (EOF == (ch = get_number(&num1, low, names, ch, file)))
                    ^~~~~~~~~~
src/entry.c:328:15: error: implicit declaration of function 'set_element' [-Werror=implicit-function-declaration]
    if (EOF == set_element(bits, low, high, num1))
               ^~~~~~~~~~~
src/entry.c: At top level:
src/entry.c:394:1: error: conflicting types for 'get_number'
 get_number(numptr, low, names, ch, file)
 ^~~~~~~~~~
src/entry.c:315:20: note: previous implicit declaration of 'get_number' was here
   if (EOF == (ch = get_number(&num1, low, names, ch, file)))
                    ^~~~~~~~~~
src/entry.c:453:1: error: static declaration of 'set_element' follows non-static declaration
 set_element(bits, low, high, number)
 ^~~~~~~~~~~
src/entry.c:328:15: note: previous implicit declaration of 'set_element' was here
    if (EOF == set_element(bits, low, high, num1))
               ^~~~~~~~~~~
src/entry.c:453:1: error: 'set_element' defined but not used [-Werror=unused-function]
 set_element(bits, low, high, number)
 ^~~~~~~~~~~
src/entry.c:394:1: error: 'get_number' defined but not used [-Werror=unused-function]
 get_number(numptr, low, names, ch, file)
 ^~~~~~~~~~
src/entry.c:291:1: error: 'get_range' defined but not used [-Werror=unused-function]
 get_range(bits, low, high, names, ch, file)
 ^~~~~~~~~
src/entry.c:244:1: error: 'get_list' defined but not used [-Werror=unused-function]
 get_list(bits, low, high, names, ch, file)
 ^~~~~~~~
cc1: all warnings being treated as errors
make: *** [<builtin>: src/entry.o] Error 1
bash-4.3#

timezone problem

my db‘s timezone

db=# show timezone ;
TimeZone

PRC
(1 row)

when i insert some record to cron.job,find that exection time is not the db‘s default timezone(RPC).
it seems that using UTC.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.