Skip to content

Commit 22b5e5e

Browse files
committed
splitting out function to get materialization information
1 parent 6ccd570 commit 22b5e5e

File tree

6 files changed

+87
-47
lines changed

6 files changed

+87
-47
lines changed

sql/cagg_api.sql

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@
22
-- Please see the included NOTICE for copyright information and
33
-- LICENSE-APACHE for a copy of the license.
44

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
159
$body$
1610
DECLARE
17-
info RECORD;
18-
aligned TSRANGE;
11+
info RECORD;
1912
BEGIN
20-
SELECT mat_hypertable_id,
13+
SELECT mat_hypertable_id AS materialization_id,
2114
bucket_width::interval AS bucket_width
2215
INTO info
2316
FROM _timescaledb_catalog.continuous_agg
@@ -30,10 +23,29 @@ BEGIN
3023
USING ERRCODE = 'wrong_object_type';
3124
END IF;
3225

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;
3431

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
3547
INSERT INTO _timescaledb_catalog.continuous_aggs_materialization_invalidation_log
36-
VALUES (info.mat_hypertable_id,
48+
VALUES (info.materialization_id,
3749
_timescaledb_functions.to_unix_microseconds(lower(aligned)),
3850
_timescaledb_functions.to_unix_microseconds(upper(aligned)));
3951
END
@@ -47,26 +59,11 @@ CREATE OR REPLACE PROCEDURE _timescaledb_functions.add_materialization_invalidat
4759
) AS
4860
$body$
4961
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);
5264
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-
6865
INSERT INTO _timescaledb_catalog.continuous_aggs_materialization_invalidation_log
69-
VALUES (info.mat_hypertable_id,
66+
VALUES (info.materialization_id,
7067
_timescaledb_functions.to_unix_microseconds(lower(aligned)),
7168
_timescaledb_functions.to_unix_microseconds(upper(aligned)));
7269
END
@@ -109,21 +106,9 @@ CREATE OR REPLACE FUNCTION _timescaledb_functions.get_materialization_invalidati
109106
$body$
110107
DECLARE
111108
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);
114111
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-
127112
-- Compute the multirange for the invalidations inside the
128113
-- restriction passed down to the function and store this in the
129114
-- result record.

sql/updates/latest-dev.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,32 @@ AS '@MODULE_PATHNAME@', 'ts_update_placeholder' LANGUAGE C IMMUTABLE STRICT PARA
8383
CREATE FUNCTION _timescaledb_functions.get_max_for_type(REGTYPE) RETURNS BIGINT
8484
AS '@MODULE_PATHNAME@', 'ts_update_placeholder' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
8585

86+
CREATE FUNCTION _timescaledb_functions.get_materialization_info(
87+
continuous_aggregate REGCLASS
88+
) RETURNS RECORD AS
89+
$body$
90+
DECLARE
91+
info RECORD;
92+
BEGIN
93+
SELECT mat_hypertable_id AS materialization_id,
94+
bucket_width::interval AS bucket_width
95+
INTO info
96+
FROM _timescaledb_catalog.continuous_agg
97+
JOIN _timescaledb_catalog.continuous_aggs_bucket_function
98+
USING (mat_hypertable_id)
99+
WHERE format('%I.%I', user_view_schema, user_view_name)::regclass = continuous_aggregate;
100+
101+
IF NOT FOUND THEN
102+
RAISE '"%" is not a continuous aggregate', continuous_aggregate
103+
USING ERRCODE = 'wrong_object_type';
104+
END IF;
105+
106+
RETURN info;
107+
END
108+
$body$
109+
LANGUAGE plpgsql
110+
SET search_path TO pg_catalog, pg_temp;
111+
86112
CREATE PROCEDURE _timescaledb_functions.add_materialization_invalidations(
87113
continuous_aggregate regclass,
88114
invalidation tsrange

sql/updates/reverse-dev.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ DROP PROCEDURE _timescaledb_functions.add_materialization_invalidations(regclass
4747
DROP PROCEDURE _timescaledb_functions.add_materialization_invalidations(regclass,tstzrange);
4848
DROP FUNCTION _timescaledb_functions.get_raw_materialization_ranges(regtype);
4949
DROP FUNCTION _timescaledb_functions.get_materialization_invalidations(REGCLASS, TSTZRANGE);
50+
DROP FUNCTION _timescaledb_functions.get_materialization_info(REGCLASS);

tsl/test/expected/cagg_api.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ SELECT time_bucket('15 minutes', time), avg(value)
7979
GROUP BY 1;
8080
NOTICE: continuous aggregate "multi_temperature_15m" is already up-to-date
8181
SET search_path TO _timescaledb_functions, public;
82+
-- These are not part of the API, but we test them here just to make
83+
-- sure they work as expected.
84+
SELECT table_name, get_materialization_info(table_name)
85+
FROM (
86+
VALUES ('tstz_temperature_15m'), ('multi_temperature_15m')
87+
) t(table_name);
88+
table_name | get_materialization_info
89+
-----------------------+--------------------------
90+
tstz_temperature_15m | (8,"@ 15 mins")
91+
multi_temperature_15m | (9,"@ 15 mins")
92+
(2 rows)
93+
94+
\set ON_ERROR_STOP 0
95+
SELECT get_materialization_info('hyper_no_cagg');
96+
ERROR: "public.hyper_no_cagg" is not a continuous aggregate
97+
\set ON_ERROR_STOP 1
8298
SELECT * INTO before FROM _timescaledb_catalog.continuous_aggs_materialization_invalidation_log;
8399
CALL _timescaledb_functions.add_materialization_invalidations(
84100
'tstz_temperature_15m'::regclass,

tsl/test/shared/expected/extension.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ ORDER BY pronamespace::regnamespace::text COLLATE "C", p.oid::regprocedure::text
8282
_timescaledb_functions.get_compressed_chunk_index_for_recompression(regclass)
8383
_timescaledb_functions.get_create_command(name)
8484
_timescaledb_functions.get_git_commit()
85+
_timescaledb_functions.get_materialization_info(regclass)
8586
_timescaledb_functions.get_materialization_invalidations(regclass,tstzrange)
8687
_timescaledb_functions.get_max_for_type(regtype)
8788
_timescaledb_functions.get_min_for_type(regtype)

tsl/test/sql/cagg_api.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ GROUP BY 1;
6767

6868
SET search_path TO _timescaledb_functions, public;
6969

70+
-- These are not part of the API, but we test them here just to make
71+
-- sure they work as expected.
72+
SELECT table_name, get_materialization_info(table_name)
73+
FROM (
74+
VALUES ('tstz_temperature_15m'), ('multi_temperature_15m')
75+
) t(table_name);
76+
77+
\set ON_ERROR_STOP 0
78+
SELECT get_materialization_info('hyper_no_cagg');
79+
\set ON_ERROR_STOP 1
80+
7081
SELECT * INTO before FROM _timescaledb_catalog.continuous_aggs_materialization_invalidation_log;
7182

7283
CALL _timescaledb_functions.add_materialization_invalidations(

0 commit comments

Comments
 (0)