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

Add functions to enable/disable policies #6116

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JamesGuthrie
Copy link
Member

@JamesGuthrie JamesGuthrie commented Sep 26, 2023

When a user performs a low-downtime data migration, we ask them to turn off some policies. Currently this instruction is an unwieldy SQL statement. This change adds some helper functions to simplify disabling various categories of policy.

  • {enable,disable}_all_policies(hypertable regclass) - enable/disable all compression, retention, and reorder policies on hypertable.
  • {enable,disable}_compression_policy(hypertable regclass) - enable/disable compression policies on hypertable.
  • {enable,disable}_reorder_policy(hypertable regclass) - enable/disable reorder policies on hypertable.
  • {enable,disable}_retention_policy(hypertable regclass) - enable/disable retention policies on hypertable.

@JamesGuthrie JamesGuthrie force-pushed the jg/add-policy-enable-disable-functions branch from 61258c1 to ac26f15 Compare September 26, 2023 07:58
@codecov
Copy link

codecov bot commented Sep 26, 2023

Codecov Report

Merging #6116 (6116ec6) into main (6f038ec) will decrease coverage by 0.02%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main    #6116      +/-   ##
==========================================
- Coverage   81.43%   81.42%   -0.02%     
==========================================
  Files         246      246              
  Lines       56978    56932      -46     
  Branches    12626    12607      -19     
==========================================
- Hits        46401    46357      -44     
+ Misses       8193     8167      -26     
- Partials     2384     2408      +24     

see 45 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@JamesGuthrie JamesGuthrie force-pushed the jg/add-policy-enable-disable-functions branch 5 times, most recently from ce0c0e2 to 5625104 Compare September 26, 2023 12:59
@JamesGuthrie JamesGuthrie marked this pull request as ready for review September 27, 2023 09:41
@github-actions
Copy link

@mahipv, @jnidzwetzki: please review this pull request.

Powered by pull-review

@github-actions github-actions bot requested a review from mahipv September 27, 2023 09:42
@JamesGuthrie JamesGuthrie force-pushed the jg/add-policy-enable-disable-functions branch from 5625104 to f279223 Compare September 27, 2023 09:44
@JamesGuthrie JamesGuthrie changed the title WIP: add functions to enable/disable policies Add functions to enable/disable policies Sep 27, 2023
@JamesGuthrie
Copy link
Member Author

I've obviously done something wrong because the update and downgrade tests are failing, but I'm not sure what

@JamesGuthrie JamesGuthrie force-pushed the jg/add-policy-enable-disable-functions branch 3 times, most recently from c910bb4 to dee3a27 Compare September 27, 2023 10:21
sql/updates/latest-dev.sql Outdated Show resolved Hide resolved
sql/updates/latest-dev.sql Outdated Show resolved Hide resolved
@JamesGuthrie JamesGuthrie force-pushed the jg/add-policy-enable-disable-functions branch 2 times, most recently from a689e89 to 574e0a0 Compare October 3, 2023 08:34
@JamesGuthrie JamesGuthrie force-pushed the jg/add-policy-enable-disable-functions branch from 574e0a0 to 533990b Compare October 9, 2023 12:14
@JamesGuthrie JamesGuthrie force-pushed the jg/add-policy-enable-disable-functions branch from 533990b to 6116ec6 Compare October 9, 2023 12:15
Copy link
Contributor

@mkindahl mkindahl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to add similar functionality for the continuous aggregate policy, but otherwise looks fine. One minor comment on naming.

LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;

CREATE OR REPLACE FUNCTION _timescaledb_functions.set_all_policy_scheduled(hypertable REGCLASS, scheduled BOOL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Consistency with the other names?

Suggested change
CREATE OR REPLACE FUNCTION _timescaledb_functions.set_all_policy_scheduled(hypertable REGCLASS, scheduled BOOL)
CREATE OR REPLACE FUNCTION _timescaledb_functions.set_all_policies_scheduled(hypertable REGCLASS, scheduled BOOL)

Copy link
Contributor

@gayyappan gayyappan Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would not work with data tiering policies. We should treat that as a timescale policy too.
Also, the external API is disable_all_policies and enable_all_policies - as a user I would expect that all policies including any user defined policy would get disabled/enabled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gayyappan Not sure I follow. Why would a different name of the above function not work with data tiering policies?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably also need to check user's permissions before the user is allowed to enable/disable policies. A Should a user without adequate permissions on the hypertable be allowed to disable a policy? IIRC, we check the role's permissions before adding a policy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkindahl The proc_schema in the sql is limited to timescaledb_internal/timescaledb_functions. Not sure I follow. Why would a different name of the above function not work with data tiering policies?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gayyappan Ah, so you were referring to the actual code for implementing the function, not the function name. It was a comment on my comment, so it wasn't entirely clear.

Copy link
Member Author

@JamesGuthrie JamesGuthrie Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would not work with data tiering policies.

Correct. Should it? Data tiering policies reside in a separate extension. Should helper functions in the TimescaleDB extension wrap functions in the OSM extension?

as a user I would expect that all policies including any user defined policy would get disabled/enabled.

The function takes a hypertable as its argument, so it only disables policies which are attached to a hypertable. Maybe the name should change to {enable,disable}_all_policies_for_hypertable. From what I can tell, user-defined policies are never associated with a hypertable, so it wouldn't make sense to enable/disable user-defined policies from this function.

We probably also need to check user's permissions before the user is allowed to enable/disable policies.

All of these functions delegate to alter_job under the hood. Does that not perform the permission check that you mention? If not, which permissions would need to be checked?

Copy link
Contributor

@gayyappan gayyappan Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is true from a code perspective, from a user perspective, this is 1 database running timescale defined policies - so it should just work out of the box. Correct. Should it? Data tiering policies reside in a separate extension. Should helper functions in the TimescaleDB extension wrap functions in the OSM extension?
Either we wrap OSM extension or add hooks so that the functionality can be extended by the OSM extension(in the same way that the timescale extension extends Postgres functionality).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these functions delegate to alter_job under the hood. Does that not perform the permission check that you mention? If not, which permissions would need to be checked?

That could very well be true. You could modify the test to show that a user/role without permissions on the hypertable cannot alter policies.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JamesGuthrie I'll add a follow up to get this to work with OSM extension as well. For this PR, we should verify that permissions are checked by the new apis and add a test case to confirm that.

Copy link
Contributor

@mkindahl mkindahl Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing similar functions for continuous aggregate policy.

Comment on lines +133 to +148
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: instead of policy_type it might be better to just pass in the regproc ID and then match it with the proc_schema.proc_name cast to a regproc. Then it would be impossible to pass in a random text string.

Suggested change
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_policy_scheduled(hypertable REGCLASS, policy_proc REGPROC, 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 format('%I.%I', j.proc_schema, j.proc_name)::regproc = set_policy_scheduled.policy_proc
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;

?column?
----------
t
(1 row)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have some tests for error cases. For example, disabling a policy that has not been added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Enhancement]: hypertable-based enable and disable functions for policies
4 participants