Skip to content

Commit 58ce092

Browse files
committed
Fix cagg_migrate_to_time_bucket during update
In #6837 we added a migration for CAggs using `time_bucket_ng` to use the new version of `time_bucket` that support orign and/or offset. We made a mistake because we place the code to migrate existing CAggs using `time_bucket_ng` to `time_bucket` in the update script but this code should be placed instead on post-update.sql script where we can call functions from the new extension version loaded.
1 parent f7c755e commit 58ce092

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

sql/updates/2.14.2--2.15.0.sql

-20
Original file line numberDiff line numberDiff line change
@@ -427,23 +427,3 @@ DROP FUNCTION IF EXISTS _timescaledb_functions.policy_job_error_retention_check(
427427
--
428428
-- END bgw_job_stat_history
429429
--
430-
431-
-- Migrate existing CAggs using time_bucket_ng to time_bucket
432-
CREATE PROCEDURE _timescaledb_functions.cagg_migrate_to_time_bucket(cagg REGCLASS)
433-
AS '@MODULE_PATHNAME@', 'ts_continuous_agg_migrate_to_time_bucket' LANGUAGE C;
434-
435-
DO $$
436-
DECLARE
437-
cagg_name regclass;
438-
BEGIN
439-
FOR cagg_name IN
440-
SELECT pg_catalog.format('%I.%I', user_view_schema, user_view_name)::regclass
441-
FROM _timescaledb_catalog.continuous_agg cagg
442-
JOIN _timescaledb_catalog.continuous_aggs_bucket_function AS bf ON (cagg.mat_hypertable_id = bf.mat_hypertable_id)
443-
WHERE
444-
bf.bucket_func::text LIKE '%time_bucket_ng%'
445-
LOOP
446-
CALL _timescaledb_functions.cagg_migrate_to_time_bucket(cagg_name);
447-
END LOOP;
448-
END
449-
$$;

sql/updates/post-update.sql

+37
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,40 @@ $$;
171171
-- Repair relations that have relacl entries for users that do not
172172
-- exist in pg_authid
173173
CALL _timescaledb_functions.repair_relation_acls();
174+
175+
-- Migrate existing CAggs using time_bucket_ng to time_bucket
176+
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_to_time_bucket(cagg REGCLASS)
177+
AS '@MODULE_PATHNAME@', 'ts_continuous_agg_migrate_to_time_bucket' LANGUAGE C;
178+
179+
DO $$
180+
DECLARE
181+
cagg regclass;
182+
ts_major INTEGER;
183+
ts_minor INTEGER;
184+
BEGIN
185+
SELECT
186+
((string_to_array(extversion,'.'))[1])::int,
187+
((string_to_array(extversion,'.'))[2])::int
188+
INTO
189+
ts_major, ts_minor
190+
FROM
191+
pg_catalog.pg_extension
192+
WHERE
193+
extname = 'timescaledb';
194+
195+
-- CAggs supporting time_bucket with orign and/or offset was released on 2.15.X
196+
IF ts_major >= 2 AND ts_minor >= 15 THEN
197+
FOR cagg IN
198+
SELECT
199+
pg_catalog.format('%I.%I', user_view_schema, user_view_name)::regclass
200+
FROM
201+
_timescaledb_catalog.continuous_agg,
202+
LATERAL _timescaledb_functions.cagg_get_bucket_function_info(mat_hypertable_id) AS bf
203+
WHERE
204+
bf.bucket_func::text LIKE '%time_bucket_ng%'
205+
LOOP
206+
CALL _timescaledb_functions.cagg_migrate_to_time_bucket(cagg);
207+
END LOOP;
208+
END IF;
209+
END
210+
$$;

0 commit comments

Comments
 (0)