-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions to enable/disable policies
- Loading branch information
1 parent
d07cb91
commit a689e89
Showing
8 changed files
with
494 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Implements: #6116 Add functions to enable/disable hypertable policies | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)::text::regclass = set_policy_scheduled.hypertable | ||
) | ||
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)::text::regclass = set_all_policy_scheduled.hypertable | ||
) | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
Oops, something went wrong.