Database timed tasks can be used to implement regular backups, statistics collection, data aggregation, data cleaning and optimization, etc. PostgreSQL does not provide built-in task scheduling features similar to Oracle, MySQL and Microsoft SQL Sever, so this article introduces you to 4 ways to implement timed tasks in PostgreSQL database.
OS Timed Tasks
Linux timed tasks (crontab) or Windows Task Scheduler provides us with a traditional way to implement timed tasks. Taking crontab as an example, we can edit the task list using the following command.
|
|
Then add a line of data in the open file using the following format.
The first five of these fields indicate when the command was executed, and you can use an asterisk (*) to match all times. For example, setting <month>
to an asterisk indicates that the command is executed every month.
For example, enter the following to indicate that the database logical backup operation is executed at zero every day.
|
|
For security reasons, instead of entering the password directly, you should add it to the .pgpass file and set the permissions of the file to be visible only to the current user.
|
|
pgAgent
pgAgent is a task scheduling agent for PostgreSQL databases, capable of running multi-step batch processes, shell scripts, and SQL commands based on complex scheduling plans. For Unix/Linux systems, pgAgent runs as a background process; for Windows systems, pgAgent runs as a service.
Install pgAgent
The PgAdmin 4 administration tool integrates with pgAgent, but both need to be installed separately. We can download PgAdmin 4 and pgAgent from the official website, and you can refer to the official documentation for installation steps and notes. After the installation is complete, we can see the “pgAgent Jobs” node in the left navigation tree of PgAdmin 4.
Create a timed job
Right-click on the “pgAgent Jobs” node and select “Create” > “pgAgent Job” to create a new timed job.
The “General” screen allows you to enter some basic information, including the name of the task. The “Steps” screen allows you to set multiple steps, including scripts or SQL statements to be executed. The “Schedules” screen is used to define the time schedule for the task execution. The “SQL” screen displays the statements that will be used to create or modify the task.
Click the “Save” button to save the settings and create the job, then we can see the created job under the “pgAgent Job” node.
pg_cron
pg_cron is a PostgreSQL timed task plugin (similar to DBMS_SCHEDULER in Oracle) developed by citusdata. pg_cron runs as a background worker, using a cron-like editing syntax, allowing timed tasks to be executed directly in the database. Example.
|
|
Install pg_cron
pg_cron currently only supports Linux operating systems. For Red Hat, CentOS, and Fedora operating systems the following commands can be used to install (PostgreSQL 12).
|
|
For Debian and Ubuntu the following command can be used to install (PostgreSQL 12).
|
|
Alternatively, we can use the source code for compilation and installation.
Configuring pg_cron
In order to run the pg_cron background worker process when starting PostgreSQL, we need to add pg_cron to the shared_preload_libraries configuration entry in the postgresql.conf file. By default, the pg_cron background process uses the postgres database to get the metadata it needs. However, we can also set this using the cron.database_name configuration parameter.
Restarting PostgreSQL.
|
|
We can then use the following command to create the pg_cron function and the metadata related tables.
pg_timetable
pg_timetable is a PostgreSQL job scheduler developed by CYBERTEC that provides a flexible configuration approach and many advanced features. These include task chains consisting of multiple tasks, support for SQL commands and executables, built-in tasks (e.g. sending emails), fully database-based configuration and logging capabilities, cron-style scheduled scheduling, protection against concurrent execution, etc.
Install pg_timetable
First, we can install pg_timetable using the official binary installer, which currently supports Windows, Linux, and macOS operating systems.
Alternatively, the official docker image can be downloaded here.
The latest tab of the master branch is the latest version, and is run using the command line as follows.
Specify the environment variables in the following way.
In addition, we can also use the source code to compile and install. First download and install the Go language environment, and then copy the pg_timetable source code using the go get command.
Run pg_timetable.
Alternatively, it can be compiled into a binary program and run.
If you want to run all the tests in your project, you can execute the following command.
You can also run tests using a postgres docker image.
|
|
Using pg_timetable
pg_timetable runs independently of the PostgreSQL server and is equivalent to a client process. After installation, run the pg_timetable program by executing the following command.
|
|
A number of examples are provided in the sample directory of the source code for reference. The following command creates a timed task that runs “MyJob” at 00:05 in August.
|
|
The following command runs the “MyJob” task at 23 minutes of every two hours from 0:00 to 20:00.
|
|
The complete configuration of the pg_timetable scheduled task consists of 3 phases.
-
The first phase is used to configure the base_task, defining the operations that need to be executed. This includes SQL statements, external procedures, and built-in operations.
-
The second stage is used to configure task_chain, which defines a set of basic tasks to be executed sequentially.
-
The third stage is used to configure chain_execution_config, which defines the execution plan of the task chain.
In addition, in order to pass control parameters to the basic tasks, each task in the task chain can be accompanied by an execution parameter. For detailed configuration methods and examples, please refer to the official website.
Summary
This article introduced 4 ways to implement timed tasks in PostgreSQL database, including OS timed tasks, pgAgent agent, pg_cron plug-in and pg_timetable tool.