|
4 | 4 | "subpartitions" schema - subpartitions of "public" schema top level metric tables (if using time / dbname-time partitioning) |
5 | 5 | */ |
6 | 6 |
|
7 | | -create schema "admin"; |
8 | | -create schema "subpartitions"; |
| 7 | +CREATE SCHEMA IF NOT EXISTS "admin"; |
| 8 | +CREATE SCHEMA IF NOT EXISTS "subpartitions"; |
9 | 9 |
|
10 | | -create extension if not exists btree_gin; |
| 10 | +CREATE EXTENSION IF NOT EXISTS btree_gin; |
11 | 11 |
|
12 | | -create function admin.get_default_storage_type() returns text as |
| 12 | +CREATE OR REPLACE FUNCTION admin.get_default_storage_type() RETURNS text as |
13 | 13 | $$ |
14 | | - select case |
15 | | - when exists(select 1 from pg_extension where extname = 'timescaledb') then |
| 14 | + SELECT CASE |
| 15 | + WHEN EXISTS(SELECT 1 FROM pg_extension WHERE extname = 'timescaledb') THEN |
16 | 16 | 'timescale' |
17 | | - else |
| 17 | + ELSE |
18 | 18 | 'postgres' |
19 | | - end; |
| 19 | + END; |
20 | 20 | $$ |
21 | | -language sql; |
| 21 | +LANGUAGE SQL; |
22 | 22 |
|
23 | | -create table admin.storage_schema_type ( |
24 | | - schema_type text not null default admin.get_default_storage_type(), |
25 | | - initialized_on timestamptz not null default now(), |
| 23 | +CREATE TABLE admin.storage_schema_type ( |
| 24 | + schema_type text NOT NULL DEFAULT admin.get_default_storage_type(), |
| 25 | + initialized_on timestamptz NOT NULL DEFAULT now(), |
26 | 26 | check (schema_type in ('postgres', 'timescale')) |
27 | 27 | ); |
28 | 28 |
|
29 | | -insert into admin.storage_schema_type default values; |
| 29 | +INSERT INTO admin.storage_schema_type DEFAULT values; |
30 | 30 |
|
31 | | -comment on table admin.storage_schema_type is 'identifies storage schema for other pgwatch components'; |
| 31 | +COMMENT ON TABLE admin.storage_schema_type IS 'identifies storage schema for other pgwatch components'; |
32 | 32 |
|
33 | | -create unique index max_one_row on admin.storage_schema_type ((1)); |
| 33 | +CREATE UNIQUE INDEX max_one_row ON admin.storage_schema_type ((1)); |
34 | 34 |
|
35 | 35 | /* for the Grafana drop-down. managed by the gatherer */ |
36 | | -create table admin.all_distinct_dbname_metrics ( |
37 | | - dbname text not null, |
38 | | - metric text not null, |
39 | | - created_on timestamptz not null default now(), |
40 | | - primary key (dbname, metric) |
| 36 | +CREATE TABLE admin.all_distinct_dbname_metrics ( |
| 37 | + dbname text NOT NULL, |
| 38 | + metric text NOT NULL, |
| 39 | + created_on timestamptz NOT NULL DEFAULT now(), |
| 40 | + PRIMARY KEY (dbname, metric) |
41 | 41 | ); |
42 | 42 |
|
43 | 43 | /* currently only used to store TimescaleDB chunk interval */ |
44 | | -create table admin.config ( |
45 | | - key text not null primary key, |
46 | | - value text not null, |
47 | | - created_on timestamptz not null default now(), |
| 44 | +CREATE TABLE admin.config ( |
| 45 | + key text NOT NULL PRIMARY KEY, |
| 46 | + value text NOT NULL, |
| 47 | + created_on timestamptz NOT NULL DEFAULT now(), |
48 | 48 | last_modified_on timestamptz |
49 | 49 | ); |
50 | 50 |
|
51 | 51 | -- to later change the value call the admin.change_timescale_chunk_interval(interval) function! |
52 | 52 | -- as changing the row directly will only be effective for completely new tables (metrics). |
53 | | -insert into admin.config (key, value) values |
| 53 | +INSERT INTO admin.config (key, value) VALUES |
54 | 54 | ('timescale_chunk_interval', '2 days'), |
55 | 55 | ('timescale_compress_interval', '1 day'); |
56 | 56 |
|
57 | | -create or replace function trg_config_modified() returns trigger |
58 | | -as $$ |
59 | | -begin |
| 57 | +CREATE OR REPLACE FUNCTION trg_config_modified() RETURNS trigger |
| 58 | +AS $$ |
| 59 | +BEGIN |
60 | 60 | new.last_modified_on = now(); |
61 | | - return new; |
62 | | -end; |
| 61 | + RETURN new; |
| 62 | +END; |
63 | 63 | $$ |
64 | | -language plpgsql; |
| 64 | +LANGUAGE plpgsql; |
65 | 65 |
|
66 | | -create trigger config_modified before update on admin.config |
67 | | -for each row execute function trg_config_modified(); |
| 66 | +CREATE TRIGGER config_modified BEFORE UPDATE ON admin.config |
| 67 | +FOR EACH ROW EXECUTE FUNCTION trg_config_modified(); |
68 | 68 |
|
69 | | -/* |
70 | | - creates a top level metric table if not already existing (non-existing tables show ugly warnings in Grafana). |
71 | | - expects the "metrics_template" table to exist. |
72 | | -*/ |
73 | | -create or replace function admin.ensure_dummy_metrics_table(metric text) returns boolean as |
74 | | -$sql$ |
75 | | -declare |
76 | | - l_schema_type text; |
77 | | -begin |
78 | | - select schema_type into l_schema_type from admin.storage_schema_type; |
79 | | - |
80 | | - if to_regclass(format('public.%I', metric)) is not null then |
81 | | - return false; |
82 | | - end if; |
83 | | - |
84 | | - if l_schema_type = 'postgres' then |
85 | | - execute format('CREATE TABLE public.%I (LIKE admin.metrics_template INCLUDING INDEXES) PARTITION BY LIST (dbname)', metric); |
86 | | - elsif l_schema_type = 'timescale' then |
87 | | - perform admin.ensure_partition_timescale(metric); |
88 | | - end if; |
89 | | - |
90 | | - execute format('COMMENT ON TABLE public.%I IS $$pgwatch-generated-metric-lvl$$', metric); |
91 | | - |
92 | | - return true; |
93 | | -end; |
94 | | -$sql$ language plpgsql; |
95 | | - |
96 | | -create table admin.metrics_template ( |
97 | | - time timestamptz not null default now(), |
98 | | - dbname text not null, |
99 | | - data jsonb not null, |
| 69 | +CREATE TABLE admin.metrics_template ( |
| 70 | + time timestamptz NOT NULL DEFAULT now(), |
| 71 | + dbname text NOT NULL, |
| 72 | + data jsonb NOT NULL, |
100 | 73 | tag_data jsonb, |
101 | | - check (false) |
| 74 | + CHECK (false) |
102 | 75 | ); |
103 | 76 |
|
104 | | -comment on table admin.metrics_template IS 'used as a template for all new metric definitions'; |
| 77 | +COMMENT ON TABLE admin.metrics_template IS 'used as a template for all new metric definitions'; |
105 | 78 |
|
106 | | -create index on admin.metrics_template (dbname, time); |
| 79 | +CREATE INDEX ON admin.metrics_template (dbname, time); |
107 | 80 | -- create index on admin.metrics_template using brin (dbname, time); /* consider BRIN instead for large data amounts */ |
108 | 81 | -- create index on admin.metrics_template using gin (tag_data) where tag_data notnull; |
109 | 82 |
|
110 | | --- define migrations you need to apply |
111 | | --- every change to the database schema should populate this table. |
112 | | --- Version value should contain issue number zero padded followed by |
113 | | --- short description of the issue\feature\bug implemented\resolved |
| 83 | +/* |
| 84 | + Define migrations you need to apply. Every change to the |
| 85 | + database schema should populate this table. |
| 86 | + Version value should contain issue number zero padded followed by |
| 87 | + short description of the issue\feature\bug implemented\resolved |
| 88 | +*/ |
114 | 89 | CREATE TABLE admin.migration( |
115 | 90 | id bigint PRIMARY KEY, |
116 | 91 | version text NOT NULL |
|
0 commit comments