In database management, job scheduling has always played an important role. And for PostgreSQL, PgAgent and pg_cron are examples of job schedulers that are already popular. However, there is another job scheduler called pg_timetable, which is completely database-driven and provides a couple of advanced concepts. In this blog, we are going to highlight some of the main features, installation, and some use cases of pg_timetable.

Main features of pg_timetable

  • Fully database-driven configuration
  • Cron-style scheduling at the PostgreSQL server time zone
  • Built-in tasks such as sending emails, etc.
  • Full support for database-driven logging
  • Tasks can be arranged in chains
  • A chain can consist of built-in commands, SQL, and executables
  • Parameters can be passed to chains/tasks

Installation

There are currently two options on how you can install and run pg_timetable.

  1. Container installation. For this, please refer to this page.
    Note: The image mentioned on the above page is based on Alpine and not from Percona.
  2. Local installation, i.e., build from sources. Here, we will discuss this approach.

2.1. Download and install GO on your system.

2.2. Clone pg_timetable repo

2.3. Run pg_timetable

2.4. Alternatively, build a binary and run it:

Demonstration of job scheduling with pg_timetable

As a use case here, it will be shown how to use pg_timetable as a scheduler to schedule a job, which will refresh the materialized view every day at 12 midnight.

1. Download pg_timetable executable (Follow step 2 mentioned above in the installation section).

2. Make sure the PostgreSQL server is up and running and has a role with CREATE privilege for a target database, e.g.:

3. Create a new job to refresh the materialized view each night at 12:00 Postgres server time zone.

4. Run the pg_timetable

During the first start of pg_timetable, the necessary schema timetable gets created. For reference, below is the catalog structure.

5. From database logs, it can be observed that MATERIALIZED VIEW gets refreshed as per the schedule.

Output from DB logs:

From psql prompt:

Below is the output from the pg_timetable catalog tables.

In conclusion, we can say that pg_timetable is open source and can be used freely by everyone. The main advantages include that pg_timetable is an independent process written in GO, which connects to PostgreSQL just like any other client program. So, if the scheduler crashes, it will not harm your server. Pg_timetable provides a variety of built-in tasks that help you to flexibly combine those operations in an easy way. Further, pg_timetable has been implemented in GO and, therefore, comes as ONE executable that can be started directly. So, there is no need to worry about libraries or dependencies during the installation.

Please refer to the links below to learn more about pg_timetable.

https://github.com/cybertec-postgresql/pg_timetable

https://www.cybertec-postgresql.com/en/products/pg_timetable/

https://pg-timetable.readthedocs.io/en/master/README.html

Percona Distribution for PostgreSQL provides the best and most critical enterprise components from the open-source community, in a single distribution, designed and tested to work together.

 

Download Percona Distribution for PostgreSQL Today!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments