2
2
-- Please see the included NOTICE for copyright information and
3
3
-- LICENSE-APACHE for a copy of the license.
4
4
5
- -- Add new invalidations to the materialization invalidation log.
6
- --
7
- -- This will add the range to the materialization invalidations for
8
- -- the continuous aggregate. The range will automatically be "aligned"
9
- -- to the bucket width to ensure that it covers all buckets that it
10
- -- touches.
11
- CREATE OR REPLACE PROCEDURE _timescaledb_functions .add_materialization_invalidations (
12
- continuous_aggregate regclass,
13
- invalidation tsrange
14
- ) AS
5
+ -- Get information about the materialization table and bucket width.
6
+ CREATE OR REPLACE FUNCTION _timescaledb_functions .get_materialization_info(
7
+ continuous_aggregate REGCLASS
8
+ ) RETURNS RECORD AS
15
9
$body$
16
10
DECLARE
17
- info RECORD;
18
- aligned TSRANGE;
11
+ info RECORD;
19
12
BEGIN
20
- SELECT mat_hypertable_id,
13
+ SELECT mat_hypertable_id AS materialization_id ,
21
14
bucket_width::interval AS bucket_width
22
15
INTO info
23
16
FROM _timescaledb_catalog .continuous_agg
@@ -30,10 +23,29 @@ BEGIN
30
23
USING ERRCODE = ' wrong_object_type' ;
31
24
END IF;
32
25
33
- aligned := _timescaledb_functions .align_to_bucket (info .bucket_width , invalidation);
26
+ RETURN info;
27
+ END
28
+ $body$
29
+ LANGUAGE plpgsql
30
+ SET search_path TO pg_catalog, pg_temp;
34
31
32
+ -- Add new invalidations to the materialization invalidation log.
33
+ --
34
+ -- This will add the range to the materialization invalidations for
35
+ -- the continuous aggregate. The range will automatically be "aligned"
36
+ -- to the bucket width to ensure that it covers all buckets that it
37
+ -- touches.
38
+ CREATE OR REPLACE PROCEDURE _timescaledb_functions .add_materialization_invalidations (
39
+ continuous_aggregate regclass,
40
+ invalidation tsrange
41
+ ) AS
42
+ $body$
43
+ DECLARE
44
+ info RECORD := _timescaledb_functions .get_materialization_info (continuous_aggregate);
45
+ aligned TSRANGE := _timescaledb_functions .align_to_bucket (info .bucket_width , invalidation);
46
+ BEGIN
35
47
INSERT INTO _timescaledb_catalog .continuous_aggs_materialization_invalidation_log
36
- VALUES (info .mat_hypertable_id ,
48
+ VALUES (info .materialization_id ,
37
49
_timescaledb_functions .to_unix_microseconds (lower (aligned)),
38
50
_timescaledb_functions .to_unix_microseconds (upper (aligned)));
39
51
END
@@ -47,26 +59,11 @@ CREATE OR REPLACE PROCEDURE _timescaledb_functions.add_materialization_invalidat
47
59
) AS
48
60
$body$
49
61
DECLARE
50
- info RECORD;
51
- aligned TSTZRANGE;
62
+ info RECORD : = _timescaledb_functions . get_materialization_info (continuous_aggregate) ;
63
+ aligned TSTZRANGE : = _timescaledb_functions . align_to_bucket ( info . bucket_width , invalidation) ;
52
64
BEGIN
53
- SELECT mat_hypertable_id,
54
- bucket_width::interval AS bucket_width
55
- INTO info
56
- FROM _timescaledb_catalog .continuous_agg
57
- JOIN _timescaledb_catalog .continuous_aggs_bucket_function
58
- USING (mat_hypertable_id)
59
- WHERE format(' %I.%I' , user_view_schema, user_view_name)::regclass = continuous_aggregate;
60
-
61
- IF NOT FOUND THEN
62
- RAISE ' "%" is not a continuous aggregate' , continuous_aggregate
63
- USING ERRCODE = ' wrong_object_type' ;
64
- END IF;
65
-
66
- aligned := _timescaledb_functions .align_to_bucket (info .bucket_width , invalidation);
67
-
68
65
INSERT INTO _timescaledb_catalog .continuous_aggs_materialization_invalidation_log
69
- VALUES (info .mat_hypertable_id ,
66
+ VALUES (info .materialization_id ,
70
67
_timescaledb_functions .to_unix_microseconds (lower (aligned)),
71
68
_timescaledb_functions .to_unix_microseconds (upper (aligned)));
72
69
END
@@ -109,21 +106,9 @@ CREATE OR REPLACE FUNCTION _timescaledb_functions.get_materialization_invalidati
109
106
$body$
110
107
DECLARE
111
108
result TSTZMULTIRANGE;
112
- aligned TSTZRANGE ;
113
- info RECORD ;
109
+ info RECORD : = _timescaledb_functions . get_materialization_info (continuous_aggregate) ;
110
+ aligned TSTZRANGE : = _timescaledb_functions . align_to_bucket ( info . bucket_width , restriction) ;
114
111
BEGIN
115
- -- Find information about the materialization table, since this is
116
- -- what we use in the invalidation log.
117
- SELECT mat_hypertable_id AS materialization_id,
118
- bucket_width::interval AS bucket_width
119
- INTO info
120
- FROM _timescaledb_catalog .continuous_agg
121
- JOIN _timescaledb_catalog .continuous_aggs_bucket_function
122
- USING (mat_hypertable_id)
123
- WHERE format(' %I.%I' , user_view_schema, user_view_name)::regclass = continuous_aggregate;
124
-
125
- aligned := _timescaledb_functions .align_to_bucket (info .bucket_width , restriction);
126
-
127
112
-- Compute the multirange for the invalidations inside the
128
113
-- restriction passed down to the function and store this in the
129
114
-- result record.
0 commit comments