@@ -102,16 +102,15 @@ SET search_path TO pg_catalog, pg_temp;
102
102
CREATE OR REPLACE FUNCTION _timescaledb_functions .get_materialization_invalidations(
103
103
continuous_aggregate REGCLASS,
104
104
restriction TSTZRANGE
105
- ) RETURNS TSTZMULTIRANGE AS
105
+ ) RETURNS TABLE (invalidations TSTZMULTIRANGE) AS
106
106
$body$
107
107
DECLARE
108
- result TSTZMULTIRANGE;
109
108
info RECORD := _timescaledb_functions .get_materialization_info (continuous_aggregate);
110
109
aligned TSTZRANGE := _timescaledb_functions .align_to_bucket (info .bucket_width , restriction);
111
110
BEGIN
112
111
-- Compute the multirange for the invalidations inside the
113
- -- restriction passed down to the function and store this in the
114
- -- result record.
112
+ -- restriction passed down to the function and return the ranges.
113
+ RETURN QUERY
115
114
WITH
116
115
ranges AS (
117
116
SELECT materialization_id,
@@ -121,53 +120,10 @@ BEGIN
121
120
FROM _timescaledb_functions .get_raw_materialization_ranges (' timestamptz' ::regtype)
122
121
GROUP BY materialization_id
123
122
)
124
- SELECT range_agg(invals * multirange(aligned)) INTO result
123
+ SELECT range_agg(invals * multirange(aligned))
125
124
FROM ranges
126
125
WHERE invals && aligned
127
126
AND materialization_id = info .materialization_id ;
128
-
129
- -- Delete all tuples that intersect with the restriction passed
130
- -- down and insert the "residue" resulting from removing the
131
- -- invalidation ranges after being restricted.
132
- WITH
133
- ranges AS (
134
- SELECT materialization_id,
135
- range_agg(_timescaledb_functions .as_multirange (lowest_modified_value,
136
- greatest_modified_value,
137
- null ::tstzrange)) AS invals
138
- FROM _timescaledb_functions .get_raw_materialization_ranges (' timestamptz' ::regtype)
139
- GROUP BY materialization_id
140
- ),
141
- residue AS (
142
- DELETE FROM _timescaledb_catalog .continuous_aggs_materialization_invalidation_log
143
- WHERE _timescaledb_functions .as_multirange (lowest_modified_value,
144
- greatest_modified_value,
145
- null ::tstzrange)
146
- && aligned
147
- AND materialization_id = info .materialization_id
148
- AND lowest_modified_value
149
- BETWEEN _timescaledb_functions .get_min_for_type (' timestamptz' ::regtype)
150
- AND _timescaledb_functions .get_max_for_type (' timestamptz' ::regtype)
151
- AND greatest_modified_value
152
- BETWEEN _timescaledb_functions .get_min_for_type (' timestamptz' ::regtype)
153
- AND _timescaledb_functions .get_max_for_type (' timestamptz' ::regtype)
154
- RETURNING materialization_id,
155
- _timescaledb_functions .as_multirange (lowest_modified_value,
156
- greatest_modified_value,
157
- null ::tstzrange)
158
- - multirange(aligned) as rng
159
- )
160
- INSERT INTO _timescaledb_catalog .continuous_aggs_materialization_invalidation_log
161
- SELECT materialization_id,
162
- _timescaledb_functions .to_unix_microseconds (lower (rng)),
163
- _timescaledb_functions .to_unix_microseconds (upper (rng))
164
- FROM (
165
- SELECT materialization_id, unnest(range_agg(rng))
166
- FROM residue
167
- GROUP BY materialization_id
168
- ) t(materialization_id, rng);
169
-
170
- RETURN result;
171
127
END
172
128
$body$
173
129
LANGUAGE plpgsql
0 commit comments