Repository navigation
Expand file tree
/
Copy pathdiagnostic.sql
More file actions
672 lines (613 loc) · 31.2 KB
/
Copy pathdiagnostic.sql
File metadata and controls
672 lines (613 loc) · 31.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE and LICENSE for copyright and licensing information.
-- collection of diagnostic checks for TimescaleDB
--
-- Checks included:
-- - informational
-- - PostgreSQL version
-- - TimescaleDB version
-- - non-default timescaledb settings
-- - deprecated features
-- - hypercore access method
-- - continuous aggregates non-finalized form
-- - undesirable settings
-- - timescaledb.restoring
-- - catalog corruption
-- - chunk_column_stats with range_start > range_end
-- - orphaned chunks
-- - orphaned hypertables
-- - scheduler checks
-- - launcher running
-- - exactly 1 scheduler running in current database
-- - job failures
-- - compression
-- - compressed batch sizes
-- - composite bloom filter columns not matching compression settings
-- - continuous aggregates
-- - continuous aggregate large materialization ranges
-- - hypertables with invalidation threshold in the future
-- - continuous aggregate chunk interval vs bucket width
-- informational
DO $$
DECLARE
v_count int;
v_settings text[];
BEGIN
SET LOCAL search_path TO pg_catalog, pg_temp;
IF current_setting('cloud.service_id', true) IS NOT NULL THEN
RAISE INFO 'Project: % Service: %', current_setting('cloud.project_id', true), current_setting('cloud.service_id', true);
END IF;
RAISE INFO 'PostgreSQL: % TimescaleDB: %', current_setting('server_version'), (SELECT extversion FROM pg_extension WHERE extname='timescaledb');
-- non-default timescaledb settings
SELECT count(*), array_agg(format('%s:%s',s.name, pg_catalog.current_setting(s.name)) ORDER BY s.name) INTO v_count, v_settings
FROM pg_catalog.pg_settings s
WHERE s.name LIKE 'timescaledb.%' AND s.source <> 'default' AND s.setting IS DISTINCT FROM s.boot_val;
IF v_count > 0 THEN
RAISE INFO 'Non-default TimescaleDB settings: %', v_settings;
END IF;
END
$$;
-- deprecated features
DO $$
BEGIN
SET LOCAL search_path TO pg_catalog, pg_temp;
-- 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;
IF FOUND THEN
RAISE WARNING 'Found relations using the deprecated hypercore access method.';
END IF;
-- check for continuous aggregates using non-finalized form
IF EXISTS(SELECT FROM pg_attribute a JOIN pg_class c ON c.relname='continuous_agg' AND c.oid=a.attrelid JOIN pg_namespace n ON n.nspname='_timescaledb_catalog' AND n.oid=c.relnamespace WHERE attname='finalized') THEN
PERFORM FROM _timescaledb_catalog.continuous_agg WHERE NOT finalized;
IF FOUND THEN
RAISE WARNING 'Found continuous aggregates using deprecated non-finalized form.';
END IF;
END IF;
-- check for wal based invalidation plugin
PERFORM FROM pg_replication_slots WHERE plugin LIKE 'timescaledb-invalidations-%';
IF FOUND THEN
RAISE WARNING 'Found WAL based invalidation plugin.';
END IF;
END
$$;
-- undesirable settings
DO $$
BEGIN
SET LOCAL search_path TO pg_catalog, pg_temp;
IF current_setting('timescaledb.restoring')::bool THEN
RAISE WARNING 'timescaledb.restoring is enabled. This setting should only be enabled during maintenance operations.';
END IF;
END
$$;
-- catalog corruption checks
DO $$
DECLARE
v_query text;
v_count int8;
v_relnames text[];
v_rels regclass[];
v_hypertable regclass;
v_index regclass;
BEGIN
SET LOCAL search_path TO pg_catalog, pg_temp;
-- hypertable with missing compressed hypertable entry
SELECT array_agg(format('%I.%I',schema_name,table_name)) INTO v_relnames FROM _timescaledb_catalog.hypertable h1
WHERE
compressed_hypertable_id IS NOT NULL AND
NOT EXISTS(SELECT FROM _timescaledb_catalog.hypertable h2 WHERE h2.id = h1.compressed_hypertable_id);
IF v_relnames IS NOT NULL THEN
RAISE WARNING 'Found hypertables with missing compressed hypertable catalog entry: %', v_relnames;
END IF;
-- chunk with missing compressed chunk entry
IF EXISTS(SELECT FROM pg_attribute WHERE attname='compressed_chunk_id' AND attrelid='_timescaledb_catalog.chunk'::regclass) THEN
SELECT array_agg(format('%I.%I',schema_name,table_name)) INTO v_relnames FROM _timescaledb_catalog.chunk ch1
WHERE
compressed_chunk_id IS NOT NULL AND
NOT EXISTS(SELECT FROM _timescaledb_catalog.chunk ch2 WHERE ch2.id = ch1.compressed_chunk_id);
IF v_relnames IS NOT NULL THEN
RAISE WARNING 'Found chunks with missing compressed chunk catalog entry: %', v_relnames;
END IF;
END IF;
-- chunk with missing hypertable entry
IF EXISTS(SELECT FROM pg_attribute WHERE attname='table_name' AND attrelid='_timescaledb_catalog.chunk'::regclass) THEN
SELECT array_agg(format('%I.%I',schema_name,table_name)) INTO v_relnames FROM _timescaledb_catalog.chunk ch
WHERE
NOT EXISTS(SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = ch.hypertable_id);
IF v_relnames IS NOT NULL THEN
RAISE WARNING 'Found chunks with missing hypertable catalog entry: %', v_relnames;
END IF;
END IF;
IF EXISTS(SELECT FROM pg_attribute WHERE attname='relid' AND attrelid='_timescaledb_catalog.chunk'::regclass) THEN
SELECT array_agg(relid::text) INTO v_relnames FROM _timescaledb_catalog.chunk ch
WHERE
NOT EXISTS(SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = ch.hypertable_id);
IF v_relnames IS NOT NULL THEN
RAISE WARNING 'Found chunks with missing hypertable catalog entry: %', v_relnames;
END IF;
END IF;
-- orphaned foreign key references in _timescaledb_catalog.tablespace
SELECT array_agg(tablespace_name) INTO v_relnames FROM _timescaledb_catalog.tablespace ts
WHERE
NOT EXISTS(SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = ts.hypertable_id);
IF v_relnames IS NOT NULL THEN
RAISE WARNING 'Found tablespace entries with missing hypertable catalog entry: %', v_relnames;
END IF;
-- orphaned foreign key references in _timescaledb_catalog.dimension
SELECT array_agg(column_name) INTO v_relnames FROM _timescaledb_catalog.dimension dim
WHERE
NOT EXISTS(SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = dim.hypertable_id);
IF v_relnames IS NOT NULL THEN
RAISE WARNING 'Found dimension entries with missing hypertable catalog entry: %', v_relnames;
END IF;
-- orphaned foreign key references in _timescaledb_catalog.dimension_slice
SELECT array_agg(dimension_id::text) INTO v_relnames FROM _timescaledb_catalog.dimension_slice slice
WHERE
NOT EXISTS(SELECT FROM _timescaledb_catalog.dimension dim WHERE dim.id = slice.dimension_id);
IF v_relnames IS NOT NULL THEN
RAISE WARNING 'Found dimension_slice entries with missing dimension catalog entry: %', v_relnames;
END IF;
-- orphaned foreign key references in _timescaledb_catalog.chunk_constraint
SELECT array_agg(constraint_name) INTO v_relnames FROM _timescaledb_catalog.chunk_constraint cc
WHERE
NOT EXISTS(SELECT FROM _timescaledb_catalog.chunk ch WHERE ch.id = cc.chunk_id);
IF v_relnames IS NOT NULL THEN
RAISE WARNING 'Found chunk_constraint entry with missing chunk catalog entry: %', v_relnames;
END IF;
-- orphaned foreign key references in _timescaledb_catalog.chunk_constraint
SELECT array_agg(constraint_name) INTO v_relnames FROM _timescaledb_catalog.chunk_constraint cc
WHERE
NOT EXISTS(SELECT FROM _timescaledb_catalog.dimension_slice ds WHERE ds.id = cc.dimension_slice_id);
IF v_relnames IS NOT NULL THEN
RAISE WARNING 'Found % chunk_constraint entry with missing dimension slice catalog entry: %', array_length(v_relnames,1), v_relnames[1:20];
END IF;
-- orphaned foreign key references in _timescaledb_catalog.chunk_column_stats
IF EXISTS(SELECT FROM pg_class c JOIN pg_namespace nsp ON c.relnamespace=nsp.oid AND nspname = '_timescaledb_catalog' WHERE relname='chunk_column_stats') THEN
IF EXISTS(SELECT FROM _timescaledb_catalog.chunk_column_stats ccs
WHERE
NOT EXISTS(SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = ccs.hypertable_id)
OR NOT EXISTS(SELECT FROM _timescaledb_catalog.chunk ch WHERE ch.id = ccs.chunk_id)) THEN
RAISE WARNING 'Found chunk_column_stats entries with missing hypertable or chunk catalog entry.';
END IF;
END IF;
-- orphaned foreign key references in bgw_job
SET LOCAL search_path TO pg_catalog, _timescaledb_config, _timescaledb_catalog, pg_temp;
SELECT array_agg(application_name) INTO v_relnames FROM bgw_job j
WHERE
hypertable_id IS NOT NULL AND
NOT EXISTS(SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = j.hypertable_id);
IF v_relnames IS NOT NULL THEN
RAISE WARNING 'Found chunk_constraint entry with missing dimension slice catalog entry: %', v_relnames;
END IF;
-- orphaned foreign key references in bgw_job_stat
IF EXISTS(SELECT FROM _timescaledb_internal.bgw_job_stat js WHERE NOT EXISTS(SELECT FROM bgw_job j WHERE j.id = js.job_id)) THEN
RAISE WARNING 'Found orphaned bgw_job_stat entry';
END IF;
-- orphaned foreign key references in bgw_policy_chunk_stats
IF EXISTS(SELECT FROM _timescaledb_internal.bgw_policy_chunk_stats cs
WHERE
NOT EXISTS(SELECT FROM _timescaledb_catalog.chunk ch WHERE ch.id = cs.chunk_id)
OR NOT EXISTS(SELECT FROM bgw_job j WHERE j.id = cs.job_id)) THEN
RAISE WARNING 'Found orphaned bgw_policy_chunk_stats entry';
END IF;
SET LOCAL search_path TO pg_catalog, pg_temp;
-- orphaned foreign key references in _timescaledb_catalog.continuous_agg
IF EXISTS(
SELECT FROM _timescaledb_catalog.continuous_agg cagg
WHERE
NOT EXISTS(SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = cagg.raw_hypertable_id)
OR (parent_mat_hypertable_id IS NOT NULL AND NOT EXISTS(SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = cagg.parent_mat_hypertable_id))
OR NOT EXISTS(SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = cagg.mat_hypertable_id)
) THEN
RAISE WARNING 'Found continuous_agg entries with missing hypertable catalog entry.';
END IF;
-- orphaned foreign key references in _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log
IF EXISTS(
SELECT FROM _timescaledb_catalog.continuous_aggs_hypertable_invalidation_log l
WHERE NOT EXISTS (SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = l.hypertable_id)
) THEN
RAISE WARNING 'Found continuous_aggs_hypertable_invalidation_log entries with missing hypertable catalog entry.';
END IF;
-- orphaned foreign key references in _timescaledb_catalog.continuous_aggs_invalidation_threshold
IF EXISTS(
SELECT FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold t
WHERE NOT EXISTS (SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = t.hypertable_id)
) THEN
RAISE WARNING 'Found continuous_aggs_invalidation_threshold entries with missing hypertable catalog entry.';
END IF;
-- orphaned foreign key references in _timescaledb_catalog.continuous_aggs_watermark
IF EXISTS(SELECT FROM pg_class c JOIN pg_namespace nsp ON c.relnamespace=nsp.oid AND nspname = '_timescaledb_catalog' WHERE relname='continuous_aggs_watermark') THEN
IF EXISTS(
SELECT FROM _timescaledb_catalog.continuous_aggs_watermark w
WHERE NOT EXISTS (SELECT FROM _timescaledb_catalog.hypertable ht WHERE ht.id = w.mat_hypertable_id)
) THEN
RAISE WARNING 'Found continuous_aggs_watermark entries with missing hypertable catalog entry.';
END IF;
END IF;
-- orphaned foreign key references in _timescaledb_catalog.continuous_aggs_materialization_invalidation_log
IF EXISTS(
SELECT FROM _timescaledb_catalog.continuous_aggs_materialization_invalidation_log l
WHERE NOT EXISTS (SELECT FROM _timescaledb_catalog.continuous_agg cagg WHERE cagg.mat_hypertable_id = l.materialization_id)
) THEN
RAISE WARNING 'Found continuous_aggs_materialization_invalidation_log entries with missing continuous_agg catalog entry.';
END IF;
-- orphaned foreign key references in _timescaledb_catalog.continuous_aggs_materialization_ranges
IF EXISTS(SELECT FROM pg_class c JOIN pg_namespace nsp ON c.relnamespace=nsp.oid AND nspname = '_timescaledb_catalog' WHERE relname='continuous_aggs_materialization_ranges') THEN
IF EXISTS(
SELECT FROM _timescaledb_catalog.continuous_aggs_materialization_ranges r
WHERE NOT EXISTS (SELECT FROM _timescaledb_catalog.continuous_agg cagg WHERE cagg.mat_hypertable_id = r.materialization_id)
) THEN
RAISE WARNING 'Found continuous_aggs_materialization_invalidation_log entries with missing continuous_agg catalog entry.';
END IF;
END IF;
-- orphaned foreign key references in _timescaledb_catalog.compression_chunk_size
IF EXISTS(SELECT FROM _timescaledb_catalog.compression_chunk_size cs
WHERE
NOT EXISTS(SELECT FROM _timescaledb_catalog.chunk ch WHERE ch.id = cs.chunk_id)
OR NOT EXISTS(SELECT FROM _timescaledb_catalog.chunk ch WHERE ch.id = cs.compressed_chunk_id)
) THEN
RAISE WARNING 'Found compression_chunk_size entries with missing chunk catalog entry.';
END IF;
-- corrupt _timescaledb_catalog.chunk_column_stats entries
IF EXISTS(SELECT FROM pg_class c JOIN pg_namespace nsp ON c.relnamespace=nsp.oid AND nspname = '_timescaledb_catalog' WHERE relname='chunk_column_stats') THEN
SELECT count(*) INTO v_count FROM _timescaledb_catalog.chunk_column_stats WHERE range_start > range_end;
IF v_count >= 1 THEN
RAISE WARNING 'Found %s entries in _timescaledb_catalog.chunk_column_stats with range_start > range_end', v_count;
END IF;
END IF;
-- chunks with missing relations
-- finds chunks that have an entry in our catalog but the actual table is missing
IF EXISTS(SELECT FROM pg_attribute WHERE attname='table_name' AND attrelid='_timescaledb_catalog.chunk'::regclass) THEN
v_query := $sql$
SELECT count(*), array_agg(format('%I.%I',ch.schema_name,ch.table_name))
FROM _timescaledb_catalog.chunk ch WHERE
NOT EXISTS (SELECT FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid WHERE c.relname=ch.table_name AND n.nspname = ch.schema_name)
$sql$;
PERFORM FROM pg_attribute a JOIN pg_class c ON c.oid = a.attrelid AND c.relnamespace='_timescaledb_catalog'::regnamespace AND c.relname = 'chunk' WHERE a.attname = 'dropped';
IF FOUND THEN
v_query := v_query || ' AND NOT ch.dropped';
END IF;
ELSE
v_query := $sql$
SELECT count(*), array_agg(ch.relid::text)
FROM _timescaledb_catalog.chunk ch WHERE
NOT EXISTS (SELECT FROM pg_class c WHERE c.oid=ch.relid)
$sql$;
END IF;
EXECUTE v_query INTO v_count, v_relnames;
IF v_count > 0 THEN
RAISE WARNING 'Found % chunk entries without relations: %', v_count, v_relnames[1:20];
END IF;
-- find hypertables that have an entry in our catalog but the actual table is missing
SELECT count(*), array_agg(format('%I.%I',ht.schema_name,ht.table_name)) INTO v_count, v_relnames
FROM _timescaledb_catalog.hypertable ht
WHERE NOT EXISTS(
SELECT FROM pg_class c
JOIN pg_namespace ns ON ns.oid=c.relnamespace AND ns.nspname = ht.schema_name
WHERE c.relname=ht.table_name
);
IF v_count > 0 THEN
RAISE WARNING 'Found % hypertable entries without relations: %', v_count, v_relnames[1:20];
END IF;
-- orphaned chunks
IF EXISTS(SELECT FROM pg_attribute WHERE attname='table_name' AND attrelid='_timescaledb_catalog.chunk'::regclass) THEN
SELECT count(*), array_agg(oid::regclass) INTO v_count, v_rels
FROM pg_class
WHERE
relnamespace='_timescaledb_internal'::regnamespace AND
relkind='r' AND
relname LIKE '%_chunk' AND
NOT EXISTS(SELECT FROM _timescaledb_catalog.chunk where schema_name='_timescaledb_internal' and table_name = relname);
IF v_count > 0 THEN
RAISE WARNING 'Found % orphaned chunk relations: %', v_count, v_rels[1:20];
END IF;
END IF;
-- unique indexes defined on the hypertable that are not present on all chunks
IF EXISTS(SELECT FROM pg_attribute WHERE attname='table_name' AND attrelid='_timescaledb_catalog.chunk'::regclass) THEN
FOR v_hypertable, v_index, v_rels IN
SELECT c.oid::regclass hypertable, ht_i.indexrelid::regclass, array_agg(c_ch.oid::regclass)
FROM _timescaledb_catalog.hypertable ht
JOIN pg_class c ON c.relname=ht.table_name
JOIN pg_namespace nsp ON c.relnamespace=nsp.oid AND nsp.nspname=ht.schema_name
JOIN pg_index ht_i ON ht_i.indrelid=c.oid AND ht_i.indisunique
JOIN _timescaledb_catalog.chunk ch ON ch.hypertable_id=ht.id
JOIN pg_class c_ch ON c_ch.relname=ch.table_name
JOIN pg_namespace nsp_ch ON c_ch.relnamespace=nsp_ch.oid
WHERE
NOT EXISTS(
SELECT FROM pg_index ch_i
WHERE
ch_i.indrelid=c_ch.oid AND
ch_i.indisunique AND
ch_i.indisvalid AND
(SELECT array_agg(attname ORDER BY attnum) FROM pg_attribute att WHERE att.attrelid=c.oid AND attnum =ANY(ht_i.indkey)) = (SELECT array_agg(attname ORDER BY attnum) FROM pg_attribute att WHERE att.attrelid=c_ch.oid AND attnum =ANY(ch_i.indkey)))
GROUP BY c.oid, ht_i.indexrelid
LOOP
RAISE WARNING 'Hypertable % unique index % missing on chunks %', v_hypertable, v_index, v_rels[1:20];
END LOOP;
END IF;
END
$$;
-- scheduler checks
DO $$
DECLARE
v_count int8;
BEGIN
SET LOCAL search_path TO pg_catalog, pg_temp;
IF NOT pg_is_in_recovery() THEN
PERFORM FROM pg_stat_activity WHERE application_name = 'TimescaleDB Background Worker Launcher';
IF NOT FOUND THEN
RAISE WARNING 'TimescaleDB launcher not running';
END IF;
PERFORM FROM pg_stat_activity WHERE application_name = 'TimescaleDB Background Worker Scheduler' AND datname = current_database();
IF NOT FOUND THEN
RAISE WARNING 'TimescaleDB scheduler not running in current database';
END IF;
SELECT count(*) INTO v_count FROM pg_stat_activity WHERE application_name = 'TimescaleDB Background Worker Scheduler' AND datname = current_database();
IF v_count > 1 THEN
RAISE WARNING 'Multiple TimescaleDB scheduler (%) running in current database', v_count;
END IF;
END IF;
END
$$;
-- 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
SET LOCAL search_path TO pg_catalog, _timescaledb_config, _timescaledb_catalog, pg_temp;
-- check for job failures in the last 7 days
IF EXISTS(SELECT FROM pg_class c JOIN pg_namespace nsp ON c.relnamespace=nsp.oid AND nspname = '_timescaledb_internal' WHERE relname='bgw_job_stat_history') THEN
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 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 IF;
END
$$;
-- continuous aggregate checks
DO $$
DECLARE
v_cagg regclass;
v_range text;
v_cagg_width text;
v_chunk_width text;
BEGIN
SET LOCAL search_path TO pg_catalog, pg_temp;
-- continuous aggregates with large materialization ranges
IF EXISTS(SELECT FROM pg_class c JOIN pg_namespace nsp ON c.relnamespace=nsp.oid AND nspname = '_timescaledb_catalog' WHERE relname='continuous_aggs_materialization_ranges') AND
EXISTS(SELECT FROM pg_proc p JOIN pg_namespace nsp ON p.pronamespace=nsp.oid AND nsp.nspname = '_timescaledb_functions' WHERE proname='cagg_get_bucket_function_info')
THEN
FOR v_cagg, v_range IN
SELECT
format('%I.%I', c.user_view_schema, c.user_view_name)::regclass AS continuous_aggregate,
CASE
WHEN d.column_type =ANY('{int4,int8}'::regtype[]) THEN (r.greatest_modified_value - r.lowest_modified_value)::text
WHEN d.column_type =ANY('{timestamp,timestamptz}'::regtype[]) THEN _timescaledb_functions.to_interval(r.greatest_modified_value - r.lowest_modified_value)::text
END AS range
FROM _timescaledb_catalog.continuous_aggs_materialization_ranges r
JOIN _timescaledb_catalog.continuous_agg c ON c.mat_hypertable_id=r.materialization_id
JOIN LATERAL(SELECT * FROM _timescaledb_functions.cagg_get_bucket_function_info(c.mat_hypertable_id)) f on true
JOIN _timescaledb_catalog.dimension d ON d.hypertable_id=c.mat_hypertable_id
WHERE
CASE
WHEN d.column_type =ANY('{int4,int8}'::regtype[]) THEN (r.greatest_modified_value - r.lowest_modified_value) > 250 * f.bucket_width::int
WHEN d.column_type =ANY('{timestamp,timestamptz}'::regtype[]) THEN _timescaledb_functions.to_interval(r.greatest_modified_value - r.lowest_modified_value) > 250 * f.bucket_width::interval
END
LOOP
RAISE WARNING 'Continuous aggregate % has large materialization range ''%''.', v_cagg, v_range;
END LOOP;
END IF;
-- hypertable with invalidation threshold in the future
IF EXISTS(SELECT FROM pg_class c JOIN pg_namespace nsp ON c.relnamespace=nsp.oid AND nspname = '_timescaledb_catalog' WHERE relname='continuous_aggs_invalidation_threshold') THEN
SET LOCAL search_path TO _timescaledb_functions, _timescaledb_internal, pg_catalog, pg_temp;
IF EXISTS(SELECT FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold WHERE to_timestamp(watermark) > now()) THEN
RAISE WARNING 'Found hypertables with invalidation threshold in the future: %', (
SELECT array_agg(format('%s: %s', format('%I.%I',ht.schema_name,ht.table_name), _timescaledb_functions.to_timestamp(watermark)))
FROM _timescaledb_catalog.continuous_aggs_invalidation_threshold i
JOIN _timescaledb_catalog.hypertable ht ON ht.id = i.hypertable_id
WHERE _timescaledb_functions.to_timestamp(watermark) > now()
);
END IF;
SET LOCAL search_path TO pg_catalog, pg_temp;
END IF;
-- continuous aggregates with chunk interval smaller than bucket width
IF EXISTS(SELECT FROM pg_proc p JOIN pg_namespace nsp ON p.pronamespace=nsp.oid AND nsp.nspname = '_timescaledb_functions' WHERE proname='cagg_get_bucket_function_info') THEN
FOR v_cagg, v_chunk_width, v_cagg_width IN
SELECT
format('%I.%I', c.user_view_schema, c.user_view_name)::regclass AS continuous_aggregate,
CASE
WHEN d.column_type =ANY('{int4,int8}'::regtype[]) THEN d.interval_length::text
WHEN d.column_type =ANY('{timestamp,timestamptz}'::regtype[]) THEN _timescaledb_functions.to_interval(d.interval_length)::text
END AS chunk_width,
f.bucket_width cagg_width
FROM _timescaledb_catalog.continuous_agg c
JOIN LATERAL(SELECT * FROM _timescaledb_functions.cagg_get_bucket_function_info(c.mat_hypertable_id)) f on true
JOIN _timescaledb_catalog.dimension d ON d.hypertable_id=c.mat_hypertable_id
WHERE
CASE
WHEN d.column_type =ANY('{int4,int8}'::regtype[]) THEN d.interval_length <= f.bucket_width::int
WHEN d.column_type =ANY('{timestamp,timestamptz}'::regtype[]) THEN _timescaledb_functions.to_interval(d.interval_length) <= f.bucket_width::interval
END
LOOP
RAISE WARNING 'Continuous aggregate % has chunk width smaller than bucket width % <= %', v_cagg, v_chunk_width, v_cagg_width;
END LOOP;
END IF;
END
$$;
-- compression checks
-- compressed chunk batch sizes
DO $$
DECLARE
v_hypertable_id int;
v_hypertable regclass;
v_chunk regclass;
v_compressed_chunk regclass;
v_batch_sub100 int8;
v_batch_sub100_pct float;
v_batch_sub100_avg float;
v_batch_total int8;
v_chunks_sub100 int8;
v_chunks_total int8;
v_returned_rows int;
BEGIN
SET LOCAL search_path TO pg_catalog, pg_temp;
-- pre 2.29.0
IF EXISTS (SELECT FROM pg_attribute WHERE attrelid = '_timescaledb_catalog.hypertable'::regclass AND attname = 'compressed_hypertable_id') THEN
FOR v_hypertable_id, v_hypertable IN SELECT id, format('%I.%I',schema_name, table_name)::regclass FROM _timescaledb_catalog.hypertable WHERE compressed_hypertable_id IS NOT NULL
LOOP
v_chunks_total := 0;
v_chunks_sub100 := 0;
RAISE NOTICE 'Checking hypertable %', v_hypertable;
FOR v_chunk, v_compressed_chunk IN
SELECT
format('%I.%I',ch.schema_name, ch.table_name) chunk,
format('%I.%I',ch2.schema_name, ch2.table_name) compressed
FROM _timescaledb_catalog.chunk ch
JOIN pg_class c_ch ON c_ch.relname = ch.table_name AND c_ch.relnamespace = ch.schema_name::regnamespace
JOIN _timescaledb_catalog.chunk ch2 ON ch2.id = ch.compressed_chunk_id
JOIN pg_class c_ch2 ON c_ch2.relname = ch2.table_name AND c_ch2.relnamespace = ch2.schema_name::regnamespace
WHERE ch.hypertable_id = v_hypertable_id
LOOP
RAISE DEBUG ' Checking chunk: %', v_chunk;
v_chunks_total := v_chunks_total + 1;
-- check if batch has more than 20% of batches with less than 100 tuples
EXECUTE format('
SELECT
count(_ts_meta_count) FILTER (WHERE _ts_meta_count < 100) batch_sub100,
avg(_ts_meta_count) FILTER (WHERE _ts_meta_count < 100) batch_sub100_avg,
(count(_ts_meta_count) FILTER (WHERE _ts_meta_count < 100)/count(_ts_meta_count)::float) * 100 batch_sub100_pct,
count(_ts_meta_count) batch_total
FROM %s HAVING (count(_ts_meta_count) FILTER (WHERE _ts_meta_count < 100)/count(_ts_meta_count)::float) > 0.2;
', v_compressed_chunk) INTO v_batch_sub100, v_batch_sub100_avg, v_batch_sub100_pct, v_batch_total;
GET DIAGNOSTICS v_returned_rows = ROW_COUNT;
IF v_returned_rows > 0 THEN
v_chunks_sub100 := v_chunks_sub100 + 1;
-- only print first 5 warnings to avoid flooding
IF v_chunks_sub100 <= 5 THEN
RAISE WARNING ' Chunk % has %/% (% %% avg %) batches with less than 100 tuples.', v_chunk, v_batch_sub100, v_batch_total, v_batch_sub100_pct::decimal(4,1), v_batch_sub100_avg::decimal(4,1);
END IF;
END IF;
END LOOP;
IF v_chunks_sub100 > 0 THEN
RAISE WARNING '%/% chunks found with more than 20%% of batches having less than 100 tuples in %.', v_chunks_sub100, v_chunks_total, v_hypertable;
END IF;
END LOOP;
END IF;
-- 2.29.0 and later
IF NOT EXISTS (SELECT FROM pg_attribute WHERE attrelid = '_timescaledb_catalog.hypertable'::regclass AND attname = 'compressed_hypertable_id') THEN
FOR v_hypertable_id, v_hypertable IN SELECT id, format('%I.%I',schema_name, table_name)::regclass FROM _timescaledb_catalog.hypertable WHERE status & 4 = 4
LOOP
v_chunks_total := 0;
v_chunks_sub100 := 0;
RAISE NOTICE 'Checking hypertable %', v_hypertable;
FOR v_chunk, v_compressed_chunk IN
SELECT
ch.relid, cs.compress_relid
FROM _timescaledb_catalog.chunk ch
JOIN compression_settings cs ON cs.relid = ch.relid
WHERE ch.hypertable_id = v_hypertable_id
LOOP
RAISE DEBUG ' Checking chunk: %', v_chunk;
v_chunks_total := v_chunks_total + 1;
-- check if batch has more than 20% of batches with less than 100 tuples
EXECUTE format('
SELECT
count(_ts_meta_count) FILTER (WHERE _ts_meta_count < 100) batch_sub100,
avg(_ts_meta_count) FILTER (WHERE _ts_meta_count < 100) batch_sub100_avg,
(count(_ts_meta_count) FILTER (WHERE _ts_meta_count < 100)/count(_ts_meta_count)::float) * 100 batch_sub100_pct,
count(_ts_meta_count) batch_total
FROM %s HAVING (count(_ts_meta_count) FILTER (WHERE _ts_meta_count < 100)/count(_ts_meta_count)::float) > 0.2;
', v_compressed_chunk) INTO v_batch_sub100, v_batch_sub100_avg, v_batch_sub100_pct, v_batch_total;
GET DIAGNOSTICS v_returned_rows = ROW_COUNT;
IF v_returned_rows > 0 THEN
v_chunks_sub100 := v_chunks_sub100 + 1;
-- only print first 5 warnings to avoid flooding
IF v_chunks_sub100 <= 5 THEN
RAISE WARNING ' Chunk % has %/% (% %% avg %) batches with less than 100 tuples.', v_chunk, v_batch_sub100, v_batch_total, v_batch_sub100_pct::decimal(4,1), v_batch_sub100_avg::decimal(4,1);
END IF;
END IF;
END LOOP;
IF v_chunks_sub100 > 0 THEN
RAISE WARNING '%/% chunks found with more than 20%% of batches having less than 100 tuples in %.', v_chunks_sub100, v_chunks_total, v_hypertable;
END IF;
END LOOP;
END IF;
END
$$;
-- composite bloom filter columns not matching compression settings
-- Renaming a column used in a composite bloom filter could leave the bloom
-- column in the compressed chunk under its old name. The bloom filter is then
-- ignored, or used for a different column and returns wrong results.
-- Composite bloom columns created before 2.27.0 also use an older naming scheme
-- that later versions no longer find. Recompressing the chunk fixes both.
DO $$
DECLARE
v_version int[];
v_prefix text;
v_count int8;
v_chunks regclass[];
BEGIN
SET LOCAL search_path TO pg_catalog, pg_temp;
v_version := string_to_array(split_part((SELECT extversion FROM pg_extension WHERE extname='timescaledb'), '-', 1), '.')::int[];
v_prefix := current_setting('timescaledb.bloom1_column_prefix', true);
-- composite bloom filters were added in 2.26.0
IF v_version >= '{2,26,0}' AND v_prefix IS NOT NULL THEN
SELECT count(DISTINCT relid), array_agg(DISTINCT relid) INTO v_count, v_chunks
FROM (
SELECT s.relid
FROM _timescaledb_catalog.compression_settings s,
jsonb_array_elements(s.index) e,
LATERAL (
SELECT
string_agg(c, '_' ORDER BY i) AS joined,
string_agg(convert_to(c, current_setting('server_encoding')), '\x00'::bytea ORDER BY i) AS hash_input
FROM jsonb_array_elements_text(e->'column') WITH ORDINALITY AS x(c, i)
) n,
LATERAL (
SELECT CASE
WHEN v_version >= '{2,27,0}' THEN
format('_ts_meta_v2_%s_%s_%s', left(v_prefix, 6), left(md5(n.hash_input), 4), left(n.joined, 39))
WHEN length(n.joined) > 39 THEN
format('_ts_meta_v2_%s_%s_%s', left(v_prefix, 6), left(md5(n.joined), 4), left(n.joined, 39))
ELSE
format('_ts_meta_v2_%s_%s', left(v_prefix, 6), n.joined)
END AS expected_column
) m
WHERE
s.compress_relid IS NOT NULL AND
e->>'type' = 'bloom' AND
jsonb_typeof(e->'column') = 'array' AND
-- OFFSET 0 keeps this a per-row index lookup instead of a join over all of pg_attribute
NOT EXISTS(
SELECT FROM pg_attribute a
WHERE a.attrelid = s.compress_relid AND a.attname = m.expected_column::name AND NOT a.attisdropped
OFFSET 0)
) broken;
IF v_count > 0 THEN
RAISE WARNING 'Found % compressed chunks with composite bloom filter columns not matching their compression settings: %', v_count, v_chunks[1:20];
RAISE WARNING ' Recompress these chunks to rebuild the bloom filters.';
END IF;
END IF;
END
$$;