forked from timescale/timescaledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.continuous_aggs.v2.sql
330 lines (305 loc) · 11.3 KB
/
setup.continuous_aggs.v2.sql
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
-- 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.
SELECT extversion < '2.0.0' AS has_refresh_mat_view from pg_extension WHERE extname = 'timescaledb' \gset
CREATE TYPE custom_type AS (high int, low int);
CREATE TABLE conditions_before (
timec TIMESTAMPTZ NOT NULL,
location TEXT NOT NULL,
temperature DOUBLE PRECISION NULL,
humidity DOUBLE PRECISION NULL,
lowp double precision NULL,
highp double precision null,
allnull double precision null,
highlow custom_type null,
bit_int smallint,
good_life boolean
);
SELECT table_name FROM create_hypertable( 'conditions_before', 'timec');
INSERT INTO conditions_before
SELECT generate_series('2018-12-01 00:00'::timestamp, '2018-12-31 00:00'::timestamp, '1 day'), 'POR', 55, 75, 40, 70, NULL, (1,2)::custom_type, 2, true;
INSERT INTO conditions_before
SELECT generate_series('2018-11-01 00:00'::timestamp, '2018-12-31 00:00'::timestamp, '1 day'), 'NYC', 35, 45, 50, 40, NULL, (3,4)::custom_type, 4, false;
INSERT INTO conditions_before
SELECT generate_series('2018-11-01 00:00'::timestamp, '2018-12-15 00:00'::timestamp, '1 day'), 'LA', 73, 55, NULL, 28, NULL, NULL, 8, true;
DO LANGUAGE PLPGSQL $$
DECLARE
ts_version TEXT;
BEGIN
SELECT extversion INTO ts_version FROM pg_extension WHERE extname = 'timescaledb';
IF ts_version < '2.0.0' THEN
CREATE VIEW mat_before
WITH ( timescaledb.continuous, timescaledb.materialized_only=true, timescaledb.refresh_lag='-30 day', timescaledb.max_interval_per_job ='1000 day')
AS
SELECT time_bucket('1week', timec) as bucket,
location,
min(allnull) as min_allnull,
max(temperature) as max_temp,
sum(temperature)+sum(humidity) as agg_sum_expr,
avg(humidity),
stddev(humidity),
bit_and(bit_int),
bit_or(bit_int),
bool_and(good_life),
every(temperature > 0),
bool_or(good_life),
count(*) as count_rows,
count(temperature) as count_temp,
count(allnull) as count_zero,
corr(temperature, humidity),
covar_pop(temperature, humidity),
covar_samp(temperature, humidity),
regr_avgx(temperature, humidity),
regr_avgy(temperature, humidity),
regr_count(temperature, humidity),
regr_intercept(temperature, humidity),
regr_r2(temperature, humidity),
regr_slope(temperature, humidity),
regr_sxx(temperature, humidity),
regr_sxy(temperature, humidity),
regr_syy(temperature, humidity),
stddev(temperature) as stddev_temp,
stddev_pop(temperature),
stddev_samp(temperature),
variance(temperature),
var_pop(temperature),
var_samp(temperature),
last(temperature, timec) as last_temp,
last(highlow, timec) as last_hl,
first(highlow, timec) as first_hl,
histogram(temperature, 0, 100, 5)
FROM conditions_before
GROUP BY bucket, location
HAVING min(location) >= 'NYC' and avg(temperature) > 2;
ELSE
CREATE MATERIALIZED VIEW IF NOT EXISTS mat_before
WITH ( timescaledb.continuous, timescaledb.materialized_only=true)
AS
SELECT time_bucket('1week', timec) as bucket,
location,
min(allnull) as min_allnull,
max(temperature) as max_temp,
sum(temperature)+sum(humidity) as agg_sum_expr,
avg(humidity),
stddev(humidity),
bit_and(bit_int),
bit_or(bit_int),
bool_and(good_life),
every(temperature > 0),
bool_or(good_life),
count(*) as count_rows,
count(temperature) as count_temp,
count(allnull) as count_zero,
corr(temperature, humidity),
covar_pop(temperature, humidity),
covar_samp(temperature, humidity),
regr_avgx(temperature, humidity),
regr_avgy(temperature, humidity),
regr_count(temperature, humidity),
regr_intercept(temperature, humidity),
regr_r2(temperature, humidity),
regr_slope(temperature, humidity),
regr_sxx(temperature, humidity),
regr_sxy(temperature, humidity),
regr_syy(temperature, humidity),
stddev(temperature) as stddev_temp,
stddev_pop(temperature),
stddev_samp(temperature),
variance(temperature),
var_pop(temperature),
var_samp(temperature),
last(temperature, timec) as last_temp,
last(highlow, timec) as last_hl,
first(highlow, timec) as first_hl,
histogram(temperature, 0, 100, 5)
FROM conditions_before
GROUP BY bucket, location
HAVING min(location) >= 'NYC' and avg(temperature) > 2 WITH NO DATA;
PERFORM add_continuous_aggregate_policy('mat_before', NULL, '-30 days'::interval, '336 h');
END IF;
END $$;
\if :has_refresh_mat_view
REFRESH MATERIALIZED VIEW mat_before;
\else
CALL refresh_continuous_aggregate('mat_before',NULL,NULL);
\endif
-- we create separate schema for realtime agg since we dump all view definitions in public schema
-- but realtime agg view definition is not stable across versions
CREATE SCHEMA cagg;
DO LANGUAGE PLPGSQL $$
DECLARE
ts_version TEXT;
BEGIN
SELECT extversion INTO ts_version FROM pg_extension WHERE extname = 'timescaledb';
IF ts_version < '2.0.0' THEN
CREATE VIEW cagg.realtime_mat
WITH ( timescaledb.continuous, timescaledb.materialized_only=false, timescaledb.refresh_lag='-30 day', timescaledb.max_interval_per_job ='1000 day')
AS
SELECT time_bucket('1week', timec) as bucket,
location,
min(allnull) as min_allnull,
max(temperature) as max_temp,
sum(temperature)+sum(humidity) as agg_sum_expr,
avg(humidity),
stddev(humidity),
bit_and(bit_int),
bit_or(bit_int),
bool_and(good_life),
every(temperature > 0),
bool_or(good_life),
count(*) as count_rows,
count(temperature) as count_temp,
count(allnull) as count_zero,
corr(temperature, humidity),
covar_pop(temperature, humidity),
covar_samp(temperature, humidity),
regr_avgx(temperature, humidity),
regr_avgy(temperature, humidity),
regr_count(temperature, humidity),
regr_intercept(temperature, humidity),
regr_r2(temperature, humidity),
regr_slope(temperature, humidity),
regr_sxx(temperature, humidity),
regr_sxy(temperature, humidity),
regr_syy(temperature, humidity),
stddev(temperature) as stddev_temp,
stddev_pop(temperature),
stddev_samp(temperature),
variance(temperature),
var_pop(temperature),
var_samp(temperature),
last(temperature, timec) as last_temp,
last(highlow, timec) as last_hl,
first(highlow, timec) as first_hl,
histogram(temperature, 0, 100, 5)
FROM conditions_before
GROUP BY bucket, location
HAVING min(location) >= 'NYC' and avg(temperature) > 2;
ELSE
CREATE MATERIALIZED VIEW IF NOT EXISTS cagg.realtime_mat
WITH ( timescaledb.continuous, timescaledb.materialized_only=false)
AS
SELECT time_bucket('1week', timec) as bucket,
location,
min(allnull) as min_allnull,
max(temperature) as max_temp,
sum(temperature)+sum(humidity) as agg_sum_expr,
avg(humidity),
stddev(humidity),
bit_and(bit_int),
bit_or(bit_int),
bool_and(good_life),
every(temperature > 0),
bool_or(good_life),
count(*) as count_rows,
count(temperature) as count_temp,
count(allnull) as count_zero,
corr(temperature, humidity),
covar_pop(temperature, humidity),
covar_samp(temperature, humidity),
regr_avgx(temperature, humidity),
regr_avgy(temperature, humidity),
regr_count(temperature, humidity),
regr_intercept(temperature, humidity),
regr_r2(temperature, humidity),
regr_slope(temperature, humidity),
regr_sxx(temperature, humidity),
regr_sxy(temperature, humidity),
regr_syy(temperature, humidity),
stddev(temperature) as stddev_temp,
stddev_pop(temperature),
stddev_samp(temperature),
variance(temperature),
var_pop(temperature),
var_samp(temperature),
last(temperature, timec) as last_temp,
last(highlow, timec) as last_hl,
first(highlow, timec) as first_hl,
histogram(temperature, 0, 100, 5)
FROM conditions_before
GROUP BY bucket, location
HAVING min(location) >= 'NYC' and avg(temperature) > 2 WITH NO DATA;
PERFORM add_continuous_aggregate_policy('cagg.realtime_mat', NULL, '-30 days'::interval, '336 h');
END IF;
END $$;
\if :has_refresh_mat_view
REFRESH MATERIALIZED VIEW cagg.realtime_mat;
\else
CALL refresh_continuous_aggregate('cagg.realtime_mat',NULL,NULL);
\endif
-- test ignore_invalidation_older_than migration --
DO LANGUAGE PLPGSQL $$
DECLARE
ts_version TEXT;
BEGIN
SELECT extversion INTO ts_version FROM pg_extension WHERE extname = 'timescaledb';
IF ts_version < '2.0.0' THEN
CREATE VIEW mat_ignoreinval
WITH ( timescaledb.continuous, timescaledb.materialized_only=true,
timescaledb.refresh_lag='-30 day',
timescaledb.ignore_invalidation_older_than='30 days',
timescaledb.max_interval_per_job = '100000 days')
AS
SELECT time_bucket('1 week', timec) as bucket,
max(temperature) as maxtemp
FROM conditions_before
GROUP BY bucket;
ELSE
CREATE MATERIALIZED VIEW IF NOT EXISTS mat_ignoreinval
WITH ( timescaledb.continuous, timescaledb.materialized_only=true)
AS
SELECT time_bucket('1 week', timec) as bucket,
max(temperature) as maxtemp
FROM conditions_before
GROUP BY bucket WITH NO DATA;
PERFORM add_continuous_aggregate_policy('mat_ignoreinval', '30 days'::interval, '-30 days'::interval, '336 h');
END IF;
END $$;
\if :has_refresh_mat_view
REFRESH MATERIALIZED VIEW mat_ignoreinval;
\else
CALL refresh_continuous_aggregate('mat_ignoreinval',NULL,NULL);
\endif
-- test new data beyond the invalidation threshold is properly handled --
CREATE TABLE inval_test (time TIMESTAMPTZ, location TEXT, temperature DOUBLE PRECISION);
SELECT create_hypertable('inval_test', 'time', chunk_time_interval => INTERVAL '1 week');
INSERT INTO inval_test
SELECT generate_series('2018-12-01 00:00'::timestamp, '2018-12-20 00:00'::timestamp, '1 day'), 'POR', generate_series(40.5, 50.0, 0.5);
INSERT INTO inval_test
SELECT generate_series('2018-12-01 00:00'::timestamp, '2018-12-20 00:00'::timestamp, '1 day'), 'NYC', generate_series(31.0, 50.0, 1.0);
DO LANGUAGE PLPGSQL $$
DECLARE
ts_version TEXT;
BEGIN
SELECT extversion INTO ts_version FROM pg_extension WHERE extname = 'timescaledb';
IF ts_version < '2.0.0' THEN
CREATE VIEW mat_inval
WITH ( timescaledb.continuous, timescaledb.materialized_only=true,
timescaledb.refresh_lag='-20 days',
timescaledb.refresh_interval='12 hours',
timescaledb.max_interval_per_job='100000 days' )
AS
SELECT time_bucket('10 minute', time) as bucket, location, min(temperature) as min_temp,
max(temperature) as max_temp, avg(temperature) as avg_temp
FROM inval_test
GROUP BY bucket, location;
ELSE
CREATE MATERIALIZED VIEW mat_inval
WITH ( timescaledb.continuous, timescaledb.materialized_only=true )
AS
SELECT time_bucket('10 minute', time) as bucket, location, min(temperature) as min_temp,
max(temperature) as max_temp, avg(temperature) as avg_temp
FROM inval_test
GROUP BY bucket, location WITH NO DATA;
PERFORM add_continuous_aggregate_policy('mat_inval', NULL, '-20 days'::interval, '12 hours');
END IF;
END $$;
\if :has_refresh_mat_view
REFRESH MATERIALIZED VIEW mat_inval;
\else
CALL refresh_continuous_aggregate('mat_inval',NULL,NULL);
\endif
INSERT INTO inval_test
SELECT generate_series('2118-12-01 00:00'::timestamp, '2118-12-20 00:00'::timestamp, '1 day'), 'POR', generate_series(135.25, 140.0, 0.25);
INSERT INTO inval_test
SELECT generate_series('2118-12-01 00:00'::timestamp, '2118-12-20 00:00'::timestamp, '1 day'), 'NYC', generate_series(131.0, 150.0, 1.0);