Running a Postgresql Query at a specific time

时间:2017-09-28 08:34:39

标签: postgresql datetime triggers cron

Scenario

I have a table which contains tasks that need to be completed by a specific datetime. If the task is not completed by this datetime (+- variable interval) then it will run a script to 'escalate' this task. This variable interval can be as small as 2 seconds or as large as 2 years

Thoughts so far

Running a cron job every second either via pg_cron or similar will technically allow me to do a check on the database every second, however there is a lot of wasted processing here and a lot of database overhead and i'd rather not do this if possible.

Triggers can be fired on row insert/update/delete. so worst case scenario is we have an external script watching for these triggers being fired.

Question

Is there a way to schedule a query to run at a specific time, ideally within postgresql itself rather than via a bash/cron script. ie:

at 2017-09-30 09:32:00 - select * from table where datetime <= now

Edit

As it came up in the comments PGAgent is a possibility and the scenario for such would be:

The task is created by the user in the application, and the due date is set (eg 2017-09-28 13:00:00) the user has an interval before/after this due date where the task is escalated (eg One Hour Before) so at 12:00:00 on 2017-09-28 i want PGAgent/other option to run my sql script that does the escalation.

The script to escalate is already written and works, the date and time for this PGAgent script to be run is already calculated by another script.

0 个答案:

没有答案