All Projects → cybertec-postgresql → Pg_timetable

cybertec-postgresql / Pg_timetable

Licence: other
pg_timetable: Advanced scheduling for PostgreSQL

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Pg timetable

Nut
Advanced, Powerful and easy to use ORM for Qt
Stars: ✭ 181 (-52.62%)
Mutual labels:  sql, database, postgresql
Scany
Library for scanning data from a database into Go structs and more
Stars: ✭ 228 (-40.31%)
Mutual labels:  sql, database, postgresql
Npgsql
Npgsql is the .NET data provider for PostgreSQL.
Stars: ✭ 2,415 (+532.2%)
Mutual labels:  sql, database, postgresql
Rel
💎 Modern Database Access Layer for Golang - Testable, Extendable and Crafted Into a Clean and Elegant API
Stars: ✭ 317 (-17.02%)
Mutual labels:  sql, database, postgresql
Jet
Type safe SQL builder with code generation and automatic query result data mapping
Stars: ✭ 373 (-2.36%)
Mutual labels:  sql, database, postgresql
Sqlcheck
Automatically identify anti-patterns in SQL queries
Stars: ✭ 2,062 (+439.79%)
Mutual labels:  sql, database, postgresql
Db
Data access layer for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.
Stars: ✭ 2,832 (+641.36%)
Mutual labels:  sql, database, postgresql
Mysql Container
MySQL container images based on Red Hat Software Collections and intended for OpenShift and general usage. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
Stars: ✭ 117 (-69.37%)
Mutual labels:  sql, database, docker-image
Dbeaver
Free universal database tool and SQL client
Stars: ✭ 23,752 (+6117.8%)
Mutual labels:  sql, database, postgresql
Sqlfiddle3
New version based on vert.x and docker
Stars: ✭ 242 (-36.65%)
Mutual labels:  sql, database, postgresql
Querybuilder
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
Stars: ✭ 2,111 (+452.62%)
Mutual labels:  sql, database, postgresql
Squeal
Squeal, a deep embedding of SQL in Haskell
Stars: ✭ 308 (-19.37%)
Mutual labels:  sql, database, postgresql
Stratosdb
☄️ ☁️ An All-in-One GUI for Cloud SQL that can help users design and test their AWS RDS Instances
Stars: ✭ 140 (-63.35%)
Mutual labels:  sql, database, postgresql
Linq2db
Linq to database provider.
Stars: ✭ 2,211 (+478.8%)
Mutual labels:  sql, database, postgresql
Kangaroo
SQL client and admin tool for popular databases
Stars: ✭ 127 (-66.75%)
Mutual labels:  sql, database, postgresql
Shardingsphere
Build criterion and ecosystem above multi-model databases
Stars: ✭ 14,989 (+3823.82%)
Mutual labels:  sql, database, postgresql
Postguard
🐛 Statically validate Postgres SQL queries in JS / TS code and derive schemas.
Stars: ✭ 104 (-72.77%)
Mutual labels:  sql, database, postgresql
Alpine Mariadb
MariaDB running on Alpine Linux [Docker]
Stars: ✭ 117 (-69.37%)
Mutual labels:  sql, database, docker-image
Massive Js
A data mapper for Node.js and PostgreSQL.
Stars: ✭ 2,521 (+559.95%)
Mutual labels:  sql, database, postgresql
Bitnami Docker Mariadb
Bitnami MariaDB Docker Image
Stars: ✭ 251 (-34.29%)
Mutual labels:  sql, database, docker-image

We are actively developing pg_timetable v4. Please refer to the v3 branch for previous version documentation and sources.


License: MIT Coverage Status Go Report Card Release Mentioned in Awesome Go Docker Pulls Dependabot Status

pg_timetable: Advanced scheduling for PostgreSQL

pg_timetable is an advanced job scheduler for PostgreSQL, offering many advantages over traditional schedulers such as cron and others. It is completely database driven and provides a couple of advanced concepts.

# ./pg_timetable

Application Options:
  -c, --clientname=               Unique name for application instance
  -v, --verbose                   Show verbose debug information [$PGTT_VERBOSE]
  -h, --host=                     PG config DB host (default: localhost) [$PGTT_PGHOST]
  -p, --port=                     PG config DB port (default: 5432) [$PGTT_PGPORT]
  -d, --dbname=                   PG config DB dbname (default: timetable) [$PGTT_PGDATABASE]
  -u, --user=                     PG config DB user (default: scheduler) [$PGTT_PGUSER]
  -f, --file=                     SQL script file to execute during startup
      --password=                 PG config DB password (default: somestrong) [$PGTT_PGPASSWORD]
      --sslmode=[disable|require] What SSL priority use for connection (default: disable)
      --pgurl=                    PG config DB url [$PGTT_URL]
      --init                      Initialize database schema and exit. Can be used with --upgrade
      --upgrade                   Upgrade database to the latest version
      --no-program-tasks            Disable executing of PROGRAM tasks [$PGTT_NOPROGRAMTASKS]

Table of Contents

1. Main features

  • Tasks can be arranged in chains
  • A chain can consist of SQL and executables
  • Parameters can be passed to chains
  • Missed tasks (possibly due to downtime) can be retried automatically
  • Support for configurable repetitions
  • Builtin tasks such as sending emails, etc.
  • Fully database driven configuration
  • Full support for database driven logging
  • Cron-style scheduling
  • Optional concurrency protection

2. Installation

pg_timetable is compatible with the latest supported PostgreSQL versions: 11, 12 and 13.

If you want to use pg_timetable with older versions (9.5, 9.6 and 10)...

please, execute this SQL script before running pg_timetable:

CREATE OR REPLACE FUNCTION starts_with(text, text)
RETURNS bool AS 
$$
SELECT 
	CASE WHEN length($2) > length($1) THEN 
		FALSE 
	ELSE 
		left($1, length($2)) = $2 
	END
$$
LANGUAGE SQL
IMMUTABLE STRICT PARALLEL SAFE
COST 5;

2.1 Official release packages

You may find binary package for your platform on the official Releases page. Right now Windows, Linux and macOS packages are available.

2.2 Docker

The official docker image can be found here: https://hub.docker.com/r/cybertecpostgresql/pg_timetable

The latest tag is up to date with the master branch thanks to this github action.

CLI:

docker run --rm \
  cybertecpostgresql/pg_timetable:latest \
  -h 10.0.0.3 -p 54321 -c worker001

Environment variables:

docker run --rm \
  -e PGTT_PGHOST=10.0.0.3 \
  -e PGTT_PGPORT=54321 \
  cybertecpostgresql/pg_timetable:latest \
  -c worker001

2.3 Build from sources

  1. Download and install Go on your system.
  2. Clone pg_timetable using go get:
$ env GIT_TERMINAL_PROMPT=1 go get github.com/cybertec-postgresql/pg_timetable/
Username for 'https://github.com': <Github Username>
Password for 'https://[email protected]': <Github Password>
  1. Run pg_timetable:
$ cd ~/go/src/github.com/cybertec-postgresql/pg_timetable/
$ go run main.go --dbname=dbname --clientname=worker001 --user=scheduler --password=strongpwd

Alternatively, build a binary and run it:

$ go build
$ ./pg_timetable --dbname=dbname --clientname=worker001 --user=scheduler --password=strongpwd
  1. (Optional) Run tests in all sub-folders of the project:
$ cd ~/go/src/github.com/cybertec-postgresql/pg_timetable/
$ go get github.com/stretchr/testify/
$ go test ./...

Alternatively, run tests using postgres docker image:

$ RUN_DOCKER=true go test ./...

3. Features and advanced functionality

The scheduling in pg_timetable encompasses three different stages to facilitate the reuse with other parameters or additional schedules.

The first stage, base_task, defines what to do.
The second stage, task_chain, contains a list of base tasks to run sequentially.
The third stage consists of the chain_execution_config and defines if, when, and how often a chain should be executed.

Additionally, to provide the base tasks with parameters and influence their behavior, each entry in a task chain can be accompanied by an execution parameter.

3.1. Base task

In pg_timetable, the most basic building block is a base task. Currently, there are three different kinds of task:

Base task kind Task kind type Example
SQL snippet SQL Starting a cleanup, refreshing a materialized view or processing data.
External program PROGRAM Anything that can be called as an external binary, including shells, e.g. bash, pwsh, etc.
Internal Task BUILTIN A prebuilt functionality included in pg_timetable. These include:
  • Sleep
  • Log
  • SendMail
  • Download

A new base task can be created by inserting a new entry into timetable.base_task.

Excerpt of timetable.base_task

Column Type Definition
name text The name of the base task.
kind timetable.task_kind The type of the base task. Can be SQL(default), PROGRAM or BUILTIN.
script text Contains either a SQL script or a command string which will be executed.

3.2. Task chain

The next building block is a chain, which simply represents a list of tasks. An example would be:

  • Download files from a server
  • Import files
  • Run aggregations
  • Commit the transaction
  • Remove the files from disk

All tasks of the chain in pg_timetable are executed within one transaction. However, please, pay attention there is no opportunity to rollback PROGRAM and BUILTIN tasks.

Excerpt of timetable.task_chain

Column Type Definition
parent_id bigint The ID of the previous chain task. Set this to NULL if it is the first base task in the chain.
task_id bigint The ID of the base task.
run_uid text The role as which the chain should be executed as.
database_connection integer The ID of the timetable.database_connection that should be used.
ignore_error boolean Specify if the chain should resume after encountering an error (default: false).

If the chain has been configured with ignore_error set to true (the default value is false), the worker process will report a success on execution even if the task within the chain fails.

3.2.1. Chain execution configuration

Once a chain has been created, it has to be scheduled. For this, pg_timetable builds upon the standard cron-string, all the while adding multiple configuration options.

Excerpt of timetable.chain_execution_config

Column Type Definition
chain_id bigint The id of the task chain.
chain_name text The name of the chain.
run_at timetable.cron To achieve the cron equivalent of *, set the value to NULL.
max_instances integer The amount of instances that this chain may have running at the same time.
live boolean Control if the chain may be executed once it reaches its schedule.
self_destruct boolean Self destruct the chain.
exclusive_execution boolean Specifies whether the chain should be executed exclusively while all other chains are paused.
excluded_execution_configs integer[] TODO
client_name text Specifies which client should execute the chain. Set this to NULL to allow any client.

3.2.2. Chain execution parameters

As mentioned above, base tasks are simple skeletons (e.g. send email, vacuum, etc.). In most cases, they have to be brought to live by passing parameters to the execution.

Excerpt of timetable.chain_execution_parameters

Column Type Definition
chain_execution_config bigint The ID of the chain execution configuration.
chain_id bigint The ID of the chain.
order_id integer The order of the parameter.
value jsonb A string JSON array containing the parameters.

3.3 Example usages

A variety of examples can be found in the /samples directory.

3.4 Example functions

Create a Job with the timetable.job_add function. With this function you can add a new one step chain with a cron-syntax.

Parameter Type Definition Default
task_name text The name of the Task
task_function text The function which will be executed.
client_name text Specifies which client should execute the chain. Set this to NULL to allow any client. NULL
task_type text Type of the function SQL,PROGRAM and BUILTIN SQL
run_at timetable.cron Time schedule in сron syntax. NULL stands for '* * * * *' NULL
max_instances integer The amount of instances that this chain may have running at the same time. NULL
live boolean Control if the chain may be executed once it reaches its schedule. FALSE
self_destruct boolean Self destruct the chain. FALSE

3.5 Usage

Run "MyJob" at 00:05 in August. SELECT timetable.job_add('MyJob', 'SELECT public.my_func()' , NULL, 'SQL', '5 0 * 8 *', live := TRUE);

Run "MyJob" at minute 23 past every 2nd hour from 0 through 20. SELECT timetable.job_add('MyJob', 'SELECT public.my_func()' , NULL, 'SQL', '23 0-20/2 * * *', live := TRUE);

4. Database logging and transactions

The entire activity of pg_timetable is logged in database tables (timetable.log and timetable.execution_log). Since there is no need to parse files when accessing log data, the representation through an UI can be easily achieved.

Furthermore, this behavior allows a remote host to access the log in a straightforward manner, simplifying large and/or distributed applications.

Note: Logs are written in a separate transaction, in case the chain fails.

5. Runtime information

In order to examine the activity of pg_timetable, the table timetable.run_status can be queried. It contains information about active jobs and their current parameters.

6. Schema diagram

Schema diagram

7. Contributing

If you want to contribute to pg_timetable and help make it better, feel free to open an issue or even consider submitting a pull request.

8. Support

For professional support, please contact Cybertec.

9. Authors

Pavlo Golub and Hans-Jürgen Schönig.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].