summaryrefslogtreecommitdiff
path: root/heap/sql/schema_ddl.sql
blob: 15f969bec8599517085a7b8e9109ac4fd061b2ce (plain)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;

--
-- Name: plpgsql; Type: PROCEDURAL LANGUAGE; Schema: -; Owner: pgsql
--

CREATE PROCEDURAL LANGUAGE plpgsql;


ALTER PROCEDURAL LANGUAGE plpgsql OWNER TO pgsql;

SET search_path = public, pg_catalog;

--
-- Name: first_agg(anyelement, anyelement); Type: FUNCTION; Schema: public; Owner: ebus
--

CREATE FUNCTION first_agg(anyelement, anyelement) RETURNS anyelement
    LANGUAGE sql STABLE
    AS $_$
       SELECT CASE WHEN $1 IS NULL THEN $2 ELSE $1 END;
$_$;


ALTER FUNCTION public.first_agg(anyelement, anyelement) OWNER TO ebus;

--
-- Name: value_cache_aktualisieren(); Type: FUNCTION; Schema: public; Owner: ebus
--

CREATE FUNCTION value_cache_aktualisieren() RETURNS timestamp without time zone
    LANGUAGE plpgsql
    AS $$
DECLARE
   last_update timestamp = CURRENT_TIMESTAMP;
BEGIN
    select max(date_trunc('hour'::text, "timestamp"))
      into last_update
      from value_cache;

    RAISE NOTICE 'last update=%', last_update;
    if last_update is NULL then
      last_update = CURRENT_TIMESTAMP - interval '20 days';
      RAISE NOTICE 'last update set to %', last_update;
    end if;

    delete from value_cache where timestamp >= last_update;

    insert into value_cache 
        select COALESCE(avg(value_float),avg(value_int)), date_trunc('hour', timestamp), sensor_id 
          from value
         where date_trunc('hour', timestamp) > last_update
      group by date_trunc('hour', timestamp), sensor_id;

    RETURN last_update;
END;
$$;


ALTER FUNCTION public.value_cache_aktualisieren() OWNER TO ebus;

--
-- Name: value_insert_trigger_func(); Type: FUNCTION; Schema: public; Owner: ebus
--

CREATE FUNCTION value_insert_trigger_func() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
       BEGIN
    NOTIFY evt_ebus_value_insert;
 
    RETURN NULL;
        END;
$$;


ALTER FUNCTION public.value_insert_trigger_func() OWNER TO ebus;

--
-- Name: first(anyelement); Type: AGGREGATE; Schema: public; Owner: ebus
--

CREATE AGGREGATE first(anyelement) (
    SFUNC = first_agg,
    STYPE = anyelement
);


ALTER AGGREGATE public.first(anyelement) OWNER TO ebus;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: sensor; Type: TABLE; Schema: public; Owner: ebus; Tablespace: 
--

CREATE TABLE sensor (
    id integer NOT NULL,
    name character varying,
    description character varying
);


ALTER TABLE public.sensor OWNER TO ebus;

--
-- Name: sensor_id_seq; Type: SEQUENCE; Schema: public; Owner: ebus
--

CREATE SEQUENCE sensor_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


ALTER TABLE public.sensor_id_seq OWNER TO ebus;

--
-- Name: sensor_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: ebus
--

ALTER SEQUENCE sensor_id_seq OWNED BY sensor.id;


--
-- Name: temp_value; Type: TABLE; Schema: public; Owner: ebus; Tablespace: 
--

CREATE TABLE temp_value (
    id integer,
    "timestamp" timestamp without time zone,
    sensor_id integer,
    type character varying(50),
    value_float real,
    value_int integer,
    value_string character varying
);


ALTER TABLE public.temp_value OWNER TO ebus;

--
-- Name: value; Type: TABLE; Schema: public; Owner: ebus; Tablespace: 
--

CREATE TABLE value (
    id integer NOT NULL,
    "timestamp" timestamp without time zone,
    sensor_id integer,
    type character varying(50),
    value_float real,
    value_int integer,
    value_string character varying
);


ALTER TABLE public.value OWNER TO ebus;

--
-- Name: value_cache; Type: TABLE; Schema: public; Owner: ebus; Tablespace: 
--

CREATE TABLE value_cache (
    value_real integer,
    "timestamp" timestamp without time zone,
    sensor_id integer
);


ALTER TABLE public.value_cache OWNER TO ebus;

--
-- Name: value_id_seq; Type: SEQUENCE; Schema: public; Owner: ebus
--

CREATE SEQUENCE value_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


ALTER TABLE public.value_id_seq OWNER TO ebus;

--
-- Name: value_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: ebus
--

ALTER SEQUENCE value_id_seq OWNED BY value.id;


--
-- Name: vi_value_cache; Type: VIEW; Schema: public; Owner: ebus
--

CREATE VIEW vi_value_cache AS
    SELECT value_cache."timestamp", value_cache.sensor_id, value_cache.value_real, 'CACHE' FROM value_cache UNION SELECT date_trunc('hour'::text, value."timestamp") AS "timestamp", value.sensor_id, COALESCE((avg(value.value_int))::double precision, avg(value.value_float)) AS value_real, 'LIVE' FROM value WHERE (date_trunc('hour'::text, value."timestamp") > COALESCE(((SELECT max(value_cache."timestamp") AS max FROM value_cache))::timestamp with time zone, (now() - '2 days'::interval))) GROUP BY date_trunc('hour'::text, value."timestamp"), value.sensor_id;


ALTER TABLE public.vi_value_cache OWNER TO ebus;

--
-- Name: id; Type: DEFAULT; Schema: public; Owner: ebus
--

ALTER TABLE sensor ALTER COLUMN id SET DEFAULT nextval('sensor_id_seq'::regclass);


--
-- Name: id; Type: DEFAULT; Schema: public; Owner: ebus
--

ALTER TABLE value ALTER COLUMN id SET DEFAULT nextval('value_id_seq'::regclass);


--
-- Name: sensor_pkey; Type: CONSTRAINT; Schema: public; Owner: ebus; Tablespace: 
--

ALTER TABLE ONLY sensor
    ADD CONSTRAINT sensor_pkey PRIMARY KEY (id);


--
-- Name: value_pkey; Type: CONSTRAINT; Schema: public; Owner: ebus; Tablespace: 
--

ALTER TABLE ONLY value
    ADD CONSTRAINT value_pkey PRIMARY KEY (id);


--
-- Name: ix_value__sensor_timestamp; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value__sensor_timestamp ON value USING btree (date_trunc('hour'::text, "timestamp"), sensor_id);


--
-- Name: ix_value__sensor_timestamp_day; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value__sensor_timestamp_day ON value USING btree (date_trunc('day'::text, "timestamp"), sensor_id);


--
-- Name: ix_value_cache_sensor_id; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value_cache_sensor_id ON value_cache USING btree (sensor_id);


--
-- Name: ix_value_cache_timestamp_sensor_id; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value_cache_timestamp_sensor_id ON value_cache USING btree ("timestamp", sensor_id);


--
-- Name: ix_value_sensor_id; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value_sensor_id ON value USING btree (sensor_id);


--
-- Name: ix_value_timestamp_full; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value_timestamp_full ON value USING btree ("timestamp");


--
-- Name: ix_value_timestamp_sensor_144; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value_timestamp_sensor_144 ON value USING btree ("timestamp", sensor_id) WHERE (sensor_id = 144);


--
-- Name: ix_value_timestamp_sensor_146; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value_timestamp_sensor_146 ON value USING btree ("timestamp", sensor_id) WHERE (sensor_id = 146);


--
-- Name: ix_value_timestamp_sensor_159; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value_timestamp_sensor_159 ON value USING btree ("timestamp", sensor_id) WHERE (sensor_id = 159);


--
-- Name: ix_value_timestamp_sensor_160; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value_timestamp_sensor_160 ON value USING btree ("timestamp", sensor_id) WHERE (sensor_id = 160);


--
-- Name: ix_value_timestamp_sensor_170; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value_timestamp_sensor_170 ON value USING btree ("timestamp", sensor_id) WHERE ((sensor_id = 170) AND ("timestamp" > '2011-07-01 00:00:00'::timestamp without time zone));


--
-- Name: ix_value_timestamp_sensoren; Type: INDEX; Schema: public; Owner: ebus; Tablespace: 
--

CREATE INDEX ix_value_timestamp_sensoren ON value USING btree ("timestamp", sensor_id) WHERE ((((sensor_id = 159) OR (sensor_id = 144)) OR (sensor_id = 160)) OR (sensor_id = 170));


--
-- Name: value_insert_trigger; Type: TRIGGER; Schema: public; Owner: ebus
--

CREATE TRIGGER value_insert_trigger
    AFTER INSERT ON value
    FOR EACH STATEMENT
    EXECUTE PROCEDURE value_insert_trigger_func();


--
-- Name: value_sensor_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: ebus
--

ALTER TABLE ONLY value
    ADD CONSTRAINT value_sensor_id_fkey FOREIGN KEY (sensor_id) REFERENCES sensor(id);


--
-- Name: public; Type: ACL; Schema: -; Owner: pgsql
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM pgsql;
GRANT ALL ON SCHEMA public TO pgsql;
GRANT ALL ON SCHEMA public TO PUBLIC;


--
-- PostgreSQL database dump complete
--