-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhypercore.sql
More file actions
81 lines (67 loc) · 3.3 KB
/
hypercore.sql
File metadata and controls
81 lines (67 loc) · 3.3 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
drop table if exists metrics cascade;
drop table if exists metric_readings cascade;
-- Enable pgcrypto extension for gen_random_uuid()
CREATE EXTENSION IF NOT EXISTS pgcrypto;
-- Create table
create table metrics (
uuid uuid primary key default gen_random_uuid(),
name text not null
);
CREATE TABLE metric_readings (
time TIMESTAMPTZ NOT NULL,
metric_uuid uuid references metrics(uuid),
value DOUBLE PRECISION
);
-- Convert to hypertable
SELECT create_hypertable('metric_readings', by_range('time', INTERVAL '1 day'));
-- Create secondary indexes
CREATE INDEX metric_readings_metric_uuid_hash_idx ON metric_readings USING hash (metric_uuid);
CREATE UNIQUE INDEX metric_readings_metric_uuid_time_idx ON metric_readings (metric_uuid, time);
alter table metric_readings set access method hypercore;
alter table metric_readings
set (
timescaledb.orderby = 'time',
timescaledb.segmentby = 'metric_uuid'
);
alter table metric_readings
set (
timescaledb.compress,
timescaledb.compress_segmentby = 'metric_uuid',
timescaledb.compress_orderby = 'time'
);
-- Create compression policy
SELECT add_compression_policy('metric_readings', INTERVAL '3 days', if_not_exists => true);
-- Create columnstore policy
CALL add_columnstore_policy( 'metric_readings', interval '1 day', if_not_exists => true);
insert into metrics (name) values ('temperature'),('humidity'),('pressure');
with temperature_uuid as (select uuid from metrics where name = 'temperature'),
humidity_uuid as (select uuid from metrics where name = 'humidity'),
pressure_uuid as (select uuid from metrics where name = 'pressure')
INSERT INTO metric_readings (time, metric_uuid, value)
VALUES
(now(), (select uuid from temperature_uuid), 100),
(now() - interval '1 day', (select uuid from temperature_uuid), 22.0),
(now() - interval '2 day', (select uuid from temperature_uuid), 22.3),
(now() - interval '3 day', (select uuid from temperature_uuid), 22.7),
(now() - interval '4 day', (select uuid from temperature_uuid), 22.0),
(now() - interval '5 day', (select uuid from temperature_uuid), 21.0),
(now() - interval '6 day', (select uuid from temperature_uuid), 20.0),
(now() - interval '7 day', (select uuid from temperature_uuid), 19.0),
(now(), (select uuid from humidity_uuid), 100),
(now() - interval '1 day', (select uuid from humidity_uuid), 50.0),
(now() - interval '2 day', (select uuid from humidity_uuid), 55.0),
(now() - interval '3 day', (select uuid from humidity_uuid), 60.0),
(now() - interval '4 day', (select uuid from humidity_uuid), 65.0),
(now() - interval '5 day', (select uuid from humidity_uuid), 70.0),
(now() - interval '6 day', (select uuid from humidity_uuid), 75.0),
(now() - interval '7 day', (select uuid from humidity_uuid), 80.0),
(now(), (select uuid from pressure_uuid), 1000),
(now() - interval '1 day', (select uuid from pressure_uuid), 1001),
(now() - interval '2 day', (select uuid from pressure_uuid), 1002),
(now() - interval '3 day', (select uuid from pressure_uuid), 1003),
(now() - interval '4 day', (select uuid from pressure_uuid), 1004),
(now() - interval '5 day', (select uuid from pressure_uuid), 1005),
(now() - interval '6 day', (select uuid from pressure_uuid), 1006),
(now() - interval '7 day', (select uuid from pressure_uuid), 1007)
ON CONFLICT (metric_uuid, time) DO UPDATE SET value = EXCLUDED.value;
SELECT * FROM metric_readings;