Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move cagg migrate functions into _timescaledb_functions schema #5560

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/pgspot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ jobs:
--proc-without-search-path
'_timescaledb_internal.cagg_migrate_execute_plan(_cagg_data _timescaledb_catalog.continuous_agg)'
--proc-without-search-path
'extschema.cagg_migrate(cagg regclass,override boolean,drop_old boolean)'
'_timescaledb_functions.cagg_migrate_execute_plan(_cagg_data _timescaledb_catalog.continuous_agg)'
--proc-without-search-path 'extschema.cagg_migrate(cagg regclass,override boolean,drop_old boolean)'

steps:

Expand Down
36 changes: 18 additions & 18 deletions sql/cagg_migrate.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-- aggregate format to the finals form (without partials).

-- Check if exists a plan for migrationg a given cagg
CREATE OR REPLACE FUNCTION _timescaledb_internal.cagg_migrate_plan_exists (
CREATE OR REPLACE FUNCTION _timescaledb_functions.cagg_migrate_plan_exists (
_hypertable_id INTEGER
)
RETURNS BOOLEAN
Expand All @@ -21,7 +21,7 @@ $BODY$
$BODY$ SET search_path TO pg_catalog, pg_temp;

-- Execute all pre-validations required to execute the migration
CREATE OR REPLACE FUNCTION _timescaledb_internal.cagg_migrate_pre_validation (
CREATE OR REPLACE FUNCTION _timescaledb_functions.cagg_migrate_pre_validation (
_cagg_schema TEXT,
_cagg_name TEXT,
_cagg_name_new TEXT
Expand All @@ -46,7 +46,7 @@ BEGIN
RAISE EXCEPTION 'continuous aggregate "%.%" does not require any migration', _cagg_schema, _cagg_name;
END IF;

IF _timescaledb_internal.cagg_migrate_plan_exists(_cagg_data.mat_hypertable_id) IS TRUE THEN
IF _timescaledb_functions.cagg_migrate_plan_exists(_cagg_data.mat_hypertable_id) IS TRUE THEN
RAISE EXCEPTION 'plan already exists for continuous aggregate %.%', _cagg_schema, _cagg_name;
END IF;

Expand All @@ -64,7 +64,7 @@ END;
$BODY$ SET search_path TO pg_catalog, pg_temp;

-- Create migration plan for given cagg
CREATE OR REPLACE PROCEDURE _timescaledb_internal.cagg_migrate_create_plan (
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_create_plan (
_cagg_data _timescaledb_catalog.continuous_agg,
_cagg_name_new TEXT,
_override BOOLEAN DEFAULT FALSE,
Expand All @@ -84,7 +84,7 @@ DECLARE
_interval_type TEXT;
_interval_value TEXT;
BEGIN
IF _timescaledb_internal.cagg_migrate_plan_exists(_cagg_data.mat_hypertable_id) IS TRUE THEN
IF _timescaledb_functions.cagg_migrate_plan_exists(_cagg_data.mat_hypertable_id) IS TRUE THEN
RAISE EXCEPTION 'plan already exists for materialized hypertable %', _cagg_data.mat_hypertable_id;
END IF;

Expand Down Expand Up @@ -209,7 +209,7 @@ END;
$BODY$ SET search_path TO pg_catalog, pg_temp;

-- Create new cagg using the new format
CREATE OR REPLACE PROCEDURE _timescaledb_internal.cagg_migrate_execute_create_new_cagg (
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_execute_create_new_cagg (
_cagg_data _timescaledb_catalog.continuous_agg,
_plan_step _timescaledb_catalog.continuous_agg_migrate_plan_step
)
Expand Down Expand Up @@ -243,7 +243,7 @@ END;
$BODY$ SET search_path TO pg_catalog, pg_temp;

-- Disable policies
CREATE OR REPLACE PROCEDURE _timescaledb_internal.cagg_migrate_execute_disable_policies (
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_execute_disable_policies (
_cagg_data _timescaledb_catalog.continuous_agg,
_plan_step _timescaledb_catalog.continuous_agg_migrate_plan_step
)
Expand All @@ -264,7 +264,7 @@ END;
$BODY$ SET search_path TO pg_catalog, pg_temp;

-- Enable policies
CREATE OR REPLACE PROCEDURE _timescaledb_internal.cagg_migrate_execute_enable_policies (
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_execute_enable_policies (
_cagg_data _timescaledb_catalog.continuous_agg,
_plan_step _timescaledb_catalog.continuous_agg_migrate_plan_step
)
Expand All @@ -287,7 +287,7 @@ END;
$BODY$ SET search_path TO pg_catalog, pg_temp;

-- Copy policies
CREATE OR REPLACE PROCEDURE _timescaledb_internal.cagg_migrate_execute_copy_policies (
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_execute_copy_policies (
_cagg_data _timescaledb_catalog.continuous_agg,
_plan_step _timescaledb_catalog.continuous_agg_migrate_plan_step
)
Expand Down Expand Up @@ -355,7 +355,7 @@ END;
$BODY$ SET search_path TO pg_catalog, pg_temp;

-- Refresh new cagg created by the migration
CREATE OR REPLACE PROCEDURE _timescaledb_internal.cagg_migrate_execute_refresh_new_cagg (
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_execute_refresh_new_cagg (
_cagg_data _timescaledb_catalog.continuous_agg,
_plan_step _timescaledb_catalog.continuous_agg_migrate_plan_step
)
Expand Down Expand Up @@ -396,7 +396,7 @@ END;
$BODY$ SET search_path TO pg_catalog, pg_temp;

-- Copy data from the OLD cagg to the new Materialization Hypertable
CREATE OR REPLACE PROCEDURE _timescaledb_internal.cagg_migrate_execute_copy_data (
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_execute_copy_data (
_cagg_data _timescaledb_catalog.continuous_agg,
_plan_step _timescaledb_catalog.continuous_agg_migrate_plan_step
)
Expand Down Expand Up @@ -431,7 +431,7 @@ END;
$BODY$ SET search_path TO pg_catalog, pg_temp;

-- Rename the new cagg using `_old` suffix and rename the `_new` to the original name
CREATE OR REPLACE PROCEDURE _timescaledb_internal.cagg_migrate_execute_override_cagg (
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_execute_override_cagg (
_cagg_data _timescaledb_catalog.continuous_agg,
_plan_step _timescaledb_catalog.continuous_agg_migrate_plan_step
)
Expand Down Expand Up @@ -461,7 +461,7 @@ END;
$BODY$ SET search_path TO pg_catalog, pg_temp;

-- Remove old cagg if the parameter `drop_old` and `override` is true
CREATE OR REPLACE PROCEDURE _timescaledb_internal.cagg_migrate_execute_drop_old_cagg (
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_execute_drop_old_cagg (
_cagg_data _timescaledb_catalog.continuous_agg,
_plan_step _timescaledb_catalog.continuous_agg_migrate_plan_step
)
Expand All @@ -486,7 +486,7 @@ END;
$BODY$ SET search_path TO pg_catalog, pg_temp;

-- Execute the migration plan, step by step
CREATE OR REPLACE PROCEDURE _timescaledb_internal.cagg_migrate_execute_plan (
CREATE OR REPLACE PROCEDURE _timescaledb_functions.cagg_migrate_execute_plan (
_cagg_data _timescaledb_catalog.continuous_agg
)
LANGUAGE plpgsql AS
Expand Down Expand Up @@ -525,7 +525,7 @@ BEGIN
END IF;

-- execute step migration
_call_stmt := pg_catalog.format('CALL _timescaledb_internal.cagg_migrate_execute_%s($1, $2)', pg_catalog.lower(pg_catalog.replace(_plan_step.type, ' ', '_')));
_call_stmt := pg_catalog.format('CALL _timescaledb_functions.cagg_migrate_execute_%s($1, $2)', pg_catalog.lower(pg_catalog.replace(_plan_step.type, ' ', '_')));
EXECUTE _call_stmt USING _cagg_data, _plan_step;

UPDATE _timescaledb_catalog.continuous_agg_migrate_plan_step
Expand Down Expand Up @@ -571,10 +571,10 @@ BEGIN
_cagg_name_new := pg_catalog.format('%s_new', pg_catalog.substr(_cagg_name, 1, 59));

-- pre-validate the migration and get some variables
_cagg_data := _timescaledb_internal.cagg_migrate_pre_validation(_cagg_schema, _cagg_name, _cagg_name_new);
_cagg_data := _timescaledb_functions.cagg_migrate_pre_validation(_cagg_schema, _cagg_name, _cagg_name_new);

-- create new migration plan
CALL _timescaledb_internal.cagg_migrate_create_plan(_cagg_data, _cagg_name_new, override, drop_old);
CALL _timescaledb_functions.cagg_migrate_create_plan(_cagg_data, _cagg_name_new, override, drop_old);
COMMIT;

-- SET LOCAL is only active until end of transaction.
Expand All @@ -584,7 +584,7 @@ BEGIN
SET LOCAL search_path TO pg_catalog, pg_temp;

-- execute the migration plan
CALL _timescaledb_internal.cagg_migrate_execute_plan(_cagg_data);
CALL _timescaledb_functions.cagg_migrate_execute_plan(_cagg_data);

-- finish the migration plan
UPDATE _timescaledb_catalog.continuous_agg_migrate_plan
Expand Down
20 changes: 20 additions & 0 deletions sql/updates/latest-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,23 @@ $$;
UPDATE _timescaledb_config.bgw_job SET proc_schema = '_timescaledb_functions' WHERE proc_schema = '_timescaledb_internal';
UPDATE _timescaledb_config.bgw_job SET check_schema = '_timescaledb_functions' WHERE check_schema = '_timescaledb_internal';

-- migrate cagg migration functions into _timescaledb_functions schema
DO $$
DECLARE
foid regprocedure;
kind text;
funcs text[] = '{
cagg_migrate_plan_exists, cagg_migrate_pre_validation, cagg_migrate_create_plan, cagg_migrate_execute_create_new_cagg,
cagg_migrate_execute_disable_policies, cagg_migrate_execute_enable_policies, cagg_migrate_execute_copy_policies,
cagg_migrate_execute_refresh_new_cagg, cagg_migrate_execute_copy_data, cagg_migrate_execute_override_cagg,
cagg_migrate_execute_drop_old_cagg, cagg_migrate_execute_plan
}';
BEGIN
FOR foid, kind IN
SELECT oid, CASE WHEN prokind = 'f' THEN 'FUNCTION' ELSE 'PROCEDURE' END FROM pg_proc WHERE proname = ANY(funcs) AND pronamespace = '_timescaledb_internal'::regnamespace
LOOP
EXECUTE format('ALTER %s %s SET SCHEMA _timescaledb_functions', kind, foid);
END LOOP;
END;
$$;

13 changes: 13 additions & 0 deletions sql/updates/reverse-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,16 @@ ALTER PROCEDURE _timescaledb_functions.policy_retention(integer,jsonb) SET SCHEM
UPDATE _timescaledb_config.bgw_job SET proc_schema = '_timescaledb_internal' WHERE proc_schema = '_timescaledb_functions';
UPDATE _timescaledb_config.bgw_job SET check_schema = '_timescaledb_internal' WHERE check_schema = '_timescaledb_functions';

ALTER FUNCTION _timescaledb_functions.cagg_migrate_plan_exists(INTEGER) SET SCHEMA _timescaledb_internal;
ALTER FUNCTION _timescaledb_functions.cagg_migrate_pre_validation(TEXT, TEXT, TEXT) SET SCHEMA _timescaledb_internal;
ALTER PROCEDURE _timescaledb_functions.cagg_migrate_create_plan(_timescaledb_catalog.continuous_agg, TEXT, BOOLEAN, BOOLEAN) SET SCHEMA _timescaledb_internal;
ALTER PROCEDURE _timescaledb_functions.cagg_migrate_execute_create_new_cagg(_timescaledb_catalog.continuous_agg, _timescaledb_catalog.continuous_agg_migrate_plan_step) SET SCHEMA _timescaledb_internal;
ALTER PROCEDURE _timescaledb_functions.cagg_migrate_execute_disable_policies(_timescaledb_catalog.continuous_agg, _timescaledb_catalog.continuous_agg_migrate_plan_step) SET SCHEMA _timescaledb_internal;
ALTER PROCEDURE _timescaledb_functions.cagg_migrate_execute_enable_policies(_timescaledb_catalog.continuous_agg, _timescaledb_catalog.continuous_agg_migrate_plan_step) SET SCHEMA _timescaledb_internal;
ALTER PROCEDURE _timescaledb_functions.cagg_migrate_execute_copy_policies(_timescaledb_catalog.continuous_agg, _timescaledb_catalog.continuous_agg_migrate_plan_step) SET SCHEMA _timescaledb_internal;
ALTER PROCEDURE _timescaledb_functions.cagg_migrate_execute_refresh_new_cagg(_timescaledb_catalog.continuous_agg, _timescaledb_catalog.continuous_agg_migrate_plan_step) SET SCHEMA _timescaledb_internal;
ALTER PROCEDURE _timescaledb_functions.cagg_migrate_execute_copy_data(_timescaledb_catalog.continuous_agg, _timescaledb_catalog.continuous_agg_migrate_plan_step) SET SCHEMA _timescaledb_internal;
ALTER PROCEDURE _timescaledb_functions.cagg_migrate_execute_override_cagg(_timescaledb_catalog.continuous_agg, _timescaledb_catalog.continuous_agg_migrate_plan_step) SET SCHEMA _timescaledb_internal;
ALTER PROCEDURE _timescaledb_functions.cagg_migrate_execute_drop_old_cagg(_timescaledb_catalog.continuous_agg, _timescaledb_catalog.continuous_agg_migrate_plan_step) SET SCHEMA _timescaledb_internal;
ALTER PROCEDURE _timescaledb_functions.cagg_migrate_execute_plan(_timescaledb_catalog.continuous_agg) SET SCHEMA _timescaledb_internal;

18 changes: 9 additions & 9 deletions tsl/test/expected/cagg_migrate.out
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ psql:include/cagg_migrate_common.sql:129: ERROR: continuous aggregate "public.c
DROP MATERIALIZED VIEW conditions_summary_daily_new;
-- get and set all the cagg data
SELECT
_timescaledb_internal.cagg_migrate_pre_validation(
_timescaledb_functions.cagg_migrate_pre_validation(
'public',
'conditions_summary_daily',
'conditions_summary_daily_new'
) AS "CAGG_DATA"
\gset
CALL _timescaledb_internal.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
CALL _timescaledb_functions.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
\x on
SELECT mat_hypertable_id, user_view_definition FROM _timescaledb_catalog.continuous_agg_migrate_plan;
-[ RECORD 1 ]--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -224,7 +224,7 @@ SELECT mat_hypertable_id, step_id, status, type, config FROM _timescaledb_catalo

\set ON_ERROR_STOP 0
-- should error because plan already exists
CALL _timescaledb_internal.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
CALL _timescaledb_functions.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
psql:include/cagg_migrate_common.sql:156: ERROR: plan already exists for materialized hypertable 3
CALL cagg_migrate('conditions_summary_daily');
psql:include/cagg_migrate_common.sql:157: ERROR: plan already exists for continuous aggregate public.conditions_summary_daily
Expand Down Expand Up @@ -846,13 +846,13 @@ psql:include/cagg_migrate_common.sql:129: ERROR: continuous aggregate "public.c
DROP MATERIALIZED VIEW conditions_summary_daily_new;
-- get and set all the cagg data
SELECT
_timescaledb_internal.cagg_migrate_pre_validation(
_timescaledb_functions.cagg_migrate_pre_validation(
'public',
'conditions_summary_daily',
'conditions_summary_daily_new'
) AS "CAGG_DATA"
\gset
CALL _timescaledb_internal.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
CALL _timescaledb_functions.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
\x on
SELECT mat_hypertable_id, user_view_definition FROM _timescaledb_catalog.continuous_agg_migrate_plan;
-[ RECORD 1 ]--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -920,7 +920,7 @@ SELECT mat_hypertable_id, step_id, status, type, config FROM _timescaledb_catalo

\set ON_ERROR_STOP 0
-- should error because plan already exists
CALL _timescaledb_internal.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
CALL _timescaledb_functions.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
psql:include/cagg_migrate_common.sql:156: ERROR: plan already exists for materialized hypertable 16
CALL cagg_migrate('conditions_summary_daily');
psql:include/cagg_migrate_common.sql:157: ERROR: plan already exists for continuous aggregate public.conditions_summary_daily
Expand Down Expand Up @@ -1525,13 +1525,13 @@ psql:include/cagg_migrate_common.sql:129: ERROR: continuous aggregate "public.c
DROP MATERIALIZED VIEW conditions_summary_daily_new;
-- get and set all the cagg data
SELECT
_timescaledb_internal.cagg_migrate_pre_validation(
_timescaledb_functions.cagg_migrate_pre_validation(
'public',
'conditions_summary_daily',
'conditions_summary_daily_new'
) AS "CAGG_DATA"
\gset
CALL _timescaledb_internal.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
CALL _timescaledb_functions.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
\x on
SELECT mat_hypertable_id, user_view_definition FROM _timescaledb_catalog.continuous_agg_migrate_plan;
-[ RECORD 1 ]--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1599,7 +1599,7 @@ SELECT mat_hypertable_id, step_id, status, type, config FROM _timescaledb_catalo

\set ON_ERROR_STOP 0
-- should error because plan already exists
CALL _timescaledb_internal.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
CALL _timescaledb_functions.cagg_migrate_create_plan(:'CAGG_DATA', 'conditions_summary_daily_new');
psql:include/cagg_migrate_common.sql:156: ERROR: plan already exists for materialized hypertable 29
CALL cagg_migrate('conditions_summary_daily');
psql:include/cagg_migrate_common.sql:157: ERROR: plan already exists for continuous aggregate public.conditions_summary_daily
Expand Down