Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 56 additions & 69 deletions diagnostic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
-- - continuous aggregates
-- - continuous aggregate large materialization ranges
-- - continuous aggregate chunk interval vs bucket width
--

CREATE OR REPLACE FUNCTION pg_temp.check_deprecated_features() RETURNS void LANGUAGE plpgsql AS
$$
SET search_path TO pg_catalog, pg_temp;

-- deprecated features
DO $$
BEGIN
-- check for hypertables with hypercore access method
PERFORM FROM pg_class c join pg_am am ON c.relam=am.oid AND am.amname='hypercore' LIMIT 1;
Expand All @@ -36,10 +37,10 @@ BEGIN
RAISE WARNING 'Found continuous aggregates using deprecated non-finalized form.';
END IF;
END
$$ SET search_path = pg_catalog, pg_temp;
$$;

CREATE OR REPLACE FUNCTION pg_temp.check_catalog_corruption() RETURNS void LANGUAGE plpgsql AS
$$
-- catalog corruption checks
DO $$
DECLARE
v_count int8;
v_relnames text[];
Expand Down Expand Up @@ -72,10 +73,10 @@ BEGIN
RAISE WARNING 'Found % orphaned chunk relations: %', v_count, v_rels[1:20];
END IF;
END
$$ SET search_path = pg_catalog, pg_temp;
$$;

CREATE OR REPLACE FUNCTION pg_temp.check_scheduler_present() RETURNS void LANGUAGE plpgsql AS
$$
-- scheduler checks
DO $$
DECLARE
v_count int8;
BEGIN
Expand All @@ -92,10 +93,45 @@ BEGIN
RAISE WARNING 'Multiple TimescaleDB scheduler (%) running in current database', v_count;
END IF;
END
$$ SET search_path = pg_catalog, pg_temp;
$$;

CREATE OR REPLACE FUNCTION pg_temp.check_cagg_large_materialization_range() RETURNS void LANGUAGE plpgsql AS
$$
-- job failure checks
DO $$
DECLARE
v_failed int;
v_total int;
v_failed_distinct int;
v_job_id int;
v_count int;
v_job_name text;
BEGIN
-- check for job failures in the last 7 days
SELECT
count(*) FILTER (WHERE NOT succeeded) AS failed,
count(succeeded) AS total,
count(DISTINCT job_id) FILTER (WHERE NOT succeeded) failed_distinct
INTO v_failed, v_total, v_failed_distinct
FROM _timescaledb_internal.bgw_job_stat_history
WHERE execution_start > now() - '7 day'::interval;
IF v_failed > 0 THEN
RAISE WARNING '%/% job executions of % distinct jobs failed in last 7 days', v_failed, v_total, v_failed_distinct;
FOR v_job_id, v_count, v_job_name IN
SELECT job_id, count(*) AS count, (SELECT application_name FROM _timescaledb_config.bgw_job WHERE id = job_id) AS job_name
FROM _timescaledb_internal.bgw_job_stat_history
WHERE execution_start > now() - '7 day'::interval AND NOT succeeded
GROUP BY job_id
ORDER BY count DESC
LIMIT 5
LOOP
RAISE WARNING ' Job % had % failures', v_job_name, v_count;
END LOOP;
END IF;
END
$$;

-- continuous aggregate checks
-- continuous aggregates with large materialization ranges
DO $$
DECLARE
v_cagg regclass;
v_range text;
Expand All @@ -120,10 +156,10 @@ BEGIN
RAISE WARNING 'Continuous aggregate % has large materialization range ''%''.', v_cagg, v_range;
END LOOP;
END
$$ SET search_path = pg_catalog, pg_temp;
$$;

CREATE OR REPLACE FUNCTION pg_temp.check_cagg_chunk_interval() RETURNS void LANGUAGE plpgsql AS
$$
-- continuous aggregates with chunk interval smaller than bucket width
DO $$
DECLARE
v_cagg regclass;
v_cagg_width text;
Expand All @@ -149,46 +185,12 @@ BEGIN
RAISE WARNING 'Continuous aggregate % has chunk width smaller than bucket width % <= %', v_cagg, v_chunk_width, v_cagg_width;
END LOOP;
END
$$ SET search_path = pg_catalog, pg_temp;

CREATE OR REPLACE FUNCTION pg_temp.check_job_failures() RETURNS void LANGUAGE plpgsql AS
$$
DECLARE
v_failed int;
v_total int;
v_failed_distinct int;
v_job_id int;
v_count int;
v_job_name text;
BEGIN
-- check for job failures in the last 7 days
SELECT
count(*) FILTER (WHERE NOT succeeded) AS failed,
count(succeeded) AS total,
count(DISTINCT job_id) FILTER (WHERE NOT succeeded) failed_distinct
INTO v_failed, v_total, v_failed_distinct
FROM _timescaledb_internal.bgw_job_stat_history
WHERE execution_start > now() - '7 day'::interval;
IF v_failed > 0 THEN
RAISE WARNING '%/% job executions of % distinct jobs failed in last 7 days', v_failed, v_total, v_failed_distinct;
FOR v_job_id, v_count, v_job_name IN
SELECT job_id, count(*) AS count, (SELECT application_name FROM _timescaledb_config.bgw_job WHERE id = job_id) AS job_name
FROM _timescaledb_internal.bgw_job_stat_history
WHERE execution_start > now() - '7 day'::interval AND NOT succeeded
GROUP BY job_id
ORDER BY count DESC
LIMIT 5
LOOP
RAISE WARNING ' Job % had % failures', v_job_name, v_count;
END LOOP;
END IF;

END
$$ SET search_path = pg_catalog, pg_temp;
$$;


CREATE OR REPLACE FUNCTION pg_temp.check_compressed_chunk_batch_sizes() RETURNS void LANGUAGE plpgsql AS
$$
-- compression checks
-- compressed chunk batch sizes
DO $$
DECLARE
v_hypertable_id int;
v_hypertable regclass;
Expand Down Expand Up @@ -243,20 +245,5 @@ BEGIN
END IF;
END LOOP;
END
$$ SET search_path = pg_catalog, pg_temp;

CREATE OR REPLACE FUNCTION pg_temp.run_checks() RETURNS void LANGUAGE plpgsql AS
$$
BEGIN
PERFORM pg_temp.check_deprecated_features();
PERFORM pg_temp.check_catalog_corruption();
PERFORM pg_temp.check_scheduler_present();
PERFORM pg_temp.check_job_failures();
PERFORM pg_temp.check_compressed_chunk_batch_sizes();
PERFORM pg_temp.check_cagg_large_materialization_range();
PERFORM pg_temp.check_cagg_chunk_interval();
END
$$ SET search_path = pg_catalog, pg_temp;

SELECT pg_temp.run_checks();
$$;