-
Notifications
You must be signed in to change notification settings - Fork 1
/
pg96-partitioning.sql
138 lines (104 loc) · 3.69 KB
/
pg96-partitioning.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
---
--- Create parent table Insert data
---
CREATE TABLE jane (id serial PRIMARY KEY, record_time TIMESTAMP NOT NULL, level INTEGER, msg TEXT);
---
--- Insert data into parent table
---
INSERT INTO jane (record_time, level, msg)
SELECT tz,random() * 6 as rand, md5(tz::text)
FROM generate_series('2018-01-01 00:00'::timestamp,'2018-07-31 12:00', '1 minute') as tz ;
---
--- Create child table
---
CREATE TABLE jane_p2018_01 (
CHECK ( record_time >= DATE '2018-01-01' AND record_time < DATE '2018-02-01' )
) INHERITS (jane);
CREATE TABLE jane_p2018_02 (
CHECK ( record_time >= DATE '2018-02-01' AND record_time < DATE '2018-03-01' )
) INHERITS (jane);
CREATE TABLE jane_p2018_03 (
CHECK ( record_time >= DATE '2018-03-01' AND record_time < DATE '2018-04-01' )
) INHERITS (jane);
CREATE TABLE jane_p2018_04 (
CHECK ( record_time >= DATE '2018-04-01' AND record_time < DATE '2018-05-01' )
) INHERITS (jane);
---
--- create index on the key column
---
CREATE INDEX jane_p2018_01_date_idx ON jane_p2018_01 (record_time);
CREATE INDEX jane_p2018_01_date_idx ON jane_p2018_02 (record_time);
CREATE INDEX jane_p2018_01_date_idx ON jane_p2018_03 (record_time);
CREATE INDEX jane_p2018_01_date_idx ON jane_p2018_04 (record_time);
---
--- create function to invoke trigger
---
CREATE OR REPLACE FUNCTION jane_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW.record_time >= DATE '2018-01-01' AND
NEW.record_time < DATE '2018-02-01' ) THEN
INSERT INTO jane_p2018_01 VALUES (NEW.*);
ELSIF ( NEW.record_time >= DATE '2018-02-01' AND
NEW.record_time < DATE '2018-03-01' ) THEN
INSERT INTO jane_p2018_02 VALUES (NEW.*);
ELSIF ( NEW.record_time >= DATE '2018-03-01' AND
NEW.record_time < DATE '2018-04-01' ) THEN
INSERT INTO jane_p2018_03 VALUES (NEW.*);
ELSIF ( NEW.record_time >= DATE '2018-04-01' AND
NEW.record_time < DATE '2018-05-01' ) THEN
INSERT INTO jane_p2018_04 VALUES (NEW.*);
ELSE
RAISE EXCEPTION 'Date out of range. Fix the jane_insert_trigger() function!';
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
---
--- create insert trigger
---
CREATE TRIGGER insert_jane_trigger
BEFORE INSERT ON jane
FOR EACH ROW EXECUTE PROCEDURE jane_insert_trigger();
---
--- move existing data from parent to child table
---
WITH deleted as (
DELETE FROM jane where record_time >= '2018-01-01' and record_time < '2018-02-01'
RETURNING *
)
INSERT INTO jane (record_time, level, msg) SELECT record_time, level, msg FROM deleted ;
---
--- clean up
---
DROP TABLE jane CASCADE ;
---
--- end of story
---
---
--- partman
---
CREATE TABLE partman_table (id serial, record_time TIMESTAMP NOT NULL, level INTEGER, msg TEXT);
INSERT INTO partman_table (record_time, level, msg)
SELECT tz,random() * 6 as rand, md5(tz::text)
FROM generate_series('2018-01-01 00:00'::timestamp,'2018-07-31 12:00', '1 minute') as tz;
SELECT create_parent(''partman_table'',''record_time'',''time-static'',''monthly'', NULL, 1);
---
--- research --- begins
---
--- select to_timestamp(overlay(created placing '.' from 10 ):: double precision) from transactions limit 10 ;
--- unix epoch time stamp default in seconds
--- n26 uses unix epoch timestamp in milliseconds
SELECT to_timestamp(created::bigint / 1000) from transactions ;
--- group by year
SELECT date_part('year',to_timestamp(created::bigint / 1000)), count(1) from transactions group by 1 ;
--- group by month
SELECT date_part('year',to_timestamp(created::bigint / 1000)),
date_part('month',to_timestamp(created::bigint / 1000)),
count(2)
from transactions group by 1,2 ;
SELECT timestamp 'epoch' + created * interval '1 ms' from transactions ;
---
--- research --- begins
---