timescaledb/sql/job_error_log_retention.sql
Konstantina Skovola 9bd772de25 Add interface for troubleshooting job failures
This commit gives more visibility into job failures by making the
information regarding a job runtime error available in an extension
table (`job_errors`) that users can directly query.
This commit also adds an infromational view on top of the table for
convenience.
To prevent the `job_errors` table from growing too large,
a retention job is also set up with a default retention interval
of 1 month. The retention job is registered with a custom check
function that requires that a valid "drop_after" interval be provided
in the config field of the job.
2022-09-30 15:22:27 +02:00

72 lines
2.0 KiB
PL/PgSQL

-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
-- A retention policy is set up for the table _timescaledb_internal.job_errors (Error Log Retention Policy [2])
-- By default, it will run once a month and and drop rows older than a month.
CREATE OR REPLACE FUNCTION _timescaledb_internal.policy_job_error_retention(job_id integer, config JSONB) RETURNS integer
LANGUAGE PLPGSQL AS
$BODY$
DECLARE
drop_after INTERVAL;
numrows INTEGER;
BEGIN
SELECT config->>'drop_after' INTO STRICT drop_after;
WITH deleted AS
(DELETE
FROM _timescaledb_internal.job_errors
WHERE finish_time < (now() - drop_after) RETURNING *)
SELECT count(*)
FROM deleted INTO numrows;
RETURN numrows;
END;
$BODY$ SET search_path TO pg_catalog, pg_temp;
CREATE OR REPLACE FUNCTION _timescaledb_internal.policy_job_error_retention_check(config JSONB) RETURNS VOID
LANGUAGE PLPGSQL AS
$BODY$
DECLARE
drop_after interval;
BEGIN
IF config IS NULL THEN
RAISE EXCEPTION 'config cannot be NULL, and must contain drop_after';
END IF;
SELECT config->>'drop_after' INTO STRICT drop_after;
IF drop_after IS NULL THEN
RAISE EXCEPTION 'drop_after interval not provided';
END IF ;
END;
$BODY$ SET search_path TO pg_catalog, pg_temp;
INSERT INTO _timescaledb_config.bgw_job (
id,
application_name,
schedule_interval,
max_runtime,
max_retries,
retry_period,
proc_schema,
proc_name,
owner,
scheduled,
config,
check_schema,
check_name
)
VALUES
(
2,
'Error Log Retention Policy [2]',
INTERVAL '1 month',
INTERVAL '1 hour',
-1,
INTERVAL '1h',
'_timescaledb_internal',
'policy_job_error_retention',
CURRENT_ROLE,
true,
'{"drop_after":"1 month"}',
'_timescaledb_internal',
'policy_job_error_retention_check'
) ON CONFLICT (id) DO NOTHING;