Skip to content

Commit

Permalink
Fix cagg_migrate_to_time_bucket during update
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fabriziomello committed Feb 21, 2025
1 parent c957433 commit 860cb3f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
20 changes: 0 additions & 20 deletions sql/updates/2.14.2--2.15.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -427,23 +427,3 @@ DROP FUNCTION IF EXISTS _timescaledb_functions.policy_job_error_retention_check(
--
-- END bgw_job_stat_history
--

-- Migrate existing CAggs using time_bucket_ng to time_bucket
CREATE PROCEDURE _timescaledb_functions.cagg_migrate_to_time_bucket(cagg REGCLASS)
AS '@MODULE_PATHNAME@', 'ts_continuous_agg_migrate_to_time_bucket' LANGUAGE C;

DO $$
DECLARE
cagg_name regclass;
BEGIN
FOR cagg_name IN
SELECT pg_catalog.format('%I.%I', user_view_schema, user_view_name)::regclass
FROM _timescaledb_catalog.continuous_agg cagg
JOIN _timescaledb_catalog.continuous_aggs_bucket_function AS bf ON (cagg.mat_hypertable_id = bf.mat_hypertable_id)
WHERE
bf.bucket_func::text LIKE '%time_bucket_ng%'
LOOP
CALL _timescaledb_functions.cagg_migrate_to_time_bucket(cagg_name);
END LOOP;
END
$$;
37 changes: 37 additions & 0 deletions sql/updates/post-update.sql
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,40 @@ $$;
-- Repair relations that have relacl entries for users that do not
-- exist in pg_authid
CALL _timescaledb_functions.repair_relation_acls();

-- Migrate existing CAggs using time_bucket_ng to time_bucket
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_to_time_bucket(cagg REGCLASS)
AS '@MODULE_PATHNAME@', 'ts_continuous_agg_migrate_to_time_bucket' LANGUAGE C;

DO $$
DECLARE
cagg regclass;
ts_major INTEGER;
ts_minor INTEGER;
BEGIN
SELECT
((string_to_array(extversion,'.'))[1])::int,
((string_to_array(extversion,'.'))[2])::int
INTO
ts_major, ts_minor
FROM
pg_catalog.pg_extension
WHERE
extname = 'timescaledb';

-- CAggs supporting time_bucket with orign and/or offset was released on 2.15.X
IF ts_major >= 2 AND ts_minor >= 15 THEN
FOR cagg IN
SELECT
pg_catalog.format('%I.%I', user_view_schema, user_view_name)::regclass
FROM
_timescaledb_catalog.continuous_agg,
LATERAL _timescaledb_functions.cagg_get_bucket_function_info(mat_hypertable_id) AS bf
WHERE
bf.bucket_func::text LIKE '%time_bucket_ng%'
LOOP
CALL _timescaledb_functions.cagg_migrate_to_time_bucket(cagg);
END LOOP;
END IF;
END
$$;

0 comments on commit 860cb3f

Please sign in to comment.