Skip to content

Commit

Permalink
Add functions to enable/disable policies
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesGuthrie committed Sep 27, 2023
1 parent d07cb91 commit f279223
Show file tree
Hide file tree
Showing 8 changed files with 494 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .unreleased/PR_6116
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Implements: #6116 Add functions to enable/disable hypertable policies

91 changes: 90 additions & 1 deletion sql/policy_api.sql
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,93 @@ CREATE OR REPLACE FUNCTION timescaledb_experimental.show_policies(
relation REGCLASS)
RETURNS SETOF JSONB
AS '@MODULE_PATHNAME@', 'ts_policies_show'
LANGUAGE C VOLATILE;
LANGUAGE C VOLATILE;

CREATE FUNCTION _timescaledb_functions.set_policy_scheduled(hypertable REGCLASS, policy_type TEXT, scheduled BOOL)
RETURNS INTEGER
AS $$
WITH affected_policies AS (
SELECT @[email protected]_job(j.id, scheduled => set_policy_scheduled.scheduled)
FROM _timescaledb_config.bgw_job j
JOIN _timescaledb_catalog.hypertable h ON h.id = j.hypertable_id
WHERE j.proc_schema IN ('_timescaledb_internal', '_timescaledb_functions')
AND j.proc_name = set_policy_scheduled.policy_type
AND j.id >= 1000
AND scheduled <> set_policy_scheduled.scheduled
AND format('%I.%I', h.schema_name, h.table_name)::text::regclass = set_policy_scheduled.hypertable
)
SELECT count(*) FROM affected_policies;
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION _timescaledb_functions.set_all_policy_scheduled(hypertable REGCLASS, scheduled BOOL)
RETURNS INTEGER
AS $$
WITH affected_policies AS (
SELECT @[email protected]_job(j.id, scheduled => set_all_policy_scheduled.scheduled)
FROM _timescaledb_config.bgw_job j
JOIN _timescaledb_catalog.hypertable h ON h.id = j.hypertable_id
WHERE j.proc_schema IN ('_timescaledb_internal', '_timescaledb_functions')
AND j.id >= 1000
AND scheduled <> set_all_policy_scheduled.scheduled
AND format('%I.%I', h.schema_name, h.table_name)::text::regclass = set_all_policy_scheduled.hypertable
)
SELECT count(*) FROM affected_policies;
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_all_policies(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_all_policy_scheduled(hypertable, false);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_all_policies(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_all_policy_scheduled(hypertable, true);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_compression_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_compression', false);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_compression_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_compression', true);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_reorder_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_reorder', false);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_reorder_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_reorder', true);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_retention_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_retention', false);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_retention_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_retention', true);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;
89 changes: 89 additions & 0 deletions sql/updates/latest-dev.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
CREATE FUNCTION _timescaledb_functions.set_policy_scheduled(hypertable REGCLASS, policy_type TEXT, scheduled BOOL)
RETURNS INTEGER
AS $$
WITH affected_policies AS (
SELECT @[email protected]_job(j.id, scheduled => set_policy_scheduled.scheduled)
FROM _timescaledb_config.bgw_job j
JOIN _timescaledb_catalog.hypertable h ON h.id = j.hypertable_id
WHERE j.proc_schema IN ('_timescaledb_internal', '_timescaledb_functions')
AND j.proc_name = set_policy_scheduled.policy_type
AND j.id >= 1000
AND scheduled <> set_policy_scheduled.scheduled
AND format('%I.%I', h.schema_name, h.table_name)::text::regclass = set_policy_scheduled.hypertable
)
SELECT count(*) FROM affected_policies;
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION _timescaledb_functions.set_all_policy_scheduled(hypertable REGCLASS, scheduled BOOL)
RETURNS INTEGER
AS $$
WITH affected_policies AS (
SELECT @[email protected]_job(j.id, scheduled => set_all_policy_scheduled.scheduled)
FROM _timescaledb_config.bgw_job j
JOIN _timescaledb_catalog.hypertable h ON h.id = j.hypertable_id
WHERE j.proc_schema IN ('_timescaledb_internal', '_timescaledb_functions')
AND j.id >= 1000
AND scheduled <> set_all_policy_scheduled.scheduled
AND format('%I.%I', h.schema_name, h.table_name)::text::regclass = set_all_policy_scheduled.hypertable
)
SELECT count(*) FROM affected_policies;
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_all_policies(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_all_policy_scheduled(hypertable, false);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_all_policies(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_all_policy_scheduled(hypertable, true);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_compression_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_compression', false);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_compression_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_compression', true);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_reorder_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_reorder', false);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_reorder_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_reorder', true);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_retention_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_retention', false);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE FUNCTION @[email protected]_retention_policy(hypertable REGCLASS)
RETURNS INTEGER
AS $$
SELECT _timescaledb_functions.set_policy_scheduled(hypertable, 'policy_retention', true);
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

11 changes: 11 additions & 0 deletions sql/updates/reverse-dev.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

DROP FUNCTION _timescaledb_functions.set_policy_scheduled(hypertable REGCLASS, policy_type TEXT, scheduled BOOL);
DROP FUNCTION _timescaledb_functions.set_all_policy_scheduled(hypertable REGCLASS, scheduled BOOL);
DROP FUNCTION @[email protected]_all_policies(hypertable REGCLASS);
DROP FUNCTION @[email protected]_all_policies(hypertable REGCLASS);
DROP FUNCTION @[email protected]_compression_policy(hypertable REGCLASS);
DROP FUNCTION @[email protected]_compression_policy(hypertable REGCLASS);
DROP FUNCTION @[email protected]_reorder_policy(hypertable REGCLASS);
DROP FUNCTION @[email protected]_reorder_policy(hypertable REGCLASS);
DROP FUNCTION @[email protected]_retention_policy(hypertable REGCLASS);
DROP FUNCTION @[email protected]_retention_policy(hypertable REGCLASS);
194 changes: 194 additions & 0 deletions tsl/test/expected/policy_api_enable_disable.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
CREATE TABLE test_table(time timestamptz, chunk_id int);
SELECT create_hypertable('test_table', 'time');
NOTICE: adding not-null constraint to column "time"
create_hypertable
-------------------------
(1,public,test_table,t)
(1 row)

ALTER TABLE test_table SET (timescaledb.compress);
-- reorder
SELECT add_reorder_policy('test_table', 'test_table_time_idx') AS reorder_job_id \gset
SELECT disable_reorder_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=1;
?column?
----------
t
(1 row)

SELECT disable_reorder_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=0;
?column?
----------
t
(1 row)

SELECT scheduled FROM _timescaledb_config.bgw_job WHERE id = :reorder_job_id \gset
SELECT :'scheduled'::bool = false;
?column?
----------
t
(1 row)

SELECT enable_reorder_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=1;
?column?
----------
t
(1 row)

SELECT enable_reorder_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=0;
?column?
----------
t
(1 row)

SELECT scheduled FROM _timescaledb_config.bgw_job WHERE id = :reorder_job_id \gset
SELECT :'scheduled'::bool = true;
?column?
----------
t
(1 row)

SELECT remove_reorder_policy('test_table');
remove_reorder_policy
-----------------------

(1 row)

-- retention
SELECT add_retention_policy('test_table', INTERVAL '4 months', true) AS retention_job_id \gset
SELECT disable_retention_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=1;
?column?
----------
t
(1 row)

SELECT disable_retention_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=0;
?column?
----------
t
(1 row)

SELECT scheduled FROM _timescaledb_config.bgw_job WHERE id = :retention_job_id \gset
SELECT :'scheduled'::bool = false;
?column?
----------
t
(1 row)

SELECT enable_retention_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=1;
?column?
----------
t
(1 row)

SELECT enable_retention_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=0;
?column?
----------
t
(1 row)

SELECT scheduled FROM _timescaledb_config.bgw_job WHERE id = :retention_job_id \gset
SELECT :'scheduled'::bool = true;
?column?
----------
t
(1 row)

SELECT remove_retention_policy('test_table');
remove_retention_policy
-------------------------

(1 row)

-- compression
SELECT add_compression_policy('test_table', compress_after => '1 month'::INTERVAL) AS compression_job_id \gset
SELECT disable_compression_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=1;
?column?
----------
t
(1 row)

SELECT disable_compression_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=0;
?column?
----------
t
(1 row)

SELECT scheduled FROM _timescaledb_config.bgw_job WHERE id = :compression_job_id \gset
SELECT :'scheduled'::bool = false;
?column?
----------
t
(1 row)

SELECT enable_compression_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=1;
?column?
----------
t
(1 row)

SELECT enable_compression_policy('test_table') AS affected_policies \gset
SELECT :affected_policies=0;
?column?
----------
t
(1 row)

SELECT scheduled FROM _timescaledb_config.bgw_job WHERE id = :compression_job_id \gset
SELECT :'scheduled'::bool = true;
?column?
----------
t
(1 row)

SELECT remove_compression_policy('test_table');
remove_compression_policy
---------------------------
t
(1 row)

-- all together now
SELECT add_reorder_policy('test_table', 'test_table_time_idx') AS reorder_job_id \gset
SELECT add_retention_policy('test_table', INTERVAL '4 months', true) AS retention_job_id \gset
SELECT add_compression_policy('test_table', compress_after => '1 month'::INTERVAL) AS compression_job_id \gset
SELECT disable_all_policies('test_table') AS affected_policies \gset
SELECT :affected_policies = 3;
?column?
----------
t
(1 row)

SELECT disable_all_policies('test_table') AS affected_policies \gset
SELECT :affected_policies = 0;
?column?
----------
t
(1 row)

SELECT enable_all_policies('test_table') AS affected_policies \gset
SELECT :affected_policies = 3;
?column?
----------
t
(1 row)

SELECT enable_all_policies('test_table') AS affected_policies \gset
SELECT :affected_policies = 0;
?column?
----------
t
(1 row)

Loading

0 comments on commit f279223

Please sign in to comment.