From ecbda14ea61dc9c425f0265bd68c3ff464a4e464 Mon Sep 17 00:00:00 2001 From: Pavlo Golub Date: Tue, 21 Jul 2026 16:04:17 +0200 Subject: [PATCH] [+] add indexes to `timetable.execution_log`, closes #797 - B-tree index for looking up the latest executions of a specific chain - BRIN index for time-window queries and retention cleanup --------- Co-authored-by: 0xgouda --- internal/pgengine/migration.go | 6 ++++ internal/pgengine/sql/ddl.sql | 32 ++++++++++++++++++++++ internal/pgengine/sql/init.sql | 3 +- internal/pgengine/sql/migrations/00797.sql | 7 +++++ main.go | 2 +- 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 internal/pgengine/sql/migrations/00797.sql diff --git a/internal/pgengine/migration.go b/internal/pgengine/migration.go index e3af90a5..c51dadb7 100644 --- a/internal/pgengine/migration.go +++ b/internal/pgengine/migration.go @@ -159,6 +159,12 @@ var Migrations func() migrator.Option = func() migrator.Option { return ExecuteMigrationScript(ctx, tx, "00792.sql") }, }, + &migrator.Migration{ + Name: "00797 Add indexes to timetable.execution_log", + Func: func(ctx context.Context, tx pgx.Tx) error { + return ExecuteMigrationScript(ctx, tx, "00797.sql") + }, + }, // adding new migration here, update "timetable"."migration" in "sql/init.sql" // and "dbapi" variable in main.go! diff --git a/internal/pgengine/sql/ddl.sql b/internal/pgengine/sql/ddl.sql index 10322272..c6ffdc51 100644 --- a/internal/pgengine/sql/ddl.sql +++ b/internal/pgengine/sql/ddl.sql @@ -127,6 +127,38 @@ CREATE TABLE timetable.execution_log ( COMMENT ON TABLE timetable.execution_log IS 'Stores log entries of executed tasks and chains'; +COMMENT ON COLUMN timetable.execution_log.chain_id IS + 'Link to the chain executed'; +COMMENT ON COLUMN timetable.execution_log.task_id IS + 'Link to the task executed'; +COMMENT ON COLUMN timetable.execution_log.txid IS + 'Transaction ID of the executed task'; +COMMENT ON COLUMN timetable.execution_log.last_run IS + 'Timestamp of the last execution of the task'; +COMMENT ON COLUMN timetable.execution_log.finished IS + 'Timestamp of the task execution finish'; +COMMENT ON COLUMN timetable.execution_log.pid IS + 'Process ID of the worker executing the task'; +COMMENT ON COLUMN timetable.execution_log.returncode IS + 'Return code of the executed task'; +COMMENT ON COLUMN timetable.execution_log.ignore_error IS + 'Indicates whether a next task in a chain can be executed regardless of the success of the current one'; +COMMENT ON COLUMN timetable.execution_log.kind IS + 'Indicates whether "command" is SQL, built-in function or an external program'; +COMMENT ON COLUMN timetable.execution_log.command IS + 'Contains either an SQL command, or command string to be executed'; +COMMENT ON COLUMN timetable.execution_log.output IS + 'Contains output of the executed task'; +COMMENT ON COLUMN timetable.execution_log.client_name IS + 'Name of the client executing the task'; +COMMENT ON COLUMN timetable.execution_log.params IS + 'Contains parameters passed as arguments to a chain task'; + +CREATE INDEX execution_log_chain_id_finished_idx + ON timetable.execution_log (chain_id, finished); + +CREATE INDEX execution_log_finished_brin_idx + ON timetable.execution_log USING brin (finished); CREATE UNLOGGED TABLE timetable.active_chain( chain_id BIGINT NOT NULL, diff --git a/internal/pgengine/sql/init.sql b/internal/pgengine/sql/init.sql index 19412df3..8fb5fa2d 100644 --- a/internal/pgengine/sql/init.sql +++ b/internal/pgengine/sql/init.sql @@ -29,4 +29,5 @@ VALUES (13, '00629 Add ignore_error column to timetable.execution_log'), (14, '00721 Add more job control functions'), (15, '00733 Add params column to timetable.execution_log table'), - (16, '00792 Add ability to enable and disable tasks'); \ No newline at end of file + (16, '00792 Add ability to enable and disable tasks'), + (17, '00797 Add indexes to timetable.execution_log'); diff --git a/internal/pgengine/sql/migrations/00797.sql b/internal/pgengine/sql/migrations/00797.sql new file mode 100644 index 00000000..eec6c12b --- /dev/null +++ b/internal/pgengine/sql/migrations/00797.sql @@ -0,0 +1,7 @@ +-- B-tree index for looking up the latest executions of a specific chain +CREATE INDEX IF NOT EXISTS execution_log_chain_id_finished_idx + ON timetable.execution_log (chain_id, finished); + +-- BRIN index for time-window queries and retention cleanup +CREATE INDEX IF NOT EXISTS execution_log_finished_brin_idx + ON timetable.execution_log USING brin (finished); diff --git a/main.go b/main.go index 481d2726..0f10ba87 100644 --- a/main.go +++ b/main.go @@ -55,7 +55,7 @@ var ( commit = "000000" version = "master" date = "unknown" - dbapi = "00792" + dbapi = "00797" ) func printVersion() {