Commit 188c3e5b636e981cc3534c74bd27fdaaf6173fcd
Committed by
GitHub
1 parent
aabc22d7
Upgrade Sql Ts & Timescale improvements (#2495)
* psql & timescale ts upgrade improved * fix typo * fix typo 2 * removed tenant_id from timescale db schema & upgade scipt logic
Showing
21 changed files
with
259 additions
and
376 deletions
... | ... | @@ -14,33 +14,27 @@ |
14 | 14 | -- limitations under the License. |
15 | 15 | -- |
16 | 16 | |
17 | --- select check_version(); | |
17 | +-- call check_version(); | |
18 | 18 | |
19 | -CREATE OR REPLACE FUNCTION check_version() RETURNS boolean AS $$ | |
19 | +CREATE OR REPLACE PROCEDURE check_version(INOUT valid_version boolean) LANGUAGE plpgsql AS $BODY$ | |
20 | 20 | DECLARE |
21 | 21 | current_version integer; |
22 | - valid_version boolean; | |
23 | 22 | BEGIN |
24 | 23 | RAISE NOTICE 'Check the current installed PostgreSQL version...'; |
25 | 24 | SELECT current_setting('server_version_num') INTO current_version; |
26 | - IF current_version < 100000 THEN | |
27 | - valid_version := FALSE; | |
28 | - ELSE | |
29 | - valid_version := TRUE; | |
30 | - END IF; | |
31 | - IF valid_version = FALSE THEN | |
32 | - RAISE NOTICE 'Postgres version should be at least more than 10!'; | |
33 | - ELSE | |
25 | + IF current_version > 110000 THEN | |
34 | 26 | RAISE NOTICE 'PostgreSQL version is valid!'; |
35 | 27 | RAISE NOTICE 'Schema update started...'; |
28 | + SELECT true INTO valid_version; | |
29 | + ELSE | |
30 | + RAISE NOTICE 'Postgres version should be at least more than 10!'; | |
36 | 31 | END IF; |
37 | - RETURN valid_version; | |
38 | 32 | END; |
39 | -$$ LANGUAGE 'plpgsql'; | |
33 | +$BODY$; | |
40 | 34 | |
41 | --- select create_partition_ts_kv_table(); | |
35 | +-- call create_partition_ts_kv_table(); | |
42 | 36 | |
43 | -CREATE OR REPLACE FUNCTION create_partition_ts_kv_table() RETURNS VOID AS $$ | |
37 | +CREATE OR REPLACE PROCEDURE create_partition_ts_kv_table() LANGUAGE plpgsql AS $$ | |
44 | 38 | |
45 | 39 | BEGIN |
46 | 40 | ALTER TABLE ts_kv |
... | ... | @@ -57,11 +51,11 @@ BEGIN |
57 | 51 | ALTER TABLE ts_kv |
58 | 52 | ALTER COLUMN key TYPE integer USING key::integer; |
59 | 53 | END; |
60 | -$$ LANGUAGE 'plpgsql'; | |
54 | +$$; | |
61 | 55 | |
62 | --- select create_new_ts_kv_latest_table(); | |
56 | +-- call create_new_ts_kv_latest_table(); | |
63 | 57 | |
64 | -CREATE OR REPLACE FUNCTION create_new_ts_kv_latest_table() RETURNS VOID AS $$ | |
58 | +CREATE OR REPLACE PROCEDURE create_new_ts_kv_latest_table() LANGUAGE plpgsql AS $$ | |
65 | 59 | |
66 | 60 | BEGIN |
67 | 61 | ALTER TABLE ts_kv_latest |
... | ... | @@ -81,13 +75,13 @@ BEGIN |
81 | 75 | ALTER TABLE ts_kv_latest |
82 | 76 | ADD CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key); |
83 | 77 | END; |
84 | -$$ LANGUAGE 'plpgsql'; | |
78 | +$$; | |
85 | 79 | |
86 | 80 | |
87 | --- select create_partitions(); | |
81 | +-- call create_partitions(); | |
82 | + | |
83 | +CREATE OR REPLACE PROCEDURE create_partitions() LANGUAGE plpgsql AS $$ | |
88 | 84 | |
89 | -CREATE OR REPLACE FUNCTION create_partitions() RETURNS VOID AS | |
90 | -$$ | |
91 | 85 | DECLARE |
92 | 86 | partition_date varchar; |
93 | 87 | from_ts bigint; |
... | ... | @@ -111,11 +105,11 @@ BEGIN |
111 | 105 | |
112 | 106 | CLOSE key_cursor; |
113 | 107 | END; |
114 | -$$ language 'plpgsql'; | |
108 | +$$; | |
115 | 109 | |
116 | --- select create_ts_kv_dictionary_table(); | |
110 | +-- call create_ts_kv_dictionary_table(); | |
117 | 111 | |
118 | -CREATE OR REPLACE FUNCTION create_ts_kv_dictionary_table() RETURNS VOID AS $$ | |
112 | +CREATE OR REPLACE PROCEDURE create_ts_kv_dictionary_table() LANGUAGE plpgsql AS $$ | |
119 | 113 | |
120 | 114 | BEGIN |
121 | 115 | CREATE TABLE IF NOT EXISTS ts_kv_dictionary |
... | ... | @@ -125,12 +119,12 @@ BEGIN |
125 | 119 | CONSTRAINT ts_key_id_pkey PRIMARY KEY (key) |
126 | 120 | ); |
127 | 121 | END; |
128 | -$$ LANGUAGE 'plpgsql'; | |
122 | +$$; | |
123 | + | |
124 | +-- call insert_into_dictionary(); | |
129 | 125 | |
130 | --- select insert_into_dictionary(); | |
126 | +CREATE OR REPLACE PROCEDURE insert_into_dictionary() LANGUAGE plpgsql AS $$ | |
131 | 127 | |
132 | -CREATE OR REPLACE FUNCTION insert_into_dictionary() RETURNS VOID AS | |
133 | -$$ | |
134 | 128 | DECLARE |
135 | 129 | insert_record RECORD; |
136 | 130 | key_cursor CURSOR FOR SELECT DISTINCT key |
... | ... | @@ -150,28 +144,27 @@ BEGIN |
150 | 144 | END LOOP; |
151 | 145 | CLOSE key_cursor; |
152 | 146 | END; |
153 | -$$ language 'plpgsql'; | |
147 | +$$; | |
154 | 148 | |
155 | --- select insert_into_ts_kv(); | |
149 | +-- call insert_into_ts_kv(); | |
156 | 150 | |
157 | -CREATE OR REPLACE FUNCTION insert_into_ts_kv() RETURNS void AS | |
158 | -$$ | |
151 | +CREATE OR REPLACE PROCEDURE insert_into_ts_kv() LANGUAGE plpgsql AS $$ | |
159 | 152 | DECLARE |
160 | 153 | insert_size CONSTANT integer := 10000; |
161 | 154 | insert_counter integer DEFAULT 0; |
162 | 155 | insert_record RECORD; |
163 | - insert_cursor CURSOR FOR SELECT CONCAT(first_part_uuid, '-', second_part_uuid, '-1', third_part_uuid, '-', fourth_part_uuid, '-', fifth_part_uuid)::uuid AS entity_id, | |
156 | + insert_cursor CURSOR FOR SELECT CONCAT(entity_id_uuid_first_part, '-', entity_id_uuid_second_part, '-1', entity_id_uuid_third_part, '-', entity_id_uuid_fourth_part, '-', entity_id_uuid_fifth_part)::uuid AS entity_id, | |
164 | 157 | ts_kv_records.key AS key, |
165 | 158 | ts_kv_records.ts AS ts, |
166 | 159 | ts_kv_records.bool_v AS bool_v, |
167 | 160 | ts_kv_records.str_v AS str_v, |
168 | 161 | ts_kv_records.long_v AS long_v, |
169 | 162 | ts_kv_records.dbl_v AS dbl_v |
170 | - FROM (SELECT SUBSTRING(entity_id, 8, 8) AS first_part_uuid, | |
171 | - SUBSTRING(entity_id, 4, 4) AS second_part_uuid, | |
172 | - SUBSTRING(entity_id, 1, 3) AS third_part_uuid, | |
173 | - SUBSTRING(entity_id, 16, 4) AS fourth_part_uuid, | |
174 | - SUBSTRING(entity_id, 20) AS fifth_part_uuid, | |
163 | + FROM (SELECT SUBSTRING(entity_id, 8, 8) AS entity_id_uuid_first_part, | |
164 | + SUBSTRING(entity_id, 4, 4) AS entity_id_uuid_second_part, | |
165 | + SUBSTRING(entity_id, 1, 3) AS entity_id_uuid_third_part, | |
166 | + SUBSTRING(entity_id, 16, 4) AS entity_id_uuid_fourth_part, | |
167 | + SUBSTRING(entity_id, 20) AS entity_id_uuid_fifth_part, | |
175 | 168 | key_id AS key, |
176 | 169 | ts, |
177 | 170 | bool_v, |
... | ... | @@ -198,28 +191,27 @@ BEGIN |
198 | 191 | END LOOP; |
199 | 192 | CLOSE insert_cursor; |
200 | 193 | END; |
201 | -$$ LANGUAGE 'plpgsql'; | |
194 | +$$; | |
202 | 195 | |
203 | --- select insert_into_ts_kv_latest(); | |
196 | +-- call insert_into_ts_kv_latest(); | |
204 | 197 | |
205 | -CREATE OR REPLACE FUNCTION insert_into_ts_kv_latest() RETURNS void AS | |
206 | -$$ | |
198 | +CREATE OR REPLACE PROCEDURE insert_into_ts_kv_latest() LANGUAGE plpgsql AS $$ | |
207 | 199 | DECLARE |
208 | 200 | insert_size CONSTANT integer := 10000; |
209 | 201 | insert_counter integer DEFAULT 0; |
210 | 202 | insert_record RECORD; |
211 | - insert_cursor CURSOR FOR SELECT CONCAT(first_part_uuid, '-', second_part_uuid, '-1', third_part_uuid, '-', fourth_part_uuid, '-', fifth_part_uuid)::uuid AS entity_id, | |
203 | + insert_cursor CURSOR FOR SELECT CONCAT(entity_id_uuid_first_part, '-', entity_id_uuid_second_part, '-1', entity_id_uuid_third_part, '-', entity_id_uuid_fourth_part, '-', entity_id_uuid_fifth_part)::uuid AS entity_id, | |
212 | 204 | ts_kv_latest_records.key AS key, |
213 | 205 | ts_kv_latest_records.ts AS ts, |
214 | 206 | ts_kv_latest_records.bool_v AS bool_v, |
215 | 207 | ts_kv_latest_records.str_v AS str_v, |
216 | 208 | ts_kv_latest_records.long_v AS long_v, |
217 | 209 | ts_kv_latest_records.dbl_v AS dbl_v |
218 | - FROM (SELECT SUBSTRING(entity_id, 8, 8) AS first_part_uuid, | |
219 | - SUBSTRING(entity_id, 4, 4) AS second_part_uuid, | |
220 | - SUBSTRING(entity_id, 1, 3) AS third_part_uuid, | |
221 | - SUBSTRING(entity_id, 16, 4) AS fourth_part_uuid, | |
222 | - SUBSTRING(entity_id, 20) AS fifth_part_uuid, | |
210 | + FROM (SELECT SUBSTRING(entity_id, 8, 8) AS entity_id_uuid_first_part, | |
211 | + SUBSTRING(entity_id, 4, 4) AS entity_id_uuid_second_part, | |
212 | + SUBSTRING(entity_id, 1, 3) AS entity_id_uuid_third_part, | |
213 | + SUBSTRING(entity_id, 16, 4) AS entity_id_uuid_fourth_part, | |
214 | + SUBSTRING(entity_id, 20) AS entity_id_uuid_fifth_part, | |
223 | 215 | key_id AS key, |
224 | 216 | ts, |
225 | 217 | bool_v, |
... | ... | @@ -246,6 +238,6 @@ BEGIN |
246 | 238 | END LOOP; |
247 | 239 | CLOSE insert_cursor; |
248 | 240 | END; |
249 | -$$ LANGUAGE 'plpgsql'; | |
241 | +$$; | |
250 | 242 | |
251 | 243 | ... | ... |
... | ... | @@ -14,60 +14,51 @@ |
14 | 14 | -- limitations under the License. |
15 | 15 | -- |
16 | 16 | |
17 | --- select check_version(); | |
17 | +-- call check_version(); | |
18 | + | |
19 | +CREATE OR REPLACE PROCEDURE check_version(INOUT valid_version boolean) LANGUAGE plpgsql AS $BODY$ | |
18 | 20 | |
19 | -CREATE OR REPLACE FUNCTION check_version() RETURNS boolean AS $$ | |
20 | 21 | DECLARE |
21 | 22 | current_version integer; |
22 | - valid_version boolean; | |
23 | 23 | BEGIN |
24 | 24 | RAISE NOTICE 'Check the current installed PostgreSQL version...'; |
25 | 25 | SELECT current_setting('server_version_num') INTO current_version; |
26 | - IF current_version < 90600 THEN | |
27 | - valid_version := FALSE; | |
28 | - ELSE | |
29 | - valid_version := TRUE; | |
30 | - END IF; | |
31 | - IF valid_version = FALSE THEN | |
32 | - RAISE NOTICE 'Postgres version should be at least more than 9.6!'; | |
33 | - ELSE | |
26 | + IF current_version > 110000 THEN | |
34 | 27 | RAISE NOTICE 'PostgreSQL version is valid!'; |
35 | 28 | RAISE NOTICE 'Schema update started...'; |
29 | + SELECT true INTO valid_version; | |
30 | + ELSE | |
31 | + RAISE NOTICE 'Postgres version should be at least more than 10!'; | |
36 | 32 | END IF; |
37 | - RETURN valid_version; | |
38 | 33 | END; |
39 | -$$ LANGUAGE 'plpgsql'; | |
34 | +$BODY$; | |
40 | 35 | |
41 | --- select create_new_tenant_ts_kv_table(); | |
36 | +-- call create_new_ts_kv_table(); | |
42 | 37 | |
43 | -CREATE OR REPLACE FUNCTION create_new_tenant_ts_kv_table() RETURNS VOID AS $$ | |
38 | +CREATE OR REPLACE PROCEDURE create_new_ts_kv_table() LANGUAGE plpgsql AS $$ | |
44 | 39 | |
45 | 40 | BEGIN |
46 | 41 | ALTER TABLE tenant_ts_kv |
47 | 42 | RENAME TO tenant_ts_kv_old; |
48 | - CREATE TABLE IF NOT EXISTS tenant_ts_kv | |
43 | + CREATE TABLE IF NOT EXISTS ts_kv | |
49 | 44 | ( |
50 | 45 | LIKE tenant_ts_kv_old |
51 | 46 | ); |
52 | - ALTER TABLE tenant_ts_kv | |
53 | - ALTER COLUMN tenant_id TYPE uuid USING tenant_id::uuid; | |
54 | - ALTER TABLE tenant_ts_kv | |
55 | - ALTER COLUMN entity_id TYPE uuid USING entity_id::uuid; | |
56 | - ALTER TABLE tenant_ts_kv | |
57 | - ALTER COLUMN key TYPE integer USING key::integer; | |
58 | - ALTER TABLE tenant_ts_kv | |
59 | - ADD CONSTRAINT tenant_ts_kv_pkey PRIMARY KEY(tenant_id, entity_id, key, ts); | |
47 | + ALTER TABLE ts_kv ALTER COLUMN entity_id TYPE uuid USING entity_id::uuid; | |
48 | + ALTER TABLE ts_kv ALTER COLUMN key TYPE integer USING key::integer; | |
49 | + ALTER INDEX ts_kv_pkey RENAME TO tenant_ts_kv_pkey_old; | |
60 | 50 | ALTER INDEX idx_tenant_ts_kv RENAME TO idx_tenant_ts_kv_old; |
61 | 51 | ALTER INDEX tenant_ts_kv_ts_idx RENAME TO tenant_ts_kv_ts_idx_old; |
62 | --- PERFORM create_hypertable('tenant_ts_kv', 'ts', chunk_time_interval => 86400000, if_not_exists => true); | |
63 | - CREATE INDEX IF NOT EXISTS idx_tenant_ts_kv ON tenant_ts_kv(tenant_id, entity_id, key, ts); | |
52 | + ALTER TABLE ts_kv ADD CONSTRAINT ts_kv_pkey PRIMARY KEY(entity_id, key, ts); | |
53 | +-- CREATE INDEX IF NOT EXISTS ts_kv_ts_idx ON ts_kv(ts DESC); | |
54 | + ALTER TABLE ts_kv DROP COLUMN IF EXISTS tenant_id; | |
64 | 55 | END; |
65 | -$$ LANGUAGE 'plpgsql'; | |
56 | +$$; | |
66 | 57 | |
67 | 58 | |
68 | --- select create_ts_kv_latest_table(); | |
59 | +-- call create_ts_kv_latest_table(); | |
69 | 60 | |
70 | -CREATE OR REPLACE FUNCTION create_ts_kv_latest_table() RETURNS VOID AS $$ | |
61 | +CREATE OR REPLACE PROCEDURE create_ts_kv_latest_table() LANGUAGE plpgsql AS $$ | |
71 | 62 | |
72 | 63 | BEGIN |
73 | 64 | CREATE TABLE IF NOT EXISTS ts_kv_latest |
... | ... | @@ -82,12 +73,12 @@ BEGIN |
82 | 73 | CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key) |
83 | 74 | ); |
84 | 75 | END; |
85 | -$$ LANGUAGE 'plpgsql'; | |
76 | +$$; | |
86 | 77 | |
87 | 78 | |
88 | --- select create_ts_kv_dictionary_table(); | |
79 | +-- call create_ts_kv_dictionary_table(); | |
89 | 80 | |
90 | -CREATE OR REPLACE FUNCTION create_ts_kv_dictionary_table() RETURNS VOID AS $$ | |
81 | +CREATE OR REPLACE PROCEDURE create_ts_kv_dictionary_table() LANGUAGE plpgsql AS $$ | |
91 | 82 | |
92 | 83 | BEGIN |
93 | 84 | CREATE TABLE IF NOT EXISTS ts_kv_dictionary |
... | ... | @@ -97,12 +88,12 @@ BEGIN |
97 | 88 | CONSTRAINT ts_key_id_pkey PRIMARY KEY (key) |
98 | 89 | ); |
99 | 90 | END; |
100 | -$$ LANGUAGE 'plpgsql'; | |
91 | +$$; | |
101 | 92 | |
102 | --- select insert_into_dictionary(); | |
93 | +-- call insert_into_dictionary(); | |
94 | + | |
95 | +CREATE OR REPLACE PROCEDURE insert_into_dictionary() LANGUAGE plpgsql AS $$ | |
103 | 96 | |
104 | -CREATE OR REPLACE FUNCTION insert_into_dictionary() RETURNS VOID AS | |
105 | -$$ | |
106 | 97 | DECLARE |
107 | 98 | insert_record RECORD; |
108 | 99 | key_cursor CURSOR FOR SELECT DISTINCT key |
... | ... | @@ -122,34 +113,28 @@ BEGIN |
122 | 113 | END LOOP; |
123 | 114 | CLOSE key_cursor; |
124 | 115 | END; |
125 | -$$ language 'plpgsql'; | |
116 | +$$; | |
117 | + | |
118 | +-- call insert_into_ts_kv(); | |
126 | 119 | |
127 | --- select insert_into_tenant_ts_kv(); | |
120 | +CREATE OR REPLACE PROCEDURE insert_into_ts_kv() LANGUAGE plpgsql AS $$ | |
128 | 121 | |
129 | -CREATE OR REPLACE FUNCTION insert_into_tenant_ts_kv() RETURNS void AS | |
130 | -$$ | |
131 | 122 | DECLARE |
132 | 123 | insert_size CONSTANT integer := 10000; |
133 | 124 | insert_counter integer DEFAULT 0; |
134 | 125 | insert_record RECORD; |
135 | - insert_cursor CURSOR FOR SELECT CONCAT(tenant_id_first_part_uuid, '-', tenant_id_second_part_uuid, '-1', tenant_id_third_part_uuid, '-', tenant_id_fourth_part_uuid, '-', tenant_id_fifth_part_uuid)::uuid AS tenant_id, | |
136 | - CONCAT(entity_id_first_part_uuid, '-', entity_id_second_part_uuid, '-1', entity_id_third_part_uuid, '-', entity_id_fourth_part_uuid, '-', entity_id_fifth_part_uuid)::uuid AS entity_id, | |
137 | - tenant_ts_kv_records.key AS key, | |
138 | - tenant_ts_kv_records.ts AS ts, | |
139 | - tenant_ts_kv_records.bool_v AS bool_v, | |
140 | - tenant_ts_kv_records.str_v AS str_v, | |
141 | - tenant_ts_kv_records.long_v AS long_v, | |
142 | - tenant_ts_kv_records.dbl_v AS dbl_v | |
143 | - FROM (SELECT SUBSTRING(tenant_id, 8, 8) AS tenant_id_first_part_uuid, | |
144 | - SUBSTRING(tenant_id, 4, 4) AS tenant_id_second_part_uuid, | |
145 | - SUBSTRING(tenant_id, 1, 3) AS tenant_id_third_part_uuid, | |
146 | - SUBSTRING(tenant_id, 16, 4) AS tenant_id_fourth_part_uuid, | |
147 | - SUBSTRING(tenant_id, 20) AS tenant_id_fifth_part_uuid, | |
148 | - SUBSTRING(entity_id, 8, 8) AS entity_id_first_part_uuid, | |
149 | - SUBSTRING(entity_id, 4, 4) AS entity_id_second_part_uuid, | |
150 | - SUBSTRING(entity_id, 1, 3) AS entity_id_third_part_uuid, | |
151 | - SUBSTRING(entity_id, 16, 4) AS entity_id_fourth_part_uuid, | |
152 | - SUBSTRING(entity_id, 20) AS entity_id_fifth_part_uuid, | |
126 | + insert_cursor CURSOR FOR SELECT CONCAT(entity_id_uuid_first_part, '-', entity_id_uuid_second_part, '-1', entity_id_uuid_third_part, '-', entity_id_uuid_fourth_part, '-', entity_id_uuid_fifth_part)::uuid AS entity_id, | |
127 | + new_ts_kv_records.key AS key, | |
128 | + new_ts_kv_records.ts AS ts, | |
129 | + new_ts_kv_records.bool_v AS bool_v, | |
130 | + new_ts_kv_records.str_v AS str_v, | |
131 | + new_ts_kv_records.long_v AS long_v, | |
132 | + new_ts_kv_records.dbl_v AS dbl_v | |
133 | + FROM (SELECT SUBSTRING(entity_id, 8, 8) AS entity_id_uuid_first_part, | |
134 | + SUBSTRING(entity_id, 4, 4) AS entity_id_uuid_second_part, | |
135 | + SUBSTRING(entity_id, 1, 3) AS entity_id_uuid_third_part, | |
136 | + SUBSTRING(entity_id, 16, 4) AS entity_id_uuid_fourth_part, | |
137 | + SUBSTRING(entity_id, 20) AS entity_id_uuid_fifth_part, | |
153 | 138 | key_id AS key, |
154 | 139 | ts, |
155 | 140 | bool_v, |
... | ... | @@ -157,31 +142,31 @@ DECLARE |
157 | 142 | long_v, |
158 | 143 | dbl_v |
159 | 144 | FROM tenant_ts_kv_old |
160 | - INNER JOIN ts_kv_dictionary ON (tenant_ts_kv_old.key = ts_kv_dictionary.key)) AS tenant_ts_kv_records; | |
145 | + INNER JOIN ts_kv_dictionary ON (tenant_ts_kv_old.key = ts_kv_dictionary.key)) AS new_ts_kv_records; | |
161 | 146 | BEGIN |
162 | 147 | OPEN insert_cursor; |
163 | 148 | LOOP |
164 | 149 | insert_counter := insert_counter + 1; |
165 | 150 | FETCH insert_cursor INTO insert_record; |
166 | 151 | IF NOT FOUND THEN |
167 | - RAISE NOTICE '% records have been inserted into the new tenant_ts_kv table!',insert_counter - 1; | |
152 | + RAISE NOTICE '% records have been inserted into the new ts_kv table!',insert_counter - 1; | |
168 | 153 | EXIT; |
169 | 154 | END IF; |
170 | - INSERT INTO tenant_ts_kv(tenant_id, entity_id, key, ts, bool_v, str_v, long_v, dbl_v) | |
171 | - VALUES (insert_record.tenant_id, insert_record.entity_id, insert_record.key, insert_record.ts, insert_record.bool_v, insert_record.str_v, | |
155 | + INSERT INTO ts_kv(entity_id, key, ts, bool_v, str_v, long_v, dbl_v) | |
156 | + VALUES (insert_record.entity_id, insert_record.key, insert_record.ts, insert_record.bool_v, insert_record.str_v, | |
172 | 157 | insert_record.long_v, insert_record.dbl_v); |
173 | 158 | IF MOD(insert_counter, insert_size) = 0 THEN |
174 | - RAISE NOTICE '% records have been inserted into the new tenant_ts_kv table!',insert_counter; | |
159 | + RAISE NOTICE '% records have been inserted into the new ts_kv table!',insert_counter; | |
175 | 160 | END IF; |
176 | 161 | END LOOP; |
177 | 162 | CLOSE insert_cursor; |
178 | 163 | END; |
179 | -$$ LANGUAGE 'plpgsql'; | |
164 | +$$; | |
165 | + | |
166 | +-- call insert_into_ts_kv_latest(); | |
180 | 167 | |
181 | --- select insert_into_ts_kv_latest(); | |
168 | +CREATE OR REPLACE PROCEDURE insert_into_ts_kv_latest() LANGUAGE plpgsql AS $$ | |
182 | 169 | |
183 | -CREATE OR REPLACE FUNCTION insert_into_ts_kv_latest() RETURNS void AS | |
184 | -$$ | |
185 | 170 | DECLARE |
186 | 171 | insert_size CONSTANT integer := 10000; |
187 | 172 | insert_counter integer DEFAULT 0; |
... | ... | @@ -191,7 +176,7 @@ DECLARE |
191 | 176 | latest_records.key AS key, |
192 | 177 | latest_records.entity_id AS entity_id, |
193 | 178 | latest_records.ts AS ts |
194 | - FROM (SELECT DISTINCT key AS key, entity_id AS entity_id, MAX(ts) AS ts FROM tenant_ts_kv GROUP BY key, entity_id) AS latest_records; | |
179 | + FROM (SELECT DISTINCT key AS key, entity_id AS entity_id, MAX(ts) AS ts FROM ts_kv GROUP BY key, entity_id) AS latest_records; | |
195 | 180 | BEGIN |
196 | 181 | OPEN insert_cursor; |
197 | 182 | LOOP |
... | ... | @@ -201,7 +186,7 @@ BEGIN |
201 | 186 | RAISE NOTICE '% records have been inserted into the ts_kv_latest table!',insert_counter - 1; |
202 | 187 | EXIT; |
203 | 188 | END IF; |
204 | - SELECT entity_id AS entity_id, key AS key, ts AS ts, bool_v AS bool_v, str_v AS str_v, long_v AS long_v, dbl_v AS dbl_v INTO insert_record FROM tenant_ts_kv WHERE entity_id = latest_record.entity_id AND key = latest_record.key AND ts = latest_record.ts; | |
189 | + SELECT entity_id AS entity_id, key AS key, ts AS ts, bool_v AS bool_v, str_v AS str_v, long_v AS long_v, dbl_v AS dbl_v INTO insert_record FROM ts_kv WHERE entity_id = latest_record.entity_id AND key = latest_record.key AND ts = latest_record.ts; | |
205 | 190 | INSERT INTO ts_kv_latest(entity_id, key, ts, bool_v, str_v, long_v, dbl_v) |
206 | 191 | VALUES (insert_record.entity_id, insert_record.key, insert_record.ts, insert_record.bool_v, insert_record.str_v, insert_record.long_v, insert_record.dbl_v); |
207 | 192 | IF MOD(insert_counter, insert_size) = 0 THEN |
... | ... | @@ -210,4 +195,4 @@ BEGIN |
210 | 195 | END LOOP; |
211 | 196 | CLOSE insert_cursor; |
212 | 197 | END; |
213 | -$$ LANGUAGE 'plpgsql'; | |
198 | +$$; | ... | ... |
... | ... | @@ -22,38 +22,21 @@ import org.springframework.beans.factory.annotation.Value; |
22 | 22 | import java.nio.charset.StandardCharsets; |
23 | 23 | import java.nio.file.Files; |
24 | 24 | import java.nio.file.Path; |
25 | -import java.sql.CallableStatement; | |
26 | 25 | import java.sql.Connection; |
26 | +import java.sql.ResultSet; | |
27 | 27 | import java.sql.SQLException; |
28 | 28 | import java.sql.SQLWarning; |
29 | -import java.sql.Types; | |
29 | +import java.sql.Statement; | |
30 | 30 | |
31 | 31 | @Slf4j |
32 | 32 | public abstract class AbstractSqlTsDatabaseUpgradeService { |
33 | 33 | |
34 | 34 | protected static final String CALL_REGEX = "call "; |
35 | - protected static final String CHECK_VERSION = "check_version()"; | |
35 | + protected static final String CHECK_VERSION = "check_version(false)"; | |
36 | + protected static final String CHECK_VERSION_TO_DELETE = "check_version(INOUT valid_version boolean)"; | |
36 | 37 | protected static final String DROP_TABLE = "DROP TABLE "; |
37 | - protected static final String DROP_FUNCTION_IF_EXISTS = "DROP FUNCTION IF EXISTS "; | |
38 | - | |
39 | - private static final String CALL_CHECK_VERSION = CALL_REGEX + CHECK_VERSION; | |
40 | - | |
41 | - | |
42 | - private static final String FUNCTION = "function: {}"; | |
43 | - private static final String DROP_STATEMENT = "drop statement: {}"; | |
44 | - private static final String QUERY = "query: {}"; | |
45 | - private static final String SUCCESSFULLY_EXECUTED = "Successfully executed "; | |
46 | - private static final String FAILED_TO_EXECUTE = "Failed to execute "; | |
47 | - private static final String FAILED_DUE_TO = " due to: {}"; | |
48 | - | |
49 | - protected static final String SUCCESSFULLY_EXECUTED_FUNCTION = SUCCESSFULLY_EXECUTED + FUNCTION; | |
50 | - protected static final String FAILED_TO_EXECUTE_FUNCTION_DUE_TO = FAILED_TO_EXECUTE + FUNCTION + FAILED_DUE_TO; | |
51 | - | |
52 | - protected static final String SUCCESSFULLY_EXECUTED_DROP_STATEMENT = SUCCESSFULLY_EXECUTED + DROP_STATEMENT; | |
53 | - protected static final String FAILED_TO_EXECUTE_DROP_STATEMENT = FAILED_TO_EXECUTE + DROP_STATEMENT + FAILED_DUE_TO; | |
54 | - | |
55 | - protected static final String SUCCESSFULLY_EXECUTED_QUERY = SUCCESSFULLY_EXECUTED + QUERY; | |
56 | - protected static final String FAILED_TO_EXECUTE_QUERY = FAILED_TO_EXECUTE + QUERY + FAILED_DUE_TO; | |
38 | + protected static final String DROP_PROCEDURE_IF_EXISTS = "DROP PROCEDURE IF EXISTS "; | |
39 | + protected static final String DROP_PROCEDURE_CHECK_VERSION = DROP_PROCEDURE_IF_EXISTS + CHECK_VERSION_TO_DELETE; | |
57 | 40 | |
58 | 41 | @Value("${spring.datasource.url}") |
59 | 42 | protected String dbUrl; |
... | ... | @@ -78,23 +61,22 @@ public abstract class AbstractSqlTsDatabaseUpgradeService { |
78 | 61 | log.info("Check the current PostgreSQL version..."); |
79 | 62 | boolean versionValid = false; |
80 | 63 | try { |
81 | - CallableStatement callableStatement = conn.prepareCall("{? = " + CALL_CHECK_VERSION + " }"); | |
82 | - callableStatement.registerOutParameter(1, Types.BOOLEAN); | |
83 | - callableStatement.execute(); | |
84 | - versionValid = callableStatement.getBoolean(1); | |
85 | - callableStatement.close(); | |
64 | + Statement statement = conn.createStatement(); | |
65 | + ResultSet resultSet = statement.executeQuery(CALL_REGEX + CHECK_VERSION); | |
66 | + resultSet.next(); | |
67 | + versionValid = resultSet.getBoolean(1); | |
68 | + statement.close(); | |
86 | 69 | } catch (Exception e) { |
87 | 70 | log.info("Failed to check current PostgreSQL version due to: {}", e.getMessage()); |
88 | 71 | } |
89 | 72 | return versionValid; |
90 | 73 | } |
91 | 74 | |
92 | - protected void executeFunction(Connection conn, String query) { | |
93 | - log.info("{} ... ", query); | |
75 | + protected void executeQuery(Connection conn, String query) { | |
94 | 76 | try { |
95 | - CallableStatement callableStatement = conn.prepareCall("{" + query + "}"); | |
96 | - callableStatement.execute(); | |
97 | - SQLWarning warnings = callableStatement.getWarnings(); | |
77 | + Statement statement = conn.createStatement(); | |
78 | + statement.execute(query); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script | |
79 | + SQLWarning warnings = statement.getWarnings(); | |
98 | 80 | if (warnings != null) { |
99 | 81 | log.info("{}", warnings.getMessage()); |
100 | 82 | SQLWarning nextWarning = warnings.getNextWarning(); |
... | ... | @@ -103,31 +85,10 @@ public abstract class AbstractSqlTsDatabaseUpgradeService { |
103 | 85 | nextWarning = nextWarning.getNextWarning(); |
104 | 86 | } |
105 | 87 | } |
106 | - callableStatement.close(); | |
107 | - log.info(SUCCESSFULLY_EXECUTED_FUNCTION, query.replace(CALL_REGEX, "")); | |
108 | - Thread.sleep(2000); | |
109 | - } catch (Exception e) { | |
110 | - log.info(FAILED_TO_EXECUTE_FUNCTION_DUE_TO, query, e.getMessage()); | |
111 | - } | |
112 | - } | |
113 | - | |
114 | - protected void executeDropStatement(Connection conn, String query) { | |
115 | - try { | |
116 | - conn.createStatement().execute(query); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script | |
117 | - log.info(SUCCESSFULLY_EXECUTED_DROP_STATEMENT, query); | |
118 | - Thread.sleep(5000); | |
119 | - } catch (InterruptedException | SQLException e) { | |
120 | - log.info(FAILED_TO_EXECUTE_DROP_STATEMENT, query, e.getMessage()); | |
121 | - } | |
122 | - } | |
123 | - | |
124 | - protected void executeQuery(Connection conn, String query) { | |
125 | - try { | |
126 | - conn.createStatement().execute(query); //NOSONAR, ignoring because method used to execute thingsboard database upgrade script | |
127 | - log.info(SUCCESSFULLY_EXECUTED_QUERY, query); | |
128 | 88 | Thread.sleep(5000); |
89 | + log.info("Successfully executed query: {}", query); | |
129 | 90 | } catch (InterruptedException | SQLException e) { |
130 | - log.info(FAILED_TO_EXECUTE_QUERY, query, e.getMessage()); | |
91 | + log.info("Failed to execute query: {} due to: {}", query, e.getMessage()); | |
131 | 92 | } |
132 | 93 | } |
133 | 94 | ... | ... |
... | ... | @@ -57,14 +57,13 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
57 | 57 | private static final String DROP_TABLE_TS_KV_OLD = DROP_TABLE + TS_KV_OLD; |
58 | 58 | private static final String DROP_TABLE_TS_KV_LATEST_OLD = DROP_TABLE + TS_KV_LATEST_OLD; |
59 | 59 | |
60 | - private static final String DROP_FUNCTION_CHECK_VERSION = DROP_FUNCTION_IF_EXISTS + CHECK_VERSION; | |
61 | - private static final String DROP_FUNCTION_CREATE_PARTITION_TS_KV_TABLE = DROP_FUNCTION_IF_EXISTS + CREATE_PARTITION_TS_KV_TABLE; | |
62 | - private static final String DROP_FUNCTION_CREATE_NEW_TS_KV_LATEST_TABLE = DROP_FUNCTION_IF_EXISTS + CREATE_NEW_TS_KV_LATEST_TABLE; | |
63 | - private static final String DROP_FUNCTION_CREATE_PARTITIONS = DROP_FUNCTION_IF_EXISTS + CREATE_PARTITIONS; | |
64 | - private static final String DROP_FUNCTION_CREATE_TS_KV_DICTIONARY_TABLE = DROP_FUNCTION_IF_EXISTS + CREATE_TS_KV_DICTIONARY_TABLE; | |
65 | - private static final String DROP_FUNCTION_INSERT_INTO_DICTIONARY = DROP_FUNCTION_IF_EXISTS + INSERT_INTO_DICTIONARY; | |
66 | - private static final String DROP_FUNCTION_INSERT_INTO_TS_KV = DROP_FUNCTION_IF_EXISTS + INSERT_INTO_TS_KV; | |
67 | - private static final String DROP_FUNCTION_INSERT_INTO_TS_KV_LATEST = DROP_FUNCTION_IF_EXISTS + INSERT_INTO_TS_KV_LATEST; | |
60 | + private static final String DROP_PROCEDURE_CREATE_PARTITION_TS_KV_TABLE = DROP_PROCEDURE_IF_EXISTS + CREATE_PARTITION_TS_KV_TABLE; | |
61 | + private static final String DROP_PROCEDURE_CREATE_NEW_TS_KV_LATEST_TABLE = DROP_PROCEDURE_IF_EXISTS + CREATE_NEW_TS_KV_LATEST_TABLE; | |
62 | + private static final String DROP_PROCEDURE_CREATE_PARTITIONS = DROP_PROCEDURE_IF_EXISTS + CREATE_PARTITIONS; | |
63 | + private static final String DROP_PROCEDURE_CREATE_TS_KV_DICTIONARY_TABLE = DROP_PROCEDURE_IF_EXISTS + CREATE_TS_KV_DICTIONARY_TABLE; | |
64 | + private static final String DROP_PROCEDURE_INSERT_INTO_DICTIONARY = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_DICTIONARY; | |
65 | + private static final String DROP_PROCEDURE_INSERT_INTO_TS_KV = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_TS_KV; | |
66 | + private static final String DROP_PROCEDURE_INSERT_INTO_TS_KV_LATEST = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_TS_KV_LATEST; | |
68 | 67 | |
69 | 68 | @Override |
70 | 69 | public void upgradeDatabase(String fromVersion) throws Exception { |
... | ... | @@ -76,30 +75,30 @@ public class PsqlTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgradeSe |
76 | 75 | loadSql(conn); |
77 | 76 | boolean versionValid = checkVersion(conn); |
78 | 77 | if (!versionValid) { |
79 | - log.info("PostgreSQL version should be at least more than 10!"); | |
78 | + log.info("PostgreSQL version should be at least more than 11!"); | |
80 | 79 | log.info("Please upgrade your PostgreSQL and restart the script!"); |
81 | 80 | } else { |
82 | 81 | log.info("PostgreSQL version is valid!"); |
83 | 82 | log.info("Updating schema ..."); |
84 | - executeFunction(conn, CALL_CREATE_PARTITION_TS_KV_TABLE); | |
85 | - executeFunction(conn, CALL_CREATE_PARTITIONS); | |
86 | - executeFunction(conn, CALL_CREATE_TS_KV_DICTIONARY_TABLE); | |
87 | - executeFunction(conn, CALL_INSERT_INTO_DICTIONARY); | |
88 | - executeFunction(conn, CALL_INSERT_INTO_TS_KV); | |
89 | - executeFunction(conn, CALL_CREATE_NEW_TS_KV_LATEST_TABLE); | |
90 | - executeFunction(conn, CALL_INSERT_INTO_TS_KV_LATEST); | |
83 | + executeQuery(conn, CALL_CREATE_PARTITION_TS_KV_TABLE); | |
84 | + executeQuery(conn, CALL_CREATE_PARTITIONS); | |
85 | + executeQuery(conn, CALL_CREATE_TS_KV_DICTIONARY_TABLE); | |
86 | + executeQuery(conn, CALL_INSERT_INTO_DICTIONARY); | |
87 | + executeQuery(conn, CALL_INSERT_INTO_TS_KV); | |
88 | + executeQuery(conn, CALL_CREATE_NEW_TS_KV_LATEST_TABLE); | |
89 | + executeQuery(conn, CALL_INSERT_INTO_TS_KV_LATEST); | |
91 | 90 | |
92 | - executeDropStatement(conn, DROP_TABLE_TS_KV_OLD); | |
93 | - executeDropStatement(conn, DROP_TABLE_TS_KV_LATEST_OLD); | |
91 | + executeQuery(conn, DROP_TABLE_TS_KV_OLD); | |
92 | + executeQuery(conn, DROP_TABLE_TS_KV_LATEST_OLD); | |
94 | 93 | |
95 | - executeDropStatement(conn, DROP_FUNCTION_CHECK_VERSION); | |
96 | - executeDropStatement(conn, DROP_FUNCTION_CREATE_PARTITION_TS_KV_TABLE); | |
97 | - executeDropStatement(conn, DROP_FUNCTION_CREATE_PARTITIONS); | |
98 | - executeDropStatement(conn, DROP_FUNCTION_CREATE_TS_KV_DICTIONARY_TABLE); | |
99 | - executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_DICTIONARY); | |
100 | - executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_TS_KV); | |
101 | - executeDropStatement(conn, DROP_FUNCTION_CREATE_NEW_TS_KV_LATEST_TABLE); | |
102 | - executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_TS_KV_LATEST); | |
94 | + executeQuery(conn, DROP_PROCEDURE_CHECK_VERSION); | |
95 | + executeQuery(conn, DROP_PROCEDURE_CREATE_PARTITION_TS_KV_TABLE); | |
96 | + executeQuery(conn, DROP_PROCEDURE_CREATE_PARTITIONS); | |
97 | + executeQuery(conn, DROP_PROCEDURE_CREATE_TS_KV_DICTIONARY_TABLE); | |
98 | + executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_DICTIONARY); | |
99 | + executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TS_KV); | |
100 | + executeQuery(conn, DROP_PROCEDURE_CREATE_NEW_TS_KV_LATEST_TABLE); | |
101 | + executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TS_KV_LATEST); | |
103 | 102 | |
104 | 103 | executeQuery(conn, "ALTER TABLE ts_kv ADD COLUMN json_v json;"); |
105 | 104 | executeQuery(conn, "ALTER TABLE ts_kv_latest ADD COLUMN json_v json;"); | ... | ... |
... | ... | @@ -45,13 +45,13 @@ public class TimescaleTsDatabaseSchemaService extends SqlAbstractDatabaseSchemaS |
45 | 45 | private long chunkTimeInterval; |
46 | 46 | |
47 | 47 | public TimescaleTsDatabaseSchemaService() { |
48 | - super("schema-timescale.sql", "schema-timescale-idx.sql"); | |
48 | + super("schema-timescale.sql", null); | |
49 | 49 | } |
50 | 50 | |
51 | 51 | @Override |
52 | 52 | public void createDatabaseSchema() throws Exception { |
53 | 53 | super.createDatabaseSchema(); |
54 | - executeQuery("SELECT create_hypertable('tenant_ts_kv', 'ts', chunk_time_interval => " + chunkTimeInterval + ", if_not_exists => true);"); | |
54 | + executeQuery("SELECT create_hypertable('ts_kv', 'ts', chunk_time_interval => " + chunkTimeInterval + ", if_not_exists => true);"); | |
55 | 55 | } |
56 | 56 | |
57 | 57 | private void executeQuery(String query) { | ... | ... |
... | ... | @@ -43,27 +43,27 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr |
43 | 43 | private static final String TENANT_TS_KV_OLD_TABLE = "tenant_ts_kv_old;"; |
44 | 44 | |
45 | 45 | private static final String CREATE_TS_KV_LATEST_TABLE = "create_ts_kv_latest_table()"; |
46 | - private static final String CREATE_NEW_TENANT_TS_KV_TABLE = "create_new_tenant_ts_kv_table()"; | |
46 | + private static final String CREATE_NEW_TS_KV_TABLE = "create_new_ts_kv_table()"; | |
47 | 47 | private static final String CREATE_TS_KV_DICTIONARY_TABLE = "create_ts_kv_dictionary_table()"; |
48 | 48 | private static final String INSERT_INTO_DICTIONARY = "insert_into_dictionary()"; |
49 | - private static final String INSERT_INTO_TENANT_TS_KV = "insert_into_tenant_ts_kv()"; | |
49 | + private static final String INSERT_INTO_TS_KV = "insert_into_ts_kv()"; | |
50 | 50 | private static final String INSERT_INTO_TS_KV_LATEST = "insert_into_ts_kv_latest()"; |
51 | 51 | |
52 | 52 | private static final String CALL_CREATE_TS_KV_LATEST_TABLE = CALL_REGEX + CREATE_TS_KV_LATEST_TABLE; |
53 | - private static final String CALL_CREATE_NEW_TENANT_TS_KV_TABLE = CALL_REGEX + CREATE_NEW_TENANT_TS_KV_TABLE; | |
53 | + private static final String CALL_CREATE_NEW_TENANT_TS_KV_TABLE = CALL_REGEX + CREATE_NEW_TS_KV_TABLE; | |
54 | 54 | private static final String CALL_CREATE_TS_KV_DICTIONARY_TABLE = CALL_REGEX + CREATE_TS_KV_DICTIONARY_TABLE; |
55 | 55 | private static final String CALL_INSERT_INTO_DICTIONARY = CALL_REGEX + INSERT_INTO_DICTIONARY; |
56 | - private static final String CALL_INSERT_INTO_TS_KV = CALL_REGEX + INSERT_INTO_TENANT_TS_KV; | |
56 | + private static final String CALL_INSERT_INTO_TS_KV = CALL_REGEX + INSERT_INTO_TS_KV; | |
57 | 57 | private static final String CALL_INSERT_INTO_TS_KV_LATEST = CALL_REGEX + INSERT_INTO_TS_KV_LATEST; |
58 | 58 | |
59 | 59 | private static final String DROP_OLD_TENANT_TS_KV_TABLE = DROP_TABLE + TENANT_TS_KV_OLD_TABLE; |
60 | 60 | |
61 | - private static final String DROP_FUNCTION_CREATE_TS_KV_LATEST_TABLE = DROP_FUNCTION_IF_EXISTS + CREATE_TS_KV_LATEST_TABLE; | |
62 | - private static final String DROP_FUNCTION_CREATE_TENANT_TS_KV_TABLE_COPY = DROP_FUNCTION_IF_EXISTS + CREATE_NEW_TENANT_TS_KV_TABLE; | |
63 | - private static final String DROP_FUNCTION_CREATE_TS_KV_DICTIONARY_TABLE = DROP_FUNCTION_IF_EXISTS + CREATE_TS_KV_DICTIONARY_TABLE; | |
64 | - private static final String DROP_FUNCTION_INSERT_INTO_DICTIONARY = DROP_FUNCTION_IF_EXISTS + INSERT_INTO_DICTIONARY; | |
65 | - private static final String DROP_FUNCTION_INSERT_INTO_TENANT_TS_KV = DROP_FUNCTION_IF_EXISTS + INSERT_INTO_TENANT_TS_KV; | |
66 | - private static final String DROP_FUNCTION_INSERT_INTO_TS_KV_LATEST = DROP_FUNCTION_IF_EXISTS + INSERT_INTO_TS_KV_LATEST; | |
61 | + private static final String DROP_PROCEDURE_CREATE_TS_KV_LATEST_TABLE = DROP_PROCEDURE_IF_EXISTS + CREATE_TS_KV_LATEST_TABLE; | |
62 | + private static final String DROP_PROCEDURE_CREATE_TENANT_TS_KV_TABLE_COPY = DROP_PROCEDURE_IF_EXISTS + CREATE_NEW_TS_KV_TABLE; | |
63 | + private static final String DROP_PROCEDURE_CREATE_TS_KV_DICTIONARY_TABLE = DROP_PROCEDURE_IF_EXISTS + CREATE_TS_KV_DICTIONARY_TABLE; | |
64 | + private static final String DROP_PROCEDURE_INSERT_INTO_DICTIONARY = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_DICTIONARY; | |
65 | + private static final String DROP_PROCEDURE_INSERT_INTO_TENANT_TS_KV = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_TS_KV; | |
66 | + private static final String DROP_PROCEDURE_INSERT_INTO_TS_KV_LATEST = DROP_PROCEDURE_IF_EXISTS + INSERT_INTO_TS_KV_LATEST; | |
67 | 67 | |
68 | 68 | @Autowired |
69 | 69 | private InstallScripts installScripts; |
... | ... | @@ -78,33 +78,31 @@ public class TimescaleTsDatabaseUpgradeService extends AbstractSqlTsDatabaseUpgr |
78 | 78 | loadSql(conn); |
79 | 79 | boolean versionValid = checkVersion(conn); |
80 | 80 | if (!versionValid) { |
81 | - log.info("PostgreSQL version should be at least more than 9.6!"); | |
81 | + log.info("PostgreSQL version should be at least more than 11!"); | |
82 | 82 | log.info("Please upgrade your PostgreSQL and restart the script!"); |
83 | 83 | } else { |
84 | 84 | log.info("PostgreSQL version is valid!"); |
85 | 85 | log.info("Updating schema ..."); |
86 | - executeFunction(conn, CALL_CREATE_TS_KV_LATEST_TABLE); | |
87 | - executeFunction(conn, CALL_CREATE_NEW_TENANT_TS_KV_TABLE); | |
86 | + executeQuery(conn, CALL_CREATE_TS_KV_LATEST_TABLE); | |
87 | + executeQuery(conn, CALL_CREATE_NEW_TENANT_TS_KV_TABLE); | |
88 | 88 | |
89 | - executeQuery(conn, "SELECT create_hypertable('tenant_ts_kv', 'ts', chunk_time_interval => " + chunkTimeInterval + ", if_not_exists => true);"); | |
89 | + executeQuery(conn, "SELECT create_hypertable('ts_kv', 'ts', chunk_time_interval => " + chunkTimeInterval + ", if_not_exists => true);"); | |
90 | 90 | |
91 | - executeFunction(conn, CALL_CREATE_TS_KV_DICTIONARY_TABLE); | |
92 | - executeFunction(conn, CALL_INSERT_INTO_DICTIONARY); | |
93 | - executeFunction(conn, CALL_INSERT_INTO_TS_KV); | |
94 | - executeFunction(conn, CALL_INSERT_INTO_TS_KV_LATEST); | |
91 | + executeQuery(conn, CALL_CREATE_TS_KV_DICTIONARY_TABLE); | |
92 | + executeQuery(conn, CALL_INSERT_INTO_DICTIONARY); | |
93 | + executeQuery(conn, CALL_INSERT_INTO_TS_KV); | |
94 | + executeQuery(conn, CALL_INSERT_INTO_TS_KV_LATEST); | |
95 | 95 | |
96 | - //executeQuery(conn, "SELECT set_chunk_time_interval('tenant_ts_kv', " + chunkTimeInterval +");"); | |
96 | + executeQuery(conn, DROP_OLD_TENANT_TS_KV_TABLE); | |
97 | 97 | |
98 | - executeDropStatement(conn, DROP_OLD_TENANT_TS_KV_TABLE); | |
98 | + executeQuery(conn, DROP_PROCEDURE_CREATE_TS_KV_LATEST_TABLE); | |
99 | + executeQuery(conn, DROP_PROCEDURE_CREATE_TENANT_TS_KV_TABLE_COPY); | |
100 | + executeQuery(conn, DROP_PROCEDURE_CREATE_TS_KV_DICTIONARY_TABLE); | |
101 | + executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_DICTIONARY); | |
102 | + executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TENANT_TS_KV); | |
103 | + executeQuery(conn, DROP_PROCEDURE_INSERT_INTO_TS_KV_LATEST); | |
99 | 104 | |
100 | - executeDropStatement(conn, DROP_FUNCTION_CREATE_TS_KV_LATEST_TABLE); | |
101 | - executeDropStatement(conn, DROP_FUNCTION_CREATE_TENANT_TS_KV_TABLE_COPY); | |
102 | - executeDropStatement(conn, DROP_FUNCTION_CREATE_TS_KV_DICTIONARY_TABLE); | |
103 | - executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_DICTIONARY); | |
104 | - executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_TENANT_TS_KV); | |
105 | - executeDropStatement(conn, DROP_FUNCTION_INSERT_INTO_TS_KV_LATEST); | |
106 | - | |
107 | - executeQuery(conn, "ALTER TABLE tenant_ts_kv ADD COLUMN json_v json;"); | |
105 | + executeQuery(conn, "ALTER TABLE ts_kv ADD COLUMN json_v json;"); | |
108 | 106 | executeQuery(conn, "ALTER TABLE ts_kv_latest ADD COLUMN json_v json;"); |
109 | 107 | |
110 | 108 | log.info("schema timeseries updated!"); | ... | ... |
... | ... | @@ -36,6 +36,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.BOOLEAN_VALUE_COLU |
36 | 36 | import static org.thingsboard.server.dao.model.ModelConstants.DOUBLE_VALUE_COLUMN; |
37 | 37 | import static org.thingsboard.server.dao.model.ModelConstants.ENTITY_ID_COLUMN; |
38 | 38 | import static org.thingsboard.server.dao.model.ModelConstants.JSON_VALUE_COLUMN; |
39 | +import static org.thingsboard.server.dao.model.ModelConstants.KEY_COLUMN; | |
39 | 40 | import static org.thingsboard.server.dao.model.ModelConstants.LONG_VALUE_COLUMN; |
40 | 41 | import static org.thingsboard.server.dao.model.ModelConstants.STRING_VALUE_COLUMN; |
41 | 42 | import static org.thingsboard.server.dao.model.ModelConstants.TS_COLUMN; |
... | ... | @@ -54,6 +55,10 @@ public abstract class AbstractTsKvEntity implements ToData<TsKvEntry> { |
54 | 55 | protected UUID entityId; |
55 | 56 | |
56 | 57 | @Id |
58 | + @Column(name = KEY_COLUMN) | |
59 | + protected int key; | |
60 | + | |
61 | + @Id | |
57 | 62 | @Column(name = TS_COLUMN) |
58 | 63 | protected Long ts; |
59 | 64 | ... | ... |
... | ... | @@ -69,10 +69,6 @@ import static org.thingsboard.server.dao.model.ModelConstants.KEY_COLUMN; |
69 | 69 | }) |
70 | 70 | public final class TsKvLatestEntity extends AbstractTsKvEntity { |
71 | 71 | |
72 | - @Id | |
73 | - @Column(name = KEY_COLUMN) | |
74 | - private int key; | |
75 | - | |
76 | 72 | @Override |
77 | 73 | public boolean isNotEmpty() { |
78 | 74 | return strValue != null || longValue != null || doubleValue != null || booleanValue != null || jsonValue != null; | ... | ... |
... | ... | @@ -18,25 +18,18 @@ package org.thingsboard.server.dao.model.sqlts.timescale.ts; |
18 | 18 | import lombok.Data; |
19 | 19 | import lombok.EqualsAndHashCode; |
20 | 20 | import org.springframework.util.StringUtils; |
21 | -import org.thingsboard.server.common.data.kv.TsKvEntry; | |
22 | -import org.thingsboard.server.dao.model.ToData; | |
23 | 21 | import org.thingsboard.server.dao.model.sql.AbstractTsKvEntity; |
24 | 22 | |
25 | -import javax.persistence.Column; | |
26 | 23 | import javax.persistence.ColumnResult; |
27 | 24 | import javax.persistence.ConstructorResult; |
28 | 25 | import javax.persistence.Entity; |
29 | -import javax.persistence.Id; | |
30 | 26 | import javax.persistence.IdClass; |
31 | 27 | import javax.persistence.NamedNativeQueries; |
32 | 28 | import javax.persistence.NamedNativeQuery; |
33 | 29 | import javax.persistence.SqlResultSetMapping; |
34 | 30 | import javax.persistence.SqlResultSetMappings; |
35 | 31 | import javax.persistence.Table; |
36 | -import java.util.UUID; | |
37 | 32 | |
38 | -import static org.thingsboard.server.dao.model.ModelConstants.KEY_COLUMN; | |
39 | -import static org.thingsboard.server.dao.model.ModelConstants.TENANT_ID_COLUMN; | |
40 | 33 | import static org.thingsboard.server.dao.sqlts.timescale.AggregationRepository.FIND_AVG; |
41 | 34 | import static org.thingsboard.server.dao.sqlts.timescale.AggregationRepository.FIND_AVG_QUERY; |
42 | 35 | import static org.thingsboard.server.dao.sqlts.timescale.AggregationRepository.FIND_COUNT; |
... | ... | @@ -52,7 +45,7 @@ import static org.thingsboard.server.dao.sqlts.timescale.AggregationRepository.F |
52 | 45 | @Data |
53 | 46 | @EqualsAndHashCode(callSuper = true) |
54 | 47 | @Entity |
55 | -@Table(name = "tenant_ts_kv") | |
48 | +@Table(name = "ts_kv") | |
56 | 49 | @IdClass(TimescaleTsKvCompositeKey.class) |
57 | 50 | @SqlResultSetMappings({ |
58 | 51 | @SqlResultSetMapping( |
... | ... | @@ -116,15 +109,7 @@ import static org.thingsboard.server.dao.sqlts.timescale.AggregationRepository.F |
116 | 109 | resultSetMapping = "timescaleCountMapping" |
117 | 110 | ) |
118 | 111 | }) |
119 | -public final class TimescaleTsKvEntity extends AbstractTsKvEntity implements ToData<TsKvEntry> { | |
120 | - | |
121 | - @Id | |
122 | - @Column(name = TENANT_ID_COLUMN, columnDefinition = "uuid") | |
123 | - private UUID tenantId; | |
124 | - | |
125 | - @Id | |
126 | - @Column(name = KEY_COLUMN) | |
127 | - private int key; | |
112 | +public final class TimescaleTsKvEntity extends AbstractTsKvEntity { | |
128 | 113 | |
129 | 114 | public TimescaleTsKvEntity() { |
130 | 115 | } | ... | ... |
... | ... | @@ -32,10 +32,6 @@ import static org.thingsboard.server.dao.model.ModelConstants.KEY_COLUMN; |
32 | 32 | @IdClass(TsKvCompositeKey.class) |
33 | 33 | public final class TsKvEntity extends AbstractTsKvEntity { |
34 | 34 | |
35 | - @Id | |
36 | - @Column(name = KEY_COLUMN) | |
37 | - private int key; | |
38 | - | |
39 | 35 | public TsKvEntity() { |
40 | 36 | } |
41 | 37 | ... | ... |
... | ... | @@ -96,7 +96,7 @@ public abstract class AbstractChunkedAggregationTimeseriesDao extends AbstractSq |
96 | 96 | |
97 | 97 | @Override |
98 | 98 | public ListenableFuture<Void> removeLatest(TenantId tenantId, EntityId entityId, DeleteTsKvQuery query) { |
99 | - return getRemoveLatestFuture(tenantId, entityId, query); | |
99 | + return getRemoveLatestFuture(entityId, query); | |
100 | 100 | } |
101 | 101 | |
102 | 102 | @Override |
... | ... | @@ -125,9 +125,9 @@ public abstract class AbstractChunkedAggregationTimeseriesDao extends AbstractSq |
125 | 125 | } |
126 | 126 | |
127 | 127 | @Override |
128 | - protected ListenableFuture<List<TsKvEntry>> findAllAsync(TenantId tenantId, EntityId entityId, ReadTsKvQuery query) { | |
128 | + protected ListenableFuture<List<TsKvEntry>> findAllAsync(EntityId entityId, ReadTsKvQuery query) { | |
129 | 129 | if (query.getAggregation() == Aggregation.NONE) { |
130 | - return findAllAsyncWithLimit(tenantId, entityId, query); | |
130 | + return findAllAsyncWithLimit(entityId, query); | |
131 | 131 | } else { |
132 | 132 | long stepTs = query.getStartTs(); |
133 | 133 | List<ListenableFuture<Optional<TsKvEntry>>> futures = new ArrayList<>(); |
... | ... | @@ -135,7 +135,7 @@ public abstract class AbstractChunkedAggregationTimeseriesDao extends AbstractSq |
135 | 135 | long startTs = stepTs; |
136 | 136 | long endTs = stepTs + query.getInterval(); |
137 | 137 | long ts = startTs + (endTs - startTs) / 2; |
138 | - futures.add(findAndAggregateAsync(tenantId, entityId, query.getKey(), startTs, endTs, ts, query.getAggregation())); | |
138 | + futures.add(findAndAggregateAsync(entityId, query.getKey(), startTs, endTs, ts, query.getAggregation())); | |
139 | 139 | stepTs = endTs; |
140 | 140 | } |
141 | 141 | return getTskvEntriesFuture(Futures.allAsList(futures)); |
... | ... | @@ -143,7 +143,7 @@ public abstract class AbstractChunkedAggregationTimeseriesDao extends AbstractSq |
143 | 143 | } |
144 | 144 | |
145 | 145 | @Override |
146 | - protected ListenableFuture<List<TsKvEntry>> findAllAsyncWithLimit(TenantId tenantId, EntityId entityId, ReadTsKvQuery query) { | |
146 | + protected ListenableFuture<List<TsKvEntry>> findAllAsyncWithLimit(EntityId entityId, ReadTsKvQuery query) { | |
147 | 147 | Integer keyId = getOrSaveKeyId(query.getKey()); |
148 | 148 | List<TsKvEntity> tsKvEntities = tsKvRepository.findAllWithLimit( |
149 | 149 | entityId.getId(), |
... | ... | @@ -157,9 +157,9 @@ public abstract class AbstractChunkedAggregationTimeseriesDao extends AbstractSq |
157 | 157 | return Futures.immediateFuture(DaoUtil.convertDataList(tsKvEntities)); |
158 | 158 | } |
159 | 159 | |
160 | - protected ListenableFuture<Optional<TsKvEntry>> findAndAggregateAsync(TenantId tenantId, EntityId entityId, String key, long startTs, long endTs, long ts, Aggregation aggregation) { | |
160 | + private ListenableFuture<Optional<TsKvEntry>> findAndAggregateAsync(EntityId entityId, String key, long startTs, long endTs, long ts, Aggregation aggregation) { | |
161 | 161 | List<CompletableFuture<TsKvEntity>> entitiesFutures = new ArrayList<>(); |
162 | - switchAggregation(tenantId, entityId, key, startTs, endTs, aggregation, entitiesFutures); | |
162 | + switchAggregation(entityId, key, startTs, endTs, aggregation, entitiesFutures); | |
163 | 163 | return Futures.transform(setFutures(entitiesFutures), entity -> { |
164 | 164 | if (entity != null && entity.isNotEmpty()) { |
165 | 165 | entity.setEntityId(entityId.getId()); |
... | ... | @@ -172,29 +172,29 @@ public abstract class AbstractChunkedAggregationTimeseriesDao extends AbstractSq |
172 | 172 | }, MoreExecutors.directExecutor()); |
173 | 173 | } |
174 | 174 | |
175 | - protected void switchAggregation(TenantId tenantId, EntityId entityId, String key, long startTs, long endTs, Aggregation aggregation, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
175 | + protected void switchAggregation(EntityId entityId, String key, long startTs, long endTs, Aggregation aggregation, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
176 | 176 | switch (aggregation) { |
177 | 177 | case AVG: |
178 | - findAvg(tenantId, entityId, key, startTs, endTs, entitiesFutures); | |
178 | + findAvg(entityId, key, startTs, endTs, entitiesFutures); | |
179 | 179 | break; |
180 | 180 | case MAX: |
181 | - findMax(tenantId, entityId, key, startTs, endTs, entitiesFutures); | |
181 | + findMax(entityId, key, startTs, endTs, entitiesFutures); | |
182 | 182 | break; |
183 | 183 | case MIN: |
184 | - findMin(tenantId, entityId, key, startTs, endTs, entitiesFutures); | |
184 | + findMin(entityId, key, startTs, endTs, entitiesFutures); | |
185 | 185 | break; |
186 | 186 | case SUM: |
187 | - findSum(tenantId, entityId, key, startTs, endTs, entitiesFutures); | |
187 | + findSum(entityId, key, startTs, endTs, entitiesFutures); | |
188 | 188 | break; |
189 | 189 | case COUNT: |
190 | - findCount(tenantId, entityId, key, startTs, endTs, entitiesFutures); | |
190 | + findCount(entityId, key, startTs, endTs, entitiesFutures); | |
191 | 191 | break; |
192 | 192 | default: |
193 | 193 | throw new IllegalArgumentException("Not supported aggregation type: " + aggregation); |
194 | 194 | } |
195 | 195 | } |
196 | 196 | |
197 | - protected void findCount(TenantId tenantId, EntityId entityId, String key, long startTs, long endTs, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
197 | + protected void findCount(EntityId entityId, String key, long startTs, long endTs, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
198 | 198 | Integer keyId = getOrSaveKeyId(key); |
199 | 199 | entitiesFutures.add(tsKvRepository.findCount( |
200 | 200 | entityId.getId(), |
... | ... | @@ -203,7 +203,7 @@ public abstract class AbstractChunkedAggregationTimeseriesDao extends AbstractSq |
203 | 203 | endTs)); |
204 | 204 | } |
205 | 205 | |
206 | - protected void findSum(TenantId tenantId, EntityId entityId, String key, long startTs, long endTs, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
206 | + protected void findSum(EntityId entityId, String key, long startTs, long endTs, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
207 | 207 | Integer keyId = getOrSaveKeyId(key); |
208 | 208 | entitiesFutures.add(tsKvRepository.findSum( |
209 | 209 | entityId.getId(), |
... | ... | @@ -212,7 +212,7 @@ public abstract class AbstractChunkedAggregationTimeseriesDao extends AbstractSq |
212 | 212 | endTs)); |
213 | 213 | } |
214 | 214 | |
215 | - protected void findMin(TenantId tenantId, EntityId entityId, String key, long startTs, long endTs, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
215 | + protected void findMin(EntityId entityId, String key, long startTs, long endTs, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
216 | 216 | Integer keyId = getOrSaveKeyId(key); |
217 | 217 | entitiesFutures.add(tsKvRepository.findStringMin( |
218 | 218 | entityId.getId(), |
... | ... | @@ -226,7 +226,7 @@ public abstract class AbstractChunkedAggregationTimeseriesDao extends AbstractSq |
226 | 226 | endTs)); |
227 | 227 | } |
228 | 228 | |
229 | - protected void findMax(TenantId tenantId, EntityId entityId, String key, long startTs, long endTs, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
229 | + protected void findMax(EntityId entityId, String key, long startTs, long endTs, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
230 | 230 | Integer keyId = getOrSaveKeyId(key); |
231 | 231 | entitiesFutures.add(tsKvRepository.findStringMax( |
232 | 232 | entityId.getId(), |
... | ... | @@ -240,7 +240,7 @@ public abstract class AbstractChunkedAggregationTimeseriesDao extends AbstractSq |
240 | 240 | endTs)); |
241 | 241 | } |
242 | 242 | |
243 | - protected void findAvg(TenantId tenantId, EntityId entityId, String key, long startTs, long endTs, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
243 | + protected void findAvg(EntityId entityId, String key, long startTs, long endTs, List<CompletableFuture<TsKvEntity>> entitiesFutures) { | |
244 | 244 | Integer keyId = getOrSaveKeyId(key); |
245 | 245 | entitiesFutures.add(tsKvRepository.findAvg( |
246 | 246 | entityId.getId(), | ... | ... |
... | ... | @@ -127,7 +127,7 @@ public abstract class AbstractSqlTimeseriesDao extends JpaAbstractDaoListeningEx |
127 | 127 | protected ListenableFuture<List<TsKvEntry>> processFindAllAsync(TenantId tenantId, EntityId entityId, List<ReadTsKvQuery> queries) { |
128 | 128 | List<ListenableFuture<List<TsKvEntry>>> futures = queries |
129 | 129 | .stream() |
130 | - .map(query -> findAllAsync(tenantId, entityId, query)) | |
130 | + .map(query -> findAllAsync(entityId, query)) | |
131 | 131 | .collect(Collectors.toList()); |
132 | 132 | return Futures.transform(Futures.allAsList(futures), new Function<List<List<TsKvEntry>>, List<TsKvEntry>>() { |
133 | 133 | @Nullable |
... | ... | @@ -144,9 +144,9 @@ public abstract class AbstractSqlTimeseriesDao extends JpaAbstractDaoListeningEx |
144 | 144 | }, service); |
145 | 145 | } |
146 | 146 | |
147 | - protected abstract ListenableFuture<List<TsKvEntry>> findAllAsync(TenantId tenantId, EntityId entityId, ReadTsKvQuery query); | |
147 | + protected abstract ListenableFuture<List<TsKvEntry>> findAllAsync(EntityId entityId, ReadTsKvQuery query); | |
148 | 148 | |
149 | - protected abstract ListenableFuture<List<TsKvEntry>> findAllAsyncWithLimit(TenantId tenantId, EntityId entityId, ReadTsKvQuery query); | |
149 | + protected abstract ListenableFuture<List<TsKvEntry>> findAllAsyncWithLimit(EntityId entityId, ReadTsKvQuery query); | |
150 | 150 | |
151 | 151 | protected ListenableFuture<List<TsKvEntry>> getTskvEntriesFuture(ListenableFuture<List<Optional<TsKvEntry>>> future) { |
152 | 152 | return Futures.transform(future, new Function<List<Optional<TsKvEntry>>, List<TsKvEntry>>() { |
... | ... | @@ -164,12 +164,12 @@ public abstract class AbstractSqlTimeseriesDao extends JpaAbstractDaoListeningEx |
164 | 164 | }, service); |
165 | 165 | } |
166 | 166 | |
167 | - protected ListenableFuture<List<TsKvEntry>> findNewLatestEntryFuture(TenantId tenantId, EntityId entityId, DeleteTsKvQuery query) { | |
167 | + protected ListenableFuture<List<TsKvEntry>> findNewLatestEntryFuture(EntityId entityId, DeleteTsKvQuery query) { | |
168 | 168 | long startTs = 0; |
169 | 169 | long endTs = query.getStartTs() - 1; |
170 | 170 | ReadTsKvQuery findNewLatestQuery = new BaseReadTsKvQuery(query.getKey(), startTs, endTs, endTs - startTs, 1, |
171 | 171 | Aggregation.NONE, DESC_ORDER); |
172 | - return findAllAsync(tenantId, entityId, findNewLatestQuery); | |
172 | + return findAllAsync(entityId, findNewLatestQuery); | |
173 | 173 | } |
174 | 174 | |
175 | 175 | protected ListenableFuture<TsKvEntry> getFindLatestFuture(EntityId entityId, String key) { |
... | ... | @@ -189,7 +189,7 @@ public abstract class AbstractSqlTimeseriesDao extends JpaAbstractDaoListeningEx |
189 | 189 | return Futures.immediateFuture(result); |
190 | 190 | } |
191 | 191 | |
192 | - protected ListenableFuture<Void> getRemoveLatestFuture(TenantId tenantId, EntityId entityId, DeleteTsKvQuery query) { | |
192 | + protected ListenableFuture<Void> getRemoveLatestFuture(EntityId entityId, DeleteTsKvQuery query) { | |
193 | 193 | ListenableFuture<TsKvEntry> latestFuture = getFindLatestFuture(entityId, query.getKey()); |
194 | 194 | |
195 | 195 | ListenableFuture<Boolean> booleanFuture = Futures.transform(latestFuture, tsKvEntry -> { |
... | ... | @@ -217,7 +217,7 @@ public abstract class AbstractSqlTimeseriesDao extends JpaAbstractDaoListeningEx |
217 | 217 | if (query.getRewriteLatestIfDeleted()) { |
218 | 218 | ListenableFuture<Void> savedLatestFuture = Futures.transformAsync(booleanFuture, isRemove -> { |
219 | 219 | if (isRemove) { |
220 | - return getNewLatestEntryFuture(tenantId, entityId, query); | |
220 | + return getNewLatestEntryFuture(entityId, query); | |
221 | 221 | } |
222 | 222 | return Futures.immediateFuture(null); |
223 | 223 | }, service); |
... | ... | @@ -296,8 +296,8 @@ public abstract class AbstractSqlTimeseriesDao extends JpaAbstractDaoListeningEx |
296 | 296 | return keyId; |
297 | 297 | } |
298 | 298 | |
299 | - private ListenableFuture<Void> getNewLatestEntryFuture(TenantId tenantId, EntityId entityId, DeleteTsKvQuery query) { | |
300 | - ListenableFuture<List<TsKvEntry>> future = findNewLatestEntryFuture(tenantId, entityId, query); | |
299 | + private ListenableFuture<Void> getNewLatestEntryFuture(EntityId entityId, DeleteTsKvQuery query) { | |
300 | + ListenableFuture<List<TsKvEntry>> future = findNewLatestEntryFuture(entityId, query); | |
301 | 301 | return Futures.transformAsync(future, entryList -> { |
302 | 302 | if (entryList.size() == 1) { |
303 | 303 | return getSaveLatestFuture(entityId, entryList.get(0)); | ... | ... |
dao/src/main/java/org/thingsboard/server/dao/sqlts/insert/timescale/TimescaleInsertTsRepository.java
... | ... | @@ -37,8 +37,8 @@ import java.util.List; |
37 | 37 | public class TimescaleInsertTsRepository extends AbstractInsertRepository implements InsertTsRepository<TimescaleTsKvEntity> { |
38 | 38 | |
39 | 39 | private static final String INSERT_OR_UPDATE = |
40 | - "INSERT INTO tenant_ts_kv (tenant_id, entity_id, key, ts, bool_v, str_v, long_v, dbl_v, json_v) VALUES(?, ?, ?, ?, ?, ?, ?, ?, cast(? AS json)) " + | |
41 | - "ON CONFLICT (tenant_id, entity_id, key, ts) DO UPDATE SET bool_v = ?, str_v = ?, long_v = ?, dbl_v = ?, json_v = cast(? AS json);"; | |
40 | + "INSERT INTO ts_kv (entity_id, key, ts, bool_v, str_v, long_v, dbl_v, json_v) VALUES(?, ?, ?, ?, ?, ?, ?, cast(? AS json)) " + | |
41 | + "ON CONFLICT (entity_id, key, ts) DO UPDATE SET bool_v = ?, str_v = ?, long_v = ?, dbl_v = ?, json_v = cast(? AS json);"; | |
42 | 42 | |
43 | 43 | @Override |
44 | 44 | public void saveOrUpdate(List<EntityContainer<TimescaleTsKvEntity>> entities) { |
... | ... | @@ -46,41 +46,40 @@ public class TimescaleInsertTsRepository extends AbstractInsertRepository implem |
46 | 46 | @Override |
47 | 47 | public void setValues(PreparedStatement ps, int i) throws SQLException { |
48 | 48 | TimescaleTsKvEntity tsKvEntity = entities.get(i).getEntity(); |
49 | - ps.setObject(1, tsKvEntity.getTenantId()); | |
50 | - ps.setObject(2, tsKvEntity.getEntityId()); | |
51 | - ps.setInt(3, tsKvEntity.getKey()); | |
52 | - ps.setLong(4, tsKvEntity.getTs()); | |
49 | + ps.setObject(1, tsKvEntity.getEntityId()); | |
50 | + ps.setInt(2, tsKvEntity.getKey()); | |
51 | + ps.setLong(3, tsKvEntity.getTs()); | |
53 | 52 | |
54 | 53 | if (tsKvEntity.getBooleanValue() != null) { |
55 | - ps.setBoolean(5, tsKvEntity.getBooleanValue()); | |
56 | - ps.setBoolean(10, tsKvEntity.getBooleanValue()); | |
54 | + ps.setBoolean(4, tsKvEntity.getBooleanValue()); | |
55 | + ps.setBoolean(9, tsKvEntity.getBooleanValue()); | |
57 | 56 | } else { |
58 | - ps.setNull(5, Types.BOOLEAN); | |
59 | - ps.setNull(10, Types.BOOLEAN); | |
57 | + ps.setNull(4, Types.BOOLEAN); | |
58 | + ps.setNull(9, Types.BOOLEAN); | |
60 | 59 | } |
61 | 60 | |
62 | - ps.setString(6, replaceNullChars(tsKvEntity.getStrValue())); | |
63 | - ps.setString(11, replaceNullChars(tsKvEntity.getStrValue())); | |
61 | + ps.setString(5, replaceNullChars(tsKvEntity.getStrValue())); | |
62 | + ps.setString(10, replaceNullChars(tsKvEntity.getStrValue())); | |
64 | 63 | |
65 | 64 | |
66 | 65 | if (tsKvEntity.getLongValue() != null) { |
67 | - ps.setLong(7, tsKvEntity.getLongValue()); | |
68 | - ps.setLong(12, tsKvEntity.getLongValue()); | |
66 | + ps.setLong(6, tsKvEntity.getLongValue()); | |
67 | + ps.setLong(11, tsKvEntity.getLongValue()); | |
69 | 68 | } else { |
70 | - ps.setNull(7, Types.BIGINT); | |
71 | - ps.setNull(12, Types.BIGINT); | |
69 | + ps.setNull(6, Types.BIGINT); | |
70 | + ps.setNull(11, Types.BIGINT); | |
72 | 71 | } |
73 | 72 | |
74 | 73 | if (tsKvEntity.getDoubleValue() != null) { |
75 | - ps.setDouble(8, tsKvEntity.getDoubleValue()); | |
76 | - ps.setDouble(13, tsKvEntity.getDoubleValue()); | |
74 | + ps.setDouble(7, tsKvEntity.getDoubleValue()); | |
75 | + ps.setDouble(12, tsKvEntity.getDoubleValue()); | |
77 | 76 | } else { |
78 | - ps.setNull(8, Types.DOUBLE); | |
79 | - ps.setNull(13, Types.DOUBLE); | |
77 | + ps.setNull(7, Types.DOUBLE); | |
78 | + ps.setNull(12, Types.DOUBLE); | |
80 | 79 | } |
81 | 80 | |
82 | - ps.setString(9, replaceNullChars(tsKvEntity.getJsonValue())); | |
83 | - ps.setString(14, replaceNullChars(tsKvEntity.getJsonValue())); | |
81 | + ps.setString(8, replaceNullChars(tsKvEntity.getJsonValue())); | |
82 | + ps.setString(13, replaceNullChars(tsKvEntity.getJsonValue())); | |
84 | 83 | } |
85 | 84 | |
86 | 85 | @Override | ... | ... |
... | ... | @@ -36,7 +36,7 @@ public class AggregationRepository { |
36 | 36 | public static final String FIND_SUM = "findSum"; |
37 | 37 | public static final String FIND_COUNT = "findCount"; |
38 | 38 | |
39 | - public static final String FROM_WHERE_CLAUSE = "FROM tenant_ts_kv tskv WHERE tskv.tenant_id = cast(:tenantId AS uuid) AND tskv.entity_id = cast(:entityId AS uuid) AND tskv.key= cast(:entityKey AS int) AND tskv.ts > :startTs AND tskv.ts <= :endTs GROUP BY tskv.tenant_id, tskv.entity_id, tskv.key, tsBucket ORDER BY tskv.tenant_id, tskv.entity_id, tskv.key, tsBucket"; | |
39 | + public static final String FROM_WHERE_CLAUSE = "FROM ts_kv tskv WHERE tskv.entity_id = cast(:entityId AS uuid) AND tskv.key= cast(:entityKey AS int) AND tskv.ts > :startTs AND tskv.ts <= :endTs GROUP BY tskv.entity_id, tskv.key, tsBucket ORDER BY tskv.entity_id, tskv.key, tsBucket"; | |
40 | 40 | |
41 | 41 | public static final String FIND_AVG_QUERY = "SELECT time_bucket(:timeBucket, tskv.ts) AS tsBucket, :timeBucket AS interval, SUM(COALESCE(tskv.long_v, 0)) AS longValue, SUM(COALESCE(tskv.dbl_v, 0.0)) AS doubleValue, SUM(CASE WHEN tskv.long_v IS NULL THEN 0 ELSE 1 END) AS longCountValue, SUM(CASE WHEN tskv.dbl_v IS NULL THEN 0 ELSE 1 END) AS doubleCountValue, null AS strValue, 'AVG' AS aggType "; |
42 | 42 | |
... | ... | @@ -52,43 +52,42 @@ public class AggregationRepository { |
52 | 52 | private EntityManager entityManager; |
53 | 53 | |
54 | 54 | @Async |
55 | - public CompletableFuture<List<TimescaleTsKvEntity>> findAvg(UUID tenantId, UUID entityId, int entityKey, long timeBucket, long startTs, long endTs) { | |
55 | + public CompletableFuture<List<TimescaleTsKvEntity>> findAvg(UUID entityId, int entityKey, long timeBucket, long startTs, long endTs) { | |
56 | 56 | @SuppressWarnings("unchecked") |
57 | - List<TimescaleTsKvEntity> resultList = getResultList(tenantId, entityId, entityKey, timeBucket, startTs, endTs, FIND_AVG); | |
57 | + List<TimescaleTsKvEntity> resultList = getResultList(entityId, entityKey, timeBucket, startTs, endTs, FIND_AVG); | |
58 | 58 | return CompletableFuture.supplyAsync(() -> resultList); |
59 | 59 | } |
60 | 60 | |
61 | 61 | @Async |
62 | - public CompletableFuture<List<TimescaleTsKvEntity>> findMax(UUID tenantId, UUID entityId, int entityKey, long timeBucket, long startTs, long endTs) { | |
62 | + public CompletableFuture<List<TimescaleTsKvEntity>> findMax(UUID entityId, int entityKey, long timeBucket, long startTs, long endTs) { | |
63 | 63 | @SuppressWarnings("unchecked") |
64 | - List<TimescaleTsKvEntity> resultList = getResultList(tenantId, entityId, entityKey, timeBucket, startTs, endTs, FIND_MAX); | |
64 | + List<TimescaleTsKvEntity> resultList = getResultList(entityId, entityKey, timeBucket, startTs, endTs, FIND_MAX); | |
65 | 65 | return CompletableFuture.supplyAsync(() -> resultList); |
66 | 66 | } |
67 | 67 | |
68 | 68 | @Async |
69 | - public CompletableFuture<List<TimescaleTsKvEntity>> findMin(UUID tenantId, UUID entityId, int entityKey, long timeBucket, long startTs, long endTs) { | |
69 | + public CompletableFuture<List<TimescaleTsKvEntity>> findMin(UUID entityId, int entityKey, long timeBucket, long startTs, long endTs) { | |
70 | 70 | @SuppressWarnings("unchecked") |
71 | - List<TimescaleTsKvEntity> resultList = getResultList(tenantId, entityId, entityKey, timeBucket, startTs, endTs, FIND_MIN); | |
71 | + List<TimescaleTsKvEntity> resultList = getResultList(entityId, entityKey, timeBucket, startTs, endTs, FIND_MIN); | |
72 | 72 | return CompletableFuture.supplyAsync(() -> resultList); |
73 | 73 | } |
74 | 74 | |
75 | 75 | @Async |
76 | - public CompletableFuture<List<TimescaleTsKvEntity>> findSum(UUID tenantId, UUID entityId, int entityKey, long timeBucket, long startTs, long endTs) { | |
76 | + public CompletableFuture<List<TimescaleTsKvEntity>> findSum(UUID entityId, int entityKey, long timeBucket, long startTs, long endTs) { | |
77 | 77 | @SuppressWarnings("unchecked") |
78 | - List<TimescaleTsKvEntity> resultList = getResultList(tenantId, entityId, entityKey, timeBucket, startTs, endTs, FIND_SUM); | |
78 | + List<TimescaleTsKvEntity> resultList = getResultList(entityId, entityKey, timeBucket, startTs, endTs, FIND_SUM); | |
79 | 79 | return CompletableFuture.supplyAsync(() -> resultList); |
80 | 80 | } |
81 | 81 | |
82 | 82 | @Async |
83 | - public CompletableFuture<List<TimescaleTsKvEntity>> findCount(UUID tenantId, UUID entityId, int entityKey, long timeBucket, long startTs, long endTs) { | |
83 | + public CompletableFuture<List<TimescaleTsKvEntity>> findCount(UUID entityId, int entityKey, long timeBucket, long startTs, long endTs) { | |
84 | 84 | @SuppressWarnings("unchecked") |
85 | - List<TimescaleTsKvEntity> resultList = getResultList(tenantId, entityId, entityKey, timeBucket, startTs, endTs, FIND_COUNT); | |
85 | + List<TimescaleTsKvEntity> resultList = getResultList(entityId, entityKey, timeBucket, startTs, endTs, FIND_COUNT); | |
86 | 86 | return CompletableFuture.supplyAsync(() -> resultList); |
87 | 87 | } |
88 | 88 | |
89 | - private List getResultList(UUID tenantId, UUID entityId, int entityKey, long timeBucket, long startTs, long endTs, String query) { | |
89 | + private List getResultList(UUID entityId, int entityKey, long timeBucket, long startTs, long endTs, String query) { | |
90 | 90 | return entityManager.createNamedQuery(query) |
91 | - .setParameter("tenantId", tenantId) | |
92 | 91 | .setParameter("entityId", entityId) |
93 | 92 | .setParameter("entityKey", entityKey) |
94 | 93 | .setParameter("timeBucket", timeBucket) | ... | ... |
... | ... | @@ -88,24 +88,23 @@ public class TimescaleTimeseriesDao extends AbstractSqlTimeseriesDao implements |
88 | 88 | } |
89 | 89 | |
90 | 90 | @Override |
91 | - protected ListenableFuture<List<TsKvEntry>> findAllAsync(TenantId tenantId, EntityId entityId, ReadTsKvQuery query) { | |
91 | + protected ListenableFuture<List<TsKvEntry>> findAllAsync(EntityId entityId, ReadTsKvQuery query) { | |
92 | 92 | if (query.getAggregation() == Aggregation.NONE) { |
93 | - return findAllAsyncWithLimit(tenantId, entityId, query); | |
93 | + return findAllAsyncWithLimit(entityId, query); | |
94 | 94 | } else { |
95 | 95 | long startTs = query.getStartTs(); |
96 | 96 | long endTs = query.getEndTs(); |
97 | 97 | long timeBucket = query.getInterval(); |
98 | - ListenableFuture<List<Optional<TsKvEntry>>> future = findAllAndAggregateAsync(tenantId, entityId, query.getKey(), startTs, endTs, timeBucket, query.getAggregation()); | |
98 | + ListenableFuture<List<Optional<TsKvEntry>>> future = findAllAndAggregateAsync(entityId, query.getKey(), startTs, endTs, timeBucket, query.getAggregation()); | |
99 | 99 | return getTskvEntriesFuture(future); |
100 | 100 | } |
101 | 101 | } |
102 | 102 | |
103 | 103 | @Override |
104 | - protected ListenableFuture<List<TsKvEntry>> findAllAsyncWithLimit(TenantId tenantId, EntityId entityId, ReadTsKvQuery query) { | |
104 | + protected ListenableFuture<List<TsKvEntry>> findAllAsyncWithLimit(EntityId entityId, ReadTsKvQuery query) { | |
105 | 105 | String strKey = query.getKey(); |
106 | 106 | Integer keyId = getOrSaveKeyId(strKey); |
107 | 107 | List<TimescaleTsKvEntity> timescaleTsKvEntities = tsKvRepository.findAllWithLimit( |
108 | - tenantId.getId(), | |
109 | 108 | entityId.getId(), |
110 | 109 | keyId, |
111 | 110 | query.getStartTs(), |
... | ... | @@ -117,8 +116,8 @@ public class TimescaleTimeseriesDao extends AbstractSqlTimeseriesDao implements |
117 | 116 | return Futures.immediateFuture(DaoUtil.convertDataList(timescaleTsKvEntities)); |
118 | 117 | } |
119 | 118 | |
120 | - private ListenableFuture<List<Optional<TsKvEntry>>> findAllAndAggregateAsync(TenantId tenantId, EntityId entityId, String key, long startTs, long endTs, long timeBucket, Aggregation aggregation) { | |
121 | - CompletableFuture<List<TimescaleTsKvEntity>> listCompletableFuture = switchAggregation(key, startTs, endTs, timeBucket, aggregation, entityId.getId(), tenantId.getId()); | |
119 | + private ListenableFuture<List<Optional<TsKvEntry>>> findAllAndAggregateAsync(EntityId entityId, String key, long startTs, long endTs, long timeBucket, Aggregation aggregation) { | |
120 | + CompletableFuture<List<TimescaleTsKvEntity>> listCompletableFuture = switchAggregation(key, startTs, endTs, timeBucket, aggregation, entityId.getId()); | |
122 | 121 | SettableFuture<List<TimescaleTsKvEntity>> listenableFuture = SettableFuture.create(); |
123 | 122 | listCompletableFuture.whenComplete((timescaleTsKvEntities, throwable) -> { |
124 | 123 | if (throwable != null) { |
... | ... | @@ -133,7 +132,6 @@ public class TimescaleTimeseriesDao extends AbstractSqlTimeseriesDao implements |
133 | 132 | timescaleTsKvEntities.forEach(entity -> { |
134 | 133 | if (entity != null && entity.isNotEmpty()) { |
135 | 134 | entity.setEntityId(entityId.getId()); |
136 | - entity.setTenantId(tenantId.getId()); | |
137 | 135 | entity.setStrKey(key); |
138 | 136 | result.add(Optional.of(DaoUtil.getData(entity))); |
139 | 137 | } else { |
... | ... | @@ -167,7 +165,6 @@ public class TimescaleTimeseriesDao extends AbstractSqlTimeseriesDao implements |
167 | 165 | String strKey = tsKvEntry.getKey(); |
168 | 166 | Integer keyId = getOrSaveKeyId(strKey); |
169 | 167 | TimescaleTsKvEntity entity = new TimescaleTsKvEntity(); |
170 | - entity.setTenantId(tenantId.getId()); | |
171 | 168 | entity.setEntityId(entityId.getId()); |
172 | 169 | entity.setTs(tsKvEntry.getTs()); |
173 | 170 | entity.setKey(keyId); |
... | ... | @@ -197,7 +194,6 @@ public class TimescaleTimeseriesDao extends AbstractSqlTimeseriesDao implements |
197 | 194 | Integer keyId = getOrSaveKeyId(strKey); |
198 | 195 | return service.submit(() -> { |
199 | 196 | tsKvRepository.delete( |
200 | - tenantId.getId(), | |
201 | 197 | entityId.getId(), |
202 | 198 | keyId, |
203 | 199 | query.getStartTs(), |
... | ... | @@ -208,7 +204,7 @@ public class TimescaleTimeseriesDao extends AbstractSqlTimeseriesDao implements |
208 | 204 | |
209 | 205 | @Override |
210 | 206 | public ListenableFuture<Void> removeLatest(TenantId tenantId, EntityId entityId, DeleteTsKvQuery query) { |
211 | - return getRemoveLatestFuture(tenantId, entityId, query); | |
207 | + return getRemoveLatestFuture(entityId, query); | |
212 | 208 | } |
213 | 209 | |
214 | 210 | @Override |
... | ... | @@ -216,27 +212,26 @@ public class TimescaleTimeseriesDao extends AbstractSqlTimeseriesDao implements |
216 | 212 | return service.submit(() -> null); |
217 | 213 | } |
218 | 214 | |
219 | - private CompletableFuture<List<TimescaleTsKvEntity>> switchAggregation(String key, long startTs, long endTs, long timeBucket, Aggregation aggregation, UUID entityId, UUID tenantId) { | |
215 | + private CompletableFuture<List<TimescaleTsKvEntity>> switchAggregation(String key, long startTs, long endTs, long timeBucket, Aggregation aggregation, UUID entityId) { | |
220 | 216 | switch (aggregation) { |
221 | 217 | case AVG: |
222 | - return findAvg(key, startTs, endTs, timeBucket, entityId, tenantId); | |
218 | + return findAvg(key, startTs, endTs, timeBucket, entityId); | |
223 | 219 | case MAX: |
224 | - return findMax(key, startTs, endTs, timeBucket, entityId, tenantId); | |
220 | + return findMax(key, startTs, endTs, timeBucket, entityId); | |
225 | 221 | case MIN: |
226 | - return findMin(key, startTs, endTs, timeBucket, entityId, tenantId); | |
222 | + return findMin(key, startTs, endTs, timeBucket, entityId); | |
227 | 223 | case SUM: |
228 | - return findSum(key, startTs, endTs, timeBucket, entityId, tenantId); | |
224 | + return findSum(key, startTs, endTs, timeBucket, entityId); | |
229 | 225 | case COUNT: |
230 | - return findCount(key, startTs, endTs, timeBucket, entityId, tenantId); | |
226 | + return findCount(key, startTs, endTs, timeBucket, entityId); | |
231 | 227 | default: |
232 | 228 | throw new IllegalArgumentException("Not supported aggregation type: " + aggregation); |
233 | 229 | } |
234 | 230 | } |
235 | 231 | |
236 | - private CompletableFuture<List<TimescaleTsKvEntity>> findCount(String key, long startTs, long endTs, long timeBucket, UUID entityId, UUID tenantId) { | |
232 | + private CompletableFuture<List<TimescaleTsKvEntity>> findCount(String key, long startTs, long endTs, long timeBucket, UUID entityId) { | |
237 | 233 | Integer keyId = getOrSaveKeyId(key); |
238 | 234 | return aggregationRepository.findCount( |
239 | - tenantId, | |
240 | 235 | entityId, |
241 | 236 | keyId, |
242 | 237 | timeBucket, |
... | ... | @@ -244,10 +239,9 @@ public class TimescaleTimeseriesDao extends AbstractSqlTimeseriesDao implements |
244 | 239 | endTs); |
245 | 240 | } |
246 | 241 | |
247 | - private CompletableFuture<List<TimescaleTsKvEntity>> findSum(String key, long startTs, long endTs, long timeBucket, UUID entityId, UUID tenantId) { | |
242 | + private CompletableFuture<List<TimescaleTsKvEntity>> findSum(String key, long startTs, long endTs, long timeBucket, UUID entityId) { | |
248 | 243 | Integer keyId = getOrSaveKeyId(key); |
249 | 244 | return aggregationRepository.findSum( |
250 | - tenantId, | |
251 | 245 | entityId, |
252 | 246 | keyId, |
253 | 247 | timeBucket, |
... | ... | @@ -255,10 +249,9 @@ public class TimescaleTimeseriesDao extends AbstractSqlTimeseriesDao implements |
255 | 249 | endTs); |
256 | 250 | } |
257 | 251 | |
258 | - private CompletableFuture<List<TimescaleTsKvEntity>> findMin(String key, long startTs, long endTs, long timeBucket, UUID entityId, UUID tenantId) { | |
252 | + private CompletableFuture<List<TimescaleTsKvEntity>> findMin(String key, long startTs, long endTs, long timeBucket, UUID entityId) { | |
259 | 253 | Integer keyId = getOrSaveKeyId(key); |
260 | 254 | return aggregationRepository.findMin( |
261 | - tenantId, | |
262 | 255 | entityId, |
263 | 256 | keyId, |
264 | 257 | timeBucket, |
... | ... | @@ -266,10 +259,9 @@ public class TimescaleTimeseriesDao extends AbstractSqlTimeseriesDao implements |
266 | 259 | endTs); |
267 | 260 | } |
268 | 261 | |
269 | - private CompletableFuture<List<TimescaleTsKvEntity>> findMax(String key, long startTs, long endTs, long timeBucket, UUID entityId, UUID tenantId) { | |
262 | + private CompletableFuture<List<TimescaleTsKvEntity>> findMax(String key, long startTs, long endTs, long timeBucket, UUID entityId) { | |
270 | 263 | Integer keyId = getOrSaveKeyId(key); |
271 | 264 | return aggregationRepository.findMax( |
272 | - tenantId, | |
273 | 265 | entityId, |
274 | 266 | keyId, |
275 | 267 | timeBucket, |
... | ... | @@ -277,10 +269,9 @@ public class TimescaleTimeseriesDao extends AbstractSqlTimeseriesDao implements |
277 | 269 | endTs); |
278 | 270 | } |
279 | 271 | |
280 | - private CompletableFuture<List<TimescaleTsKvEntity>> findAvg(String key, long startTs, long endTs, long timeBucket, UUID entityId, UUID tenantId) { | |
272 | + private CompletableFuture<List<TimescaleTsKvEntity>> findAvg(String key, long startTs, long endTs, long timeBucket, UUID entityId) { | |
281 | 273 | Integer keyId = getOrSaveKeyId(key); |
282 | 274 | return aggregationRepository.findAvg( |
283 | - tenantId, | |
284 | 275 | entityId, |
285 | 276 | keyId, |
286 | 277 | timeBucket, | ... | ... |
... | ... | @@ -31,12 +31,10 @@ import java.util.UUID; |
31 | 31 | @TimescaleDBTsDao |
32 | 32 | public interface TsKvTimescaleRepository extends CrudRepository<TimescaleTsKvEntity, TimescaleTsKvCompositeKey> { |
33 | 33 | |
34 | - @Query("SELECT tskv FROM TimescaleTsKvEntity tskv WHERE tskv.tenantId = :tenantId " + | |
35 | - "AND tskv.entityId = :entityId " + | |
34 | + @Query("SELECT tskv FROM TimescaleTsKvEntity tskv WHERE tskv.entityId = :entityId " + | |
36 | 35 | "AND tskv.key = :entityKey " + |
37 | 36 | "AND tskv.ts > :startTs AND tskv.ts <= :endTs") |
38 | 37 | List<TimescaleTsKvEntity> findAllWithLimit( |
39 | - @Param("tenantId") UUID tenantId, | |
40 | 38 | @Param("entityId") UUID entityId, |
41 | 39 | @Param("entityKey") int key, |
42 | 40 | @Param("startTs") long startTs, |
... | ... | @@ -44,12 +42,10 @@ public interface TsKvTimescaleRepository extends CrudRepository<TimescaleTsKvEnt |
44 | 42 | |
45 | 43 | @Transactional |
46 | 44 | @Modifying |
47 | - @Query("DELETE FROM TimescaleTsKvEntity tskv WHERE tskv.tenantId = :tenantId " + | |
48 | - "AND tskv.entityId = :entityId " + | |
45 | + @Query("DELETE FROM TimescaleTsKvEntity tskv WHERE tskv.entityId = :entityId " + | |
49 | 46 | "AND tskv.key = :entityKey " + |
50 | 47 | "AND tskv.ts > :startTs AND tskv.ts <= :endTs") |
51 | - void delete(@Param("tenantId") UUID tenantId, | |
52 | - @Param("entityId") UUID entityId, | |
48 | + void delete(@Param("entityId") UUID entityId, | |
53 | 49 | @Param("entityKey") int key, |
54 | 50 | @Param("startTs") long startTs, |
55 | 51 | @Param("endTs") long endTs); | ... | ... |
dao/src/main/resources/sql/schema-timescale-idx.sql
deleted
100644 → 0
1 | --- | |
2 | --- Copyright © 2016-2020 The Thingsboard Authors | |
3 | --- | |
4 | --- Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | --- you may not use this file except in compliance with the License. | |
6 | --- You may obtain a copy of the License at | |
7 | --- | |
8 | --- http://www.apache.org/licenses/LICENSE-2.0 | |
9 | --- | |
10 | --- Unless required by applicable law or agreed to in writing, software | |
11 | --- distributed under the License is distributed on an "AS IS" BASIS, | |
12 | --- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | --- See the License for the specific language governing permissions and | |
14 | --- limitations under the License. | |
15 | --- | |
16 | - | |
17 | -CREATE INDEX IF NOT EXISTS idx_tenant_ts_kv ON tenant_ts_kv(tenant_id, entity_id, key, ts); | |
\ No newline at end of file |
... | ... | @@ -16,8 +16,7 @@ |
16 | 16 | |
17 | 17 | CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE; |
18 | 18 | |
19 | -CREATE TABLE IF NOT EXISTS tenant_ts_kv ( | |
20 | - tenant_id uuid NOT NULL, | |
19 | +CREATE TABLE IF NOT EXISTS ts_kv ( | |
21 | 20 | entity_id uuid NOT NULL, |
22 | 21 | key int NOT NULL, |
23 | 22 | ts bigint NOT NULL, |
... | ... | @@ -26,7 +25,7 @@ CREATE TABLE IF NOT EXISTS tenant_ts_kv ( |
26 | 25 | long_v bigint, |
27 | 26 | dbl_v double precision, |
28 | 27 | json_v json, |
29 | - CONSTRAINT tenant_ts_kv_pkey PRIMARY KEY (tenant_id, entity_id, key, ts) | |
28 | + CONSTRAINT ts_kv_pkey PRIMARY KEY (entity_id, key, ts) | |
30 | 29 | ); |
31 | 30 | |
32 | 31 | CREATE TABLE IF NOT EXISTS ts_kv_dictionary ( | ... | ... |
... | ... | @@ -44,7 +44,7 @@ public class SqlDaoServiceTestSuite { |
44 | 44 | |
45 | 45 | // @ClassRule |
46 | 46 | // public static CustomSqlUnit sqlUnit = new CustomSqlUnit( |
47 | -// Arrays.asList("sql/schema-timescale.sql", "sql/schema-timescale-idx.sql", "sql/schema-entities.sql", "sql/schema-entities-idx.sql", "sql/system-data.sql", "sql/system-test.sql"), | |
47 | +// Arrays.asList("sql/schema-timescale.sql", "sql/schema-entities.sql", "sql/schema-entities-idx.sql", "sql/system-data.sql", "sql/system-test.sql"), | |
48 | 48 | // "sql/timescale/drop-all-tables.sql", |
49 | 49 | // "sql-test.properties" |
50 | 50 | // ); | ... | ... |
... | ... | @@ -12,7 +12,7 @@ DROP TABLE IF EXISTS event; |
12 | 12 | DROP TABLE IF EXISTS relation; |
13 | 13 | DROP TABLE IF EXISTS tb_user; |
14 | 14 | DROP TABLE IF EXISTS tenant; |
15 | -DROP TABLE IF EXISTS tenant_ts_kv; | |
15 | +DROP TABLE IF EXISTS ts_kv; | |
16 | 16 | DROP TABLE IF EXISTS ts_kv_latest; |
17 | 17 | DROP TABLE IF EXISTS user_credentials; |
18 | 18 | DROP TABLE IF EXISTS widget_type; | ... | ... |