Skip to content

Commit 81b537e

Browse files
committed
Use DO blocks instead of functions to allow running on replica
1 parent b3eec1e commit 81b537e

File tree

1 file changed

+56
-69
lines changed

1 file changed

+56
-69
lines changed

diagnostic.sql

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
-- - continuous aggregates
2020
-- - continuous aggregate large materialization ranges
2121
-- - continuous aggregate chunk interval vs bucket width
22-
--
2322

24-
CREATE OR REPLACE FUNCTION pg_temp.check_deprecated_features() RETURNS void LANGUAGE plpgsql AS
25-
$$
23+
SET search_path TO pg_catalog, pg_temp;
24+
25+
-- deprecated features
26+
DO $$
2627
BEGIN
2728
-- check for hypertables with hypercore access method
2829
PERFORM FROM pg_class c join pg_am am ON c.relam=am.oid AND am.amname='hypercore' LIMIT 1;
@@ -36,10 +37,10 @@ BEGIN
3637
RAISE WARNING 'Found continuous aggregates using deprecated non-finalized form.';
3738
END IF;
3839
END
39-
$$ SET search_path = pg_catalog, pg_temp;
40+
$$;
4041

41-
CREATE OR REPLACE FUNCTION pg_temp.check_catalog_corruption() RETURNS void LANGUAGE plpgsql AS
42-
$$
42+
-- catalog corruption checks
43+
DO $$
4344
DECLARE
4445
v_count int8;
4546
v_relnames text[];
@@ -72,10 +73,10 @@ BEGIN
7273
RAISE WARNING 'Found % orphaned chunk relations: %', v_count, v_rels[1:20];
7374
END IF;
7475
END
75-
$$ SET search_path = pg_catalog, pg_temp;
76+
$$;
7677

77-
CREATE OR REPLACE FUNCTION pg_temp.check_scheduler_present() RETURNS void LANGUAGE plpgsql AS
78-
$$
78+
-- scheduler checks
79+
DO $$
7980
DECLARE
8081
v_count int8;
8182
BEGIN
@@ -92,10 +93,45 @@ BEGIN
9293
RAISE WARNING 'Multiple TimescaleDB scheduler (%) running in current database', v_count;
9394
END IF;
9495
END
95-
$$ SET search_path = pg_catalog, pg_temp;
96+
$$;
9697

97-
CREATE OR REPLACE FUNCTION pg_temp.check_cagg_large_materialization_range() RETURNS void LANGUAGE plpgsql AS
98-
$$
98+
-- job failure checks
99+
DO $$
100+
DECLARE
101+
v_failed int;
102+
v_total int;
103+
v_failed_distinct int;
104+
v_job_id int;
105+
v_count int;
106+
v_job_name text;
107+
BEGIN
108+
-- check for job failures in the last 7 days
109+
SELECT
110+
count(*) FILTER (WHERE NOT succeeded) AS failed,
111+
count(succeeded) AS total,
112+
count(DISTINCT job_id) FILTER (WHERE NOT succeeded) failed_distinct
113+
INTO v_failed, v_total, v_failed_distinct
114+
FROM _timescaledb_internal.bgw_job_stat_history
115+
WHERE execution_start > now() - '7 day'::interval;
116+
IF v_failed > 0 THEN
117+
RAISE WARNING '%/% job executions of % distinct jobs failed in last 7 days', v_failed, v_total, v_failed_distinct;
118+
FOR v_job_id, v_count, v_job_name IN
119+
SELECT job_id, count(*) AS count, (SELECT application_name FROM _timescaledb_config.bgw_job WHERE id = job_id) AS job_name
120+
FROM _timescaledb_internal.bgw_job_stat_history
121+
WHERE execution_start > now() - '7 day'::interval AND NOT succeeded
122+
GROUP BY job_id
123+
ORDER BY count DESC
124+
LIMIT 5
125+
LOOP
126+
RAISE WARNING ' Job % had % failures', v_job_name, v_count;
127+
END LOOP;
128+
END IF;
129+
END
130+
$$;
131+
132+
-- continuous aggregate checks
133+
-- continuous aggregates with large materialization ranges
134+
DO $$
99135
DECLARE
100136
v_cagg regclass;
101137
v_range text;
@@ -120,10 +156,10 @@ BEGIN
120156
RAISE WARNING 'Continuous aggregate % has large materialization range ''%''.', v_cagg, v_range;
121157
END LOOP;
122158
END
123-
$$ SET search_path = pg_catalog, pg_temp;
159+
$$;
124160

125-
CREATE OR REPLACE FUNCTION pg_temp.check_cagg_chunk_interval() RETURNS void LANGUAGE plpgsql AS
126-
$$
161+
-- continuous aggregates with chunk interval smaller than bucket width
162+
DO $$
127163
DECLARE
128164
v_cagg regclass;
129165
v_cagg_width text;
@@ -149,46 +185,12 @@ BEGIN
149185
RAISE WARNING 'Continuous aggregate % has chunk width smaller than bucket width % <= %', v_cagg, v_chunk_width, v_cagg_width;
150186
END LOOP;
151187
END
152-
$$ SET search_path = pg_catalog, pg_temp;
153-
154-
CREATE OR REPLACE FUNCTION pg_temp.check_job_failures() RETURNS void LANGUAGE plpgsql AS
155-
$$
156-
DECLARE
157-
v_failed int;
158-
v_total int;
159-
v_failed_distinct int;
160-
v_job_id int;
161-
v_count int;
162-
v_job_name text;
163-
BEGIN
164-
-- check for job failures in the last 7 days
165-
SELECT
166-
count(*) FILTER (WHERE NOT succeeded) AS failed,
167-
count(succeeded) AS total,
168-
count(DISTINCT job_id) FILTER (WHERE NOT succeeded) failed_distinct
169-
INTO v_failed, v_total, v_failed_distinct
170-
FROM _timescaledb_internal.bgw_job_stat_history
171-
WHERE execution_start > now() - '7 day'::interval;
172-
IF v_failed > 0 THEN
173-
RAISE WARNING '%/% job executions of % distinct jobs failed in last 7 days', v_failed, v_total, v_failed_distinct;
174-
FOR v_job_id, v_count, v_job_name IN
175-
SELECT job_id, count(*) AS count, (SELECT application_name FROM _timescaledb_config.bgw_job WHERE id = job_id) AS job_name
176-
FROM _timescaledb_internal.bgw_job_stat_history
177-
WHERE execution_start > now() - '7 day'::interval AND NOT succeeded
178-
GROUP BY job_id
179-
ORDER BY count DESC
180-
LIMIT 5
181-
LOOP
182-
RAISE WARNING ' Job % had % failures', v_job_name, v_count;
183-
END LOOP;
184-
END IF;
185-
186-
END
187-
$$ SET search_path = pg_catalog, pg_temp;
188+
$$;
188189

189190

190-
CREATE OR REPLACE FUNCTION pg_temp.check_compressed_chunk_batch_sizes() RETURNS void LANGUAGE plpgsql AS
191-
$$
191+
-- compression checks
192+
-- compressed chunk batch sizes
193+
DO $$
192194
DECLARE
193195
v_hypertable_id int;
194196
v_hypertable regclass;
@@ -243,20 +245,5 @@ BEGIN
243245
END IF;
244246
END LOOP;
245247
END
246-
$$ SET search_path = pg_catalog, pg_temp;
247-
248-
CREATE OR REPLACE FUNCTION pg_temp.run_checks() RETURNS void LANGUAGE plpgsql AS
249-
$$
250-
BEGIN
251-
PERFORM pg_temp.check_deprecated_features();
252-
PERFORM pg_temp.check_catalog_corruption();
253-
PERFORM pg_temp.check_scheduler_present();
254-
PERFORM pg_temp.check_job_failures();
255-
PERFORM pg_temp.check_compressed_chunk_batch_sizes();
256-
PERFORM pg_temp.check_cagg_large_materialization_range();
257-
PERFORM pg_temp.check_cagg_chunk_interval();
258-
END
259-
$$ SET search_path = pg_catalog, pg_temp;
260-
261-
SELECT pg_temp.run_checks();
248+
$$;
262249

0 commit comments

Comments
 (0)