Skip to content

Commit

Permalink
Allow owner change of continuous aggregate
Browse files Browse the repository at this point in the history
Implements support for changing the owner of the continuous aggregate.
This will change the owner of both the materialized hypertable and the
user view.

Fixes #2414
Fixes #1277
  • Loading branch information
mkindahl committed Sep 22, 2020
1 parent c15d8be commit 0f3697f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/process_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -2814,6 +2814,19 @@ process_altertable_start_matview(ProcessUtilityArgs *args)
process_altercontinuousagg_set_with(cagg, view_relid, (List *) cmd->def);
break;

case AT_ChangeOwner:
/* Change owner of view */
AlterTableInternal(view_relid, list_make1(cmd), false);
hcache = ts_hypertable_cache_pin();
ht = ts_hypertable_cache_get_entry_by_id(hcache, cagg->data.mat_hypertable_id);
Assert(ht); /* Broken continuous aggregate */
ts_hypertable_permissions_check_by_id(ht->fd.id);
check_alter_table_allowed_on_ht_with_compression(ht, stmt);
relation_not_only(stmt->relation);
AlterTableInternal(ht->main_table_relid, list_make1(cmd), false);
ts_cache_release(hcache);
break;

case AT_SetTableSpace:
hcache = ts_hypertable_cache_pin();
ht = ts_hypertable_cache_get_entry_by_id(hcache, cagg->data.mat_hypertable_id);
Expand Down
38 changes: 38 additions & 0 deletions tsl/test/expected/continuous_aggs_ddl.out
Original file line number Diff line number Diff line change
Expand Up @@ -1061,5 +1061,43 @@ FROM metrics_int8
GROUP BY 1;
ERROR: first argument to time_bucket function should be an immutable expression for continuous aggregate query
\set ON_ERROR_STOP 1
-- Test various ALTER MATERIALIZED VIEW statements.
SET ROLE :ROLE_DEFAULT_PERM_USER;
CREATE MATERIALIZED VIEW owner_check WITH (timescaledb.continuous) AS
SELECT time_bucket(1 + 2, time)
FROM metrics_int8
GROUP BY 1
WITH NO DATA;
SELECT relname AS "name",
pg_get_userbyid(relowner) AS "owner"
FROM pg_class, cagg_info
WHERE (oid = user_view OR relname = mat_table)
AND user_view::text = 'owner_check';
name | owner
-----------------------------+-------------------
owner_check | default_perm_user
_materialized_hypertable_24 | default_perm_user
(2 rows)

-- This should not work since the target user has the wrong role, but
-- we test that the normal checks are done when changing the owner.
\set ON_ERROR_STOP 0
ALTER MATERIALIZED VIEW owner_check OWNER TO :ROLE_1;
ERROR: must be member of role "test_role_1"
\set ON_ERROR_STOP 1
-- Superuser can always change owner
SET ROLE :ROLE_SUPERUSER;
ALTER MATERIALIZED VIEW owner_check OWNER TO :ROLE_1;
SELECT relname AS "name",
pg_get_userbyid(relowner) AS "owner"
FROM pg_class, cagg_info
WHERE (oid = user_view OR relname = mat_table)
AND user_view::text = 'owner_check';
name | owner
-----------------------------+-------------
owner_check | test_role_1
_materialized_hypertable_24 | test_role_1
(2 rows)

DROP TABLESPACE tablespace1;
DROP TABLESPACE tablespace2;
31 changes: 31 additions & 0 deletions tsl/test/sql/continuous_aggs_ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,39 @@ CREATE MATERIALIZED VIEW width_expr WITH (timescaledb.continuous) AS
SELECT time_bucket(extract(year FROM now())::int, time)
FROM metrics_int8
GROUP BY 1;
\set ON_ERROR_STOP 1

-- Test various ALTER MATERIALIZED VIEW statements.

SET ROLE :ROLE_DEFAULT_PERM_USER;

CREATE MATERIALIZED VIEW owner_check WITH (timescaledb.continuous) AS
SELECT time_bucket(1 + 2, time)
FROM metrics_int8
GROUP BY 1
WITH NO DATA;

SELECT relname AS "name",
pg_get_userbyid(relowner) AS "owner"
FROM pg_class, cagg_info
WHERE (oid = user_view OR relname = mat_table)
AND user_view::text = 'owner_check';

-- This should not work since the target user has the wrong role, but
-- we test that the normal checks are done when changing the owner.
\set ON_ERROR_STOP 0
ALTER MATERIALIZED VIEW owner_check OWNER TO :ROLE_1;
\set ON_ERROR_STOP 1

-- Superuser can always change owner
SET ROLE :ROLE_SUPERUSER;
ALTER MATERIALIZED VIEW owner_check OWNER TO :ROLE_1;

SELECT relname AS "name",
pg_get_userbyid(relowner) AS "owner"
FROM pg_class, cagg_info
WHERE (oid = user_view OR relname = mat_table)
AND user_view::text = 'owner_check';

DROP TABLESPACE tablespace1;
DROP TABLESPACE tablespace2;

0 comments on commit 0f3697f

Please sign in to comment.