-
Notifications
You must be signed in to change notification settings - Fork 891
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
base: main
Are you sure you want to change the base?
Conversation
61258c1
to
ac26f15
Compare
Codecov Report
@@ 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 |
ce0c0e2
to
5625104
Compare
@mahipv, @jnidzwetzki: please review this pull request.
|
5625104
to
f279223
Compare
I've obviously done something wrong because the update and downgrade tests are failing, but I'm not sure what |
c910bb4
to
dee3a27
Compare
a689e89
to
574e0a0
Compare
574e0a0
to
533990b
Compare
533990b
to
6116ec6
Compare
There was a problem hiding this 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) |
There was a problem hiding this comment.
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?
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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; |
There was a problem hiding this comment.
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.
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) |
There was a problem hiding this comment.
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.
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 onhypertable
.{enable,disable}_compression_policy(hypertable regclass)
- enable/disable compression policies onhypertable
.{enable,disable}_reorder_policy(hypertable regclass)
- enable/disable reorder policies onhypertable
.{enable,disable}_retention_policy(hypertable regclass)
- enable/disable retention policies onhypertable
.