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 Oct 9, 2023
1 parent 6f038ec commit 6116ec6
Show file tree
Hide file tree
Showing 8 changed files with 495 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 OR REPLACE 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) = set_policy_scheduled.hypertable::text
)
SELECT count(*) FROM affected_policies;
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE OR REPLACE 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) = set_all_policy_scheduled.hypertable::text
)
SELECT count(*) FROM affected_policies;
$$
LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE OR REPLACE 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 OR REPLACE 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 OR REPLACE 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 OR REPLACE 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 OR REPLACE 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 OR REPLACE 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 OR REPLACE 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 OR REPLACE 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;
90 changes: 90 additions & 0 deletions sql/updates/latest-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,93 @@ DROP TABLE _timescaledb_internal.tmp_chunk_seq_value;
GRANT SELECT ON _timescaledb_catalog.chunk_id_seq TO PUBLIC;
GRANT SELECT ON _timescaledb_catalog.chunk TO PUBLIC;
-- end recreate _timescaledb_catalog.chunk table --

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) = set_policy_scheduled.hypertable::text
)
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) = set_all_policy_scheduled.hypertable::text
)
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
Expand Up @@ -124,3 +124,14 @@ GRANT SELECT ON _timescaledb_catalog.chunk_id_seq TO PUBLIC;
GRANT SELECT ON _timescaledb_catalog.chunk TO PUBLIC;

-- end recreate _timescaledb_catalog.chunk table --

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);
Loading

0 comments on commit 6116ec6

Please sign in to comment.