pg_cron icon indicating copy to clipboard operation
pg_cron copied to clipboard

pg_cron hangs when cron.max_running_jobs is exceeded

Open laurenz opened this issue 6 years ago • 1 comments

There is a bug in the state machine that causes pg_cron to indefinitely wait for jobs that it thinks are running, but are actually idle. I can't tell exactly where the bug is, but it happens when max_running_jobs is exceeded.

Here is a way to reproduce it:

  • Set cron.max_running_jobs = 1 in postgresql.conf and start PostgreSQL.

  • Define two jobs:

    SELECT cron.schedule('* * * * *', 'SELECT pg_sleep(30)');
    SELECT cron.schedule('* * * * *', 'SELECT pg_sleep(30)');
    

First one of the jobs is started. When that one is done, the other one is started. The second job never finishes, although the connection has become idle.

We end up with max_running_jobs idle connections that completely block pg_cron.

Attaching to the pg_cron with gdb shows an endless repetition of

poll([{fd=-1}], 1, 1000)                = 0 (Timeout)

laurenz avatar Oct 18 '18 19:10 laurenz

I have opened a pull request for a patch that fixed the issue for me.

Not sure if that patch is correct though.

laurenz avatar Oct 19 '18 07:10 laurenz